diff --git a/doc/pub/week45/html/._week45-bs000.html b/doc/pub/week45/html/._week45-bs000.html
index 5bdd49d56..b661511d2 100644
--- a/doc/pub/week45/html/._week45-bs000.html
+++ b/doc/pub/week45/html/._week45-bs000.html
@@ -187,7 +187,7 @@ MathJax.Hub.Config({
-Nov 8, 2022
+Nov 10, 2022
diff --git a/doc/pub/week45/html/._week45-bs002.html b/doc/pub/week45/html/._week45-bs002.html
index bc9bc037f..6c1b2240e 100644
--- a/doc/pub/week45/html/._week45-bs002.html
+++ b/doc/pub/week45/html/._week45-bs002.html
@@ -194,6 +194,7 @@ MathJax.Hub.Config({
from sklearn.datasets import load_breast_cancer
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
+from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import cross_validate
diff --git a/doc/pub/week45/html/week45-bs.html b/doc/pub/week45/html/week45-bs.html
index 5bdd49d56..b661511d2 100644
--- a/doc/pub/week45/html/week45-bs.html
+++ b/doc/pub/week45/html/week45-bs.html
@@ -187,7 +187,7 @@ MathJax.Hub.Config({
-Nov 8, 2022
+Nov 10, 2022
diff --git a/doc/pub/week45/html/week45-reveal.html b/doc/pub/week45/html/week45-reveal.html
index c71cd111c..cc7ffc03c 100644
--- a/doc/pub/week45/html/week45-reveal.html
+++ b/doc/pub/week45/html/week45-reveal.html
@@ -184,7 +184,7 @@ MathJax.Hub.Config({
-Nov 8, 2022
+Nov 10, 2022
@@ -249,6 +249,7 @@ MathJax.Hub.Config({
from sklearn.datasets import load_breast_cancer
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
+from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import cross_validate
diff --git a/doc/pub/week45/html/week45-solarized.html b/doc/pub/week45/html/week45-solarized.html
index bcfcac7ab..f0c8aefe5 100644
--- a/doc/pub/week45/html/week45-solarized.html
+++ b/doc/pub/week45/html/week45-solarized.html
@@ -167,7 +167,7 @@ MathJax.Hub.Config({
-Nov 8, 2022
+Nov 10, 2022
@@ -225,6 +225,7 @@ MathJax.Hub.Config({
from sklearn.datasets import load_breast_cancer
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
+from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import cross_validate
diff --git a/doc/pub/week45/html/week45.html b/doc/pub/week45/html/week45.html
index fec9a604c..aa93caaed 100644
--- a/doc/pub/week45/html/week45.html
+++ b/doc/pub/week45/html/week45.html
@@ -244,7 +244,7 @@ MathJax.Hub.Config({
-Nov 8, 2022
+Nov 10, 2022
@@ -302,6 +302,7 @@ MathJax.Hub.Config({
from sklearn.datasets import load_breast_cancer
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
+from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import cross_validate
diff --git a/doc/pub/week45/ipynb/ipynb-week45-src.tar.gz b/doc/pub/week45/ipynb/ipynb-week45-src.tar.gz
index db51604f2..6adbec2fb 100644
Binary files a/doc/pub/week45/ipynb/ipynb-week45-src.tar.gz and b/doc/pub/week45/ipynb/ipynb-week45-src.tar.gz differ
diff --git a/doc/pub/week45/ipynb/week45.ipynb b/doc/pub/week45/ipynb/week45.ipynb
index c3b5d9726..3fa52058a 100644
--- a/doc/pub/week45/ipynb/week45.ipynb
+++ b/doc/pub/week45/ipynb/week45.ipynb
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
- "id": "69ffa59c",
+ "id": "7a978d39",
"metadata": {
"editable": true
},
@@ -14,7 +14,7 @@
},
{
"cell_type": "markdown",
- "id": "22893fe1",
+ "id": "e0283e6d",
"metadata": {
"editable": true
},
@@ -22,14 +22,14 @@
"# Week 45: Decisions Trees, Random Forests, Bagging and Boosting\n",
"**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n",
"\n",
- "Date: **Nov 8, 2022**\n",
+ "Date: **Nov 10, 2022**\n",
"\n",
"Copyright 1999-2022, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license"
]
},
{
"cell_type": "markdown",
- "id": "6ed587a5",
+ "id": "a3136428",
"metadata": {
"editable": true
},
@@ -57,7 +57,7 @@
},
{
"cell_type": "markdown",
- "id": "20a33b84",
+ "id": "e2e0134a",
"metadata": {
"editable": true
},
@@ -68,7 +68,7 @@
{
"cell_type": "code",
"execution_count": 1,
- "id": "4cddf972",
+ "id": "2fdb28db",
"metadata": {
"collapsed": false,
"editable": true
@@ -93,6 +93,7 @@
"from sklearn.datasets import load_breast_cancer\n",
"from sklearn.svm import SVC\n",
"from sklearn.linear_model import LogisticRegression\n",
+ "from sklearn.tree import DecisionTreeClassifier\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.preprocessing import LabelEncoder\n",
"from sklearn.model_selection import cross_validate\n",
@@ -167,7 +168,7 @@
},
{
"cell_type": "markdown",
- "id": "3f3b4ec0",
+ "id": "eb3a5ee9",
"metadata": {
"editable": true
},
@@ -187,7 +188,7 @@
},
{
"cell_type": "markdown",
- "id": "1d2fa07b",
+ "id": "b793724a",
"metadata": {
"editable": true
},
@@ -201,7 +202,7 @@
},
{
"cell_type": "markdown",
- "id": "ae378fc0",
+ "id": "f2c22ac4",
"metadata": {
"editable": true
},
@@ -213,7 +214,7 @@
},
{
"cell_type": "markdown",
- "id": "b659b768",
+ "id": "cbaa4a6f",
"metadata": {
"editable": true
},
@@ -230,7 +231,7 @@
},
{
"cell_type": "markdown",
- "id": "25f72330",
+ "id": "c0ea84be",
"metadata": {
"editable": true
},
@@ -242,7 +243,7 @@
},
{
"cell_type": "markdown",
- "id": "45aa3f32",
+ "id": "94736458",
"metadata": {
"editable": true
},
@@ -256,7 +257,7 @@
},
{
"cell_type": "markdown",
- "id": "b4c3c38c",
+ "id": "b41565d5",
"metadata": {
"editable": true
},
@@ -268,7 +269,7 @@
},
{
"cell_type": "markdown",
- "id": "3e6e9b65",
+ "id": "c1b8efd9",
"metadata": {
"editable": true
},
@@ -281,7 +282,7 @@
},
{
"cell_type": "markdown",
- "id": "05fbdabe",
+ "id": "7f7e8a84",
"metadata": {
"editable": true
},
@@ -293,7 +294,7 @@
},
{
"cell_type": "markdown",
- "id": "1ed2d372",
+ "id": "ed8d7b77",
"metadata": {
"editable": true
},
@@ -303,7 +304,7 @@
},
{
"cell_type": "markdown",
- "id": "551e69df",
+ "id": "4246f00d",
"metadata": {
"editable": true
},
@@ -331,7 +332,7 @@
},
{
"cell_type": "markdown",
- "id": "a9173c39",
+ "id": "6ccbc0e9",
"metadata": {
"editable": true
},
@@ -347,7 +348,7 @@
},
{
"cell_type": "markdown",
- "id": "51535a0f",
+ "id": "4b4f4548",
"metadata": {
"editable": true
},
@@ -359,7 +360,7 @@
},
{
"cell_type": "markdown",
- "id": "ddad2f59",
+ "id": "f5434423",
"metadata": {
"editable": true
},
@@ -370,7 +371,7 @@
},
{
"cell_type": "markdown",
- "id": "96d2aee2",
+ "id": "a4c33651",
"metadata": {
"editable": true
},
@@ -382,7 +383,7 @@
},
{
"cell_type": "markdown",
- "id": "ed53f899",
+ "id": "d25fd438",
"metadata": {
"editable": true
},
@@ -392,7 +393,7 @@
},
{
"cell_type": "markdown",
- "id": "573671e9",
+ "id": "261f7514",
"metadata": {
"editable": true
},
@@ -404,7 +405,7 @@
},
{
"cell_type": "markdown",
- "id": "4e261b39",
+ "id": "ee46c495",
"metadata": {
"editable": true
},
@@ -414,7 +415,7 @@
},
{
"cell_type": "markdown",
- "id": "88f94be1",
+ "id": "4b6cf657",
"metadata": {
"editable": true
},
@@ -426,7 +427,7 @@
},
{
"cell_type": "markdown",
- "id": "8d5732c1",
+ "id": "07fd8905",
"metadata": {
"editable": true
},
@@ -436,7 +437,7 @@
},
{
"cell_type": "markdown",
- "id": "244df7ff",
+ "id": "17699be3",
"metadata": {
"editable": true
},
@@ -448,7 +449,7 @@
},
{
"cell_type": "markdown",
- "id": "15a2125d",
+ "id": "91046f02",
"metadata": {
"editable": true
},
@@ -462,7 +463,7 @@
},
{
"cell_type": "markdown",
- "id": "8f34d330",
+ "id": "b886c010",
"metadata": {
"editable": true
},
@@ -478,7 +479,7 @@
},
{
"cell_type": "markdown",
- "id": "b58de40d",
+ "id": "b5f82299",
"metadata": {
"editable": true
},
@@ -490,7 +491,7 @@
},
{
"cell_type": "markdown",
- "id": "630a85ef",
+ "id": "9843cd00",
"metadata": {
"editable": true
},
@@ -506,7 +507,7 @@
},
{
"cell_type": "markdown",
- "id": "e19f8094",
+ "id": "1090524c",
"metadata": {
"editable": true
},
@@ -518,7 +519,7 @@
},
{
"cell_type": "markdown",
- "id": "1bd1ca26",
+ "id": "f3088911",
"metadata": {
"editable": true
},
@@ -528,7 +529,7 @@
},
{
"cell_type": "markdown",
- "id": "28c94b01",
+ "id": "4ba3fedb",
"metadata": {
"editable": true
},
@@ -540,7 +541,7 @@
},
{
"cell_type": "markdown",
- "id": "cd5a9b54",
+ "id": "6e447640",
"metadata": {
"editable": true
},
@@ -552,7 +553,7 @@
},
{
"cell_type": "markdown",
- "id": "c80e3529",
+ "id": "a114cfe7",
"metadata": {
"editable": true
},
@@ -564,7 +565,7 @@
},
{
"cell_type": "markdown",
- "id": "9836bef1",
+ "id": "a00f5ba6",
"metadata": {
"editable": true
},
@@ -575,7 +576,7 @@
},
{
"cell_type": "markdown",
- "id": "0118af58",
+ "id": "ea0be85b",
"metadata": {
"editable": true
},
@@ -587,7 +588,7 @@
},
{
"cell_type": "markdown",
- "id": "c12b4a0d",
+ "id": "a65ab9b7",
"metadata": {
"editable": true
},
@@ -598,7 +599,7 @@
},
{
"cell_type": "markdown",
- "id": "ed7915b4",
+ "id": "b7cffa99",
"metadata": {
"editable": true
},
@@ -610,7 +611,7 @@
},
{
"cell_type": "markdown",
- "id": "bed73666",
+ "id": "081d507c",
"metadata": {
"editable": true
},
@@ -620,7 +621,7 @@
},
{
"cell_type": "markdown",
- "id": "5cf114dd",
+ "id": "6e734ee3",
"metadata": {
"editable": true
},
@@ -632,7 +633,7 @@
},
{
"cell_type": "markdown",
- "id": "99c625b1",
+ "id": "ee564d2c",
"metadata": {
"editable": true
},
@@ -644,7 +645,7 @@
},
{
"cell_type": "markdown",
- "id": "d33941de",
+ "id": "a9cc2890",
"metadata": {
"editable": true
},
@@ -656,7 +657,7 @@
},
{
"cell_type": "markdown",
- "id": "8c90eae5",
+ "id": "3a89610f",
"metadata": {
"editable": true
},
@@ -668,7 +669,7 @@
},
{
"cell_type": "markdown",
- "id": "7d0065c2",
+ "id": "a2b97b3f",
"metadata": {
"editable": true
},
@@ -678,7 +679,7 @@
},
{
"cell_type": "markdown",
- "id": "30537f2c",
+ "id": "edd6b448",
"metadata": {
"editable": true
},
@@ -690,7 +691,7 @@
},
{
"cell_type": "markdown",
- "id": "d37b9ab1",
+ "id": "6e5b95b5",
"metadata": {
"editable": true
},
@@ -700,7 +701,7 @@
},
{
"cell_type": "markdown",
- "id": "c972723b",
+ "id": "3e656f64",
"metadata": {
"editable": true
},
@@ -712,7 +713,7 @@
},
{
"cell_type": "markdown",
- "id": "a4f86aa7",
+ "id": "871fb2d1",
"metadata": {
"editable": true
},
@@ -722,7 +723,7 @@
},
{
"cell_type": "markdown",
- "id": "25115dd1",
+ "id": "80d6e316",
"metadata": {
"editable": true
},
@@ -734,7 +735,7 @@
},
{
"cell_type": "markdown",
- "id": "1b3ec70a",
+ "id": "939363d6",
"metadata": {
"editable": true
},
@@ -744,7 +745,7 @@
},
{
"cell_type": "markdown",
- "id": "d99f2c28",
+ "id": "70e5b39d",
"metadata": {
"editable": true
},
@@ -756,7 +757,7 @@
},
{
"cell_type": "markdown",
- "id": "784d5cf1",
+ "id": "1394fd74",
"metadata": {
"editable": true
},
@@ -766,7 +767,7 @@
},
{
"cell_type": "markdown",
- "id": "3f2bfe8f",
+ "id": "6909eec6",
"metadata": {
"editable": true
},
@@ -778,7 +779,7 @@
},
{
"cell_type": "markdown",
- "id": "ff895053",
+ "id": "d7b56142",
"metadata": {
"editable": true
},
@@ -798,7 +799,7 @@
},
{
"cell_type": "markdown",
- "id": "2a56a0f2",
+ "id": "a31c3041",
"metadata": {
"editable": true
},
@@ -810,7 +811,7 @@
},
{
"cell_type": "markdown",
- "id": "e9df78f4",
+ "id": "a771e5f4",
"metadata": {
"editable": true
},
@@ -820,7 +821,7 @@
},
{
"cell_type": "markdown",
- "id": "dbd929f5",
+ "id": "9a9c96d1",
"metadata": {
"editable": true
},
@@ -836,7 +837,7 @@
},
{
"cell_type": "markdown",
- "id": "c2afd475",
+ "id": "07323be9",
"metadata": {
"editable": true
},
@@ -848,7 +849,7 @@
},
{
"cell_type": "markdown",
- "id": "a7c172ee",
+ "id": "3c996e3d",
"metadata": {
"editable": true
},
@@ -876,7 +877,7 @@
},
{
"cell_type": "markdown",
- "id": "a8b51916",
+ "id": "39637844",
"metadata": {
"editable": true
},
@@ -889,7 +890,7 @@
{
"cell_type": "code",
"execution_count": 2,
- "id": "6cb3eb3c",
+ "id": "76903ecb",
"metadata": {
"collapsed": false,
"editable": true
@@ -921,7 +922,7 @@
},
{
"cell_type": "markdown",
- "id": "dd395b89",
+ "id": "ca8755d2",
"metadata": {
"editable": true
},
@@ -939,7 +940,7 @@
},
{
"cell_type": "markdown",
- "id": "1ed1052e",
+ "id": "f0463b3c",
"metadata": {
"editable": true
},
@@ -952,7 +953,7 @@
},
{
"cell_type": "markdown",
- "id": "323f5865",
+ "id": "8b514ec6",
"metadata": {
"editable": true
},
@@ -964,7 +965,7 @@
},
{
"cell_type": "markdown",
- "id": "4e5c8544",
+ "id": "423db9ff",
"metadata": {
"editable": true
},
@@ -974,7 +975,7 @@
},
{
"cell_type": "markdown",
- "id": "e9b4fb88",
+ "id": "06b275d6",
"metadata": {
"editable": true
},
@@ -986,7 +987,7 @@
},
{
"cell_type": "markdown",
- "id": "ab01f1bc",
+ "id": "4ca06fe0",
"metadata": {
"editable": true
},
@@ -996,7 +997,7 @@
},
{
"cell_type": "markdown",
- "id": "320732d2",
+ "id": "da5754bd",
"metadata": {
"editable": true
},
@@ -1008,7 +1009,7 @@
},
{
"cell_type": "markdown",
- "id": "b7977014",
+ "id": "c0f54e05",
"metadata": {
"editable": true
},
@@ -1021,7 +1022,7 @@
},
{
"cell_type": "markdown",
- "id": "0fe86cf6",
+ "id": "9152ac2b",
"metadata": {
"editable": true
},
@@ -1033,7 +1034,7 @@
},
{
"cell_type": "markdown",
- "id": "8a51677a",
+ "id": "a4d9623f",
"metadata": {
"editable": true
},
@@ -1045,7 +1046,7 @@
},
{
"cell_type": "markdown",
- "id": "2ebafed4",
+ "id": "5c95acd3",
"metadata": {
"editable": true
},
@@ -1057,7 +1058,7 @@
},
{
"cell_type": "markdown",
- "id": "afde236a",
+ "id": "08667505",
"metadata": {
"editable": true
},
@@ -1067,7 +1068,7 @@
},
{
"cell_type": "markdown",
- "id": "c529803e",
+ "id": "95940c93",
"metadata": {
"editable": true
},
@@ -1079,7 +1080,7 @@
},
{
"cell_type": "markdown",
- "id": "58eb0e6c",
+ "id": "888345e6",
"metadata": {
"editable": true
},
@@ -1089,7 +1090,7 @@
},
{
"cell_type": "markdown",
- "id": "0c3a10a0",
+ "id": "3aa4d471",
"metadata": {
"editable": true
},
@@ -1105,7 +1106,7 @@
},
{
"cell_type": "markdown",
- "id": "6ebff1c2",
+ "id": "7778dcfd",
"metadata": {
"editable": true
},
@@ -1117,7 +1118,7 @@
},
{
"cell_type": "markdown",
- "id": "cea9a34e",
+ "id": "70b44d33",
"metadata": {
"editable": true
},
@@ -1138,7 +1139,7 @@
},
{
"cell_type": "markdown",
- "id": "d226041b",
+ "id": "4ca117e7",
"metadata": {
"editable": true
},
@@ -1149,7 +1150,7 @@
{
"cell_type": "code",
"execution_count": 3,
- "id": "bcf8516d",
+ "id": "2039b018",
"metadata": {
"collapsed": false,
"editable": true
@@ -1201,7 +1202,7 @@
},
{
"cell_type": "markdown",
- "id": "b96fef32",
+ "id": "6db0b31b",
"metadata": {
"editable": true
},
@@ -1212,7 +1213,7 @@
{
"cell_type": "code",
"execution_count": 4,
- "id": "4f8ce2c3",
+ "id": "de26bfe7",
"metadata": {
"collapsed": false,
"editable": true
@@ -1263,7 +1264,7 @@
},
{
"cell_type": "markdown",
- "id": "35426902",
+ "id": "98629401",
"metadata": {
"editable": true
},
@@ -1286,7 +1287,7 @@
},
{
"cell_type": "markdown",
- "id": "bd80e051",
+ "id": "70f7d4cc",
"metadata": {
"editable": true
},
@@ -1297,7 +1298,7 @@
{
"cell_type": "code",
"execution_count": 5,
- "id": "0f0e552d",
+ "id": "01dab946",
"metadata": {
"collapsed": false,
"editable": true
@@ -1349,7 +1350,7 @@
},
{
"cell_type": "markdown",
- "id": "70eaee5f",
+ "id": "f132426b",
"metadata": {
"editable": true
},
@@ -1362,7 +1363,7 @@
{
"cell_type": "code",
"execution_count": 6,
- "id": "dbe4ac22",
+ "id": "7bed8a59",
"metadata": {
"collapsed": false,
"editable": true
diff --git a/doc/src/week44/mlp.py b/doc/src/week44/mlp.py
new file mode 100644
index 000000000..121900072
--- /dev/null
+++ b/doc/src/week44/mlp.py
@@ -0,0 +1,75 @@
+"""
+Code to test Ridge and NNs using Scikit-Learn only
+"""
+
+import numpy as np
+import pandas as pd
+import matplotlib.pyplot as plt
+from sklearn.model_selection import train_test_split
+from sklearn import linear_model
+from sklearn.neural_network import MLPRegressor
+from sklearn.metrics import accuracy_score
+import seaborn as sns
+
+
+def MSE(y_data,y_model):
+ n = np.size(y_model)
+ return np.sum((y_data-y_model)**2)/n
+# A seed just to ensure that the random numbers are the same for every run.
+# Useful for eventual debugging.
+np.random.seed(315)
+
+n = 1000
+x = np.random.rand(n)
+y = x+x*x
+X = np.zeros((n,1))
+X[:,0] = x
+
+
+# We split the data in test and training data
+X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
+
+# Decide which values of lambda to use
+
+nlambdas = 5
+lmbd_vals = np.logspace(-4, 0, nlambdas)
+MSERidgePredict = np.zeros(nlambdas)
+for i in range(nlambdas):
+ lmb = lmbd_vals[i]
+ RegRidge = linear_model.Ridge(lmb)
+ RegRidge.fit(X_train,y_train)
+ ypredictRidge = RegRidge.predict(X_test)
+ MSERidgePredict[i] = MSE(y_test,ypredictRidge)
+
+plt.figure()
+plt.plot(np.log10(lmbd_vals), MSERidgePredict, 'g--', label = 'MSE SL Ridge Test')
+plt.xlabel('log10(lambda)')
+plt.ylabel('MSE')
+plt.legend()
+plt.show()
+
+# Neural Network part
+
+n_hidden_neurons = 100
+epochs = 100
+# store models for later use
+eta_vals = np.logspace(-4, 0, 5)
+# store the models for later use
+DNN_scikit = np.zeros((len(eta_vals), len(lmbd_vals)), dtype=object)
+test_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))
+sns.set()
+for i, eta in enumerate(eta_vals):
+ for j, lmbd in enumerate(lmbd_vals):
+ dnn = MLPRegressor(hidden_layer_sizes=(n_hidden_neurons), activation='logistic',
+ alpha=lmbd, learning_rate_init=eta, max_iter=epochs)
+ dnn.fit(X_train, y_train)
+ ypredictMLP = dnn.predict(X_test)
+ test_accuracy[i][j] = MSE(ypredictMLP, y_test)
+
+fig, ax = plt.subplots(figsize = (10, 10))
+sns.heatmap(test_accuracy, annot=True, ax=ax, cmap="viridis")
+ax.set_title("Test Accuracy")
+ax.set_ylabel("$\eta$")
+ax.set_xlabel("$\lambda$")
+plt.show()
+
diff --git a/doc/src/week44/simple.py b/doc/src/week44/simple.py
new file mode 100644
index 000000000..0231e38a1
--- /dev/null
+++ b/doc/src/week44/simple.py
@@ -0,0 +1,104 @@
+"""
+Code to test Ridge and NNs using Scikit-Learn only
+"""
+
+import numpy as np
+import pandas as pd
+import matplotlib.pyplot as plt
+from sklearn.model_selection import train_test_split
+from sklearn import linear_model
+from sklearn.neural_network import MLPRegressor
+from sklearn.metrics import accuracy_score
+import seaborn as sns
+
+
+def MSE(y_data,y_model):
+ n = np.size(y_model)
+ return np.sum((y_data-y_model)**2)/n
+# A seed just to ensure that the random numbers are the same for every run.
+# Useful for eventual debugging.
+np.random.seed(315)
+
+n = 100
+x = np.random.rand(n)
+y = x+x*x#np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)
+
+Maxpolydegree = 2
+X = np.zeros((n,Maxpolydegree-1))
+
+for degree in range(1,Maxpolydegree): #No intercept column
+ X[:,degree-1] = x**(degree)
+
+# We split the data in test and training data
+X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
+
+# Decide which values of lambda to use
+
+nlambdas = 10
+lmbd_vals = np.logspace(-4, 0, nlambdas)
+MSERidgePredict = np.zeros(nlambdas)
+for i in range(nlambdas):
+ lmb = lmbd_vals[i]
+ RegRidge = linear_model.Ridge(lmb)
+ RegRidge.fit(X_train,y_train)
+ ypredictRidge = RegRidge.predict(X_test)
+ MSERidgePredict[i] = MSE(y_test,ypredictRidge)
+
+plt.figure()
+plt.plot(np.log10(lmbd_vals), MSERidgePredict, 'g--', label = 'MSE SL Ridge Test')
+plt.xlabel('log10(lambda)')
+plt.ylabel('MSE')
+plt.legend()
+plt.show()
+
+# Neural Network part
+
+n_hidden_neurons = 50
+epochs = 100
+# store models for later use
+eta_vals = np.logspace(-4, 0, 10)
+# store the models for later use
+DNN_scikit = np.zeros((len(eta_vals), len(lmbd_vals)), dtype=object)
+test_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))
+sns.set()
+for i, eta in enumerate(eta_vals):
+ for j, lmbd in enumerate(lmbd_vals):
+ dnn = MLPRegressor(hidden_layer_sizes=(n_hidden_neurons), activation='logistic',
+ alpha=lmbd, learning_rate_init=eta, max_iter=epochs)
+ dnn.fit(X_train, y_train)
+ ypredictMLP = dnn.predict(X_test)
+ test_accuracy[i][j] = MSE(ypredictMLP, y_test)
+
+fig, ax = plt.subplots(figsize = (10, 10))
+sns.heatmap(test_accuracy, annot=True, ax=ax, cmap="viridis")
+ax.set_title("Training Accuracy")
+ax.set_ylabel("$\eta$")
+ax.set_xlabel("$\lambda$")
+plt.show()
+
+# Now we redefine our design matrix to include only the x-values and try out our NN
+
+X = np.zeros((n,1))
+X[:,0] = x
+
+# We split the data in test and training data again
+X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
+# Repeat the NN calculation
+DNN_scikit = np.zeros((len(eta_vals), len(lmbd_vals)), dtype=object)
+test_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))
+sns.set()
+for i, eta in enumerate(eta_vals):
+ for j, lmbd in enumerate(lmbd_vals):
+ dnn = MLPRegressor(hidden_layer_sizes=(n_hidden_neurons), activation='logistic',
+ alpha=lmbd, learning_rate_init=eta, max_iter=epochs)
+ dnn.fit(X_train, y_train)
+ ypredictMLP = dnn.predict(X_test)
+ test_accuracy[i][j] = MSE(ypredictMLP, y_test)
+
+fig, ax = plt.subplots(figsize = (10, 10))
+sns.heatmap(test_accuracy, annot=True, ax=ax, cmap="viridis")
+ax.set_title("Training Accuracy")
+ax.set_ylabel("$\eta$")
+ax.set_xlabel("$\lambda$")
+plt.show()
+
diff --git a/doc/src/week45/week45.do.txt b/doc/src/week45/week45.do.txt
index 810ee6763..410d6fce0 100644
--- a/doc/src/week45/week45.do.txt
+++ b/doc/src/week45/week45.do.txt
@@ -40,6 +40,7 @@ from pydot import graph_from_dot_data
from sklearn.datasets import load_breast_cancer
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
+from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import cross_validate