From 666100d74a38ff958d213151258cf1a257519041 Mon Sep 17 00:00:00 2001 From: mhjensen Date: Sat, 19 Sep 2020 22:18:32 +0200 Subject: [PATCH] updating files --- doc/pub/week38/html/._week38-bs000.html | 2 +- doc/pub/week38/html/._week38-bs008.html | 18 ++--- doc/pub/week38/html/._week38-bs009.html | 6 +- doc/pub/week38/html/._week38-bs011.html | 2 +- doc/pub/week38/html/._week38-bs013.html | 2 +- doc/pub/week38/html/._week38-bs014.html | 2 +- doc/pub/week38/html/._week38-bs015.html | 2 +- doc/pub/week38/html/._week38-bs019.html | 4 +- doc/pub/week38/html/._week38-bs020.html | 2 +- doc/pub/week38/html/._week38-bs027.html | 2 +- doc/pub/week38/html/._week38-bs030.html | 6 +- doc/pub/week38/html/._week38-bs039.html | 8 +-- doc/pub/week38/html/._week38-bs040.html | 2 +- doc/pub/week38/html/._week38-bs042.html | 14 ++-- doc/pub/week38/html/week38-bs.html | 2 +- doc/pub/week38/html/week38-reveal.html | 66 ++++++++--------- doc/pub/week38/html/week38-solarized.html | 66 ++++++++--------- doc/pub/week38/html/week38.html | 72 +++++++++---------- doc/pub/week38/ipynb/ipynb-week38-src.tar.gz | Bin 192 -> 191 bytes doc/pub/week38/ipynb/week38.ipynb | 6 +- 20 files changed, 142 insertions(+), 142 deletions(-) diff --git a/doc/pub/week38/html/._week38-bs000.html b/doc/pub/week38/html/._week38-bs000.html index e0f7360df..f03c8cdf8 100644 --- a/doc/pub/week38/html/._week38-bs000.html +++ b/doc/pub/week38/html/._week38-bs000.html @@ -222,7 +222,7 @@ MathJax.Hub.Config({
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

-

Sep 18, 2020

+

Sep 19, 2020


diff --git a/doc/pub/week38/html/._week38-bs008.html b/doc/pub/week38/html/._week38-bs008.html index d18be34aa..143f403fe 100644 --- a/doc/pub/week38/html/._week38-bs008.html +++ b/doc/pub/week38/html/._week38-bs008.html @@ -232,21 +232,21 @@ polydegree = np x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2) for degree in range(maxdegree): - model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False)) + model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False)) y_pred = np.empty((y_test.shape[0], n_boostraps)) for i in range(n_boostraps): x_, y_ = resample(x_train, y_train) y_pred[:, i] = model.fit(x_, y_).predict(x_test).ravel() polydegree[degree] = degree - error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) ) - bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 ) - variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) ) - print('Polynomial degree:', degree) - print('Error:', error[degree]) - print('Bias^2:', bias[degree]) - print('Var:', variance[degree]) - print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree])) + error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) ) + bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 ) + variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) ) + print('Polynomial degree:', degree) + print('Error:', error[degree]) + print('Bias^2:', bias[degree]) + print('Var:', variance[degree]) + print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree])) plt.plot(polydegree, error, label='Error') plt.plot(polydegree, bias, label='bias') diff --git a/doc/pub/week38/html/._week38-bs009.html b/doc/pub/week38/html/._week38-bs009.html index e3e41860a..aafd89d74 100644 --- a/doc/pub/week38/html/._week38-bs009.html +++ b/doc/pub/week38/html/._week38-bs009.html @@ -229,7 +229,7 @@ MathJax.Hub.Config({ training data. """ -print(__doc__) +print(__doc__) import numpy as np import matplotlib.pyplot as plt @@ -256,7 +256,7 @@ plt.figure(figsize.setp(ax, xticks=(), yticks=()) polynomial_features = PolynomialFeatures(degree=degrees[i], - include_bias=False) + include_bias=False) linear_regression = LinearRegression() pipeline = Pipeline([("polynomial_features", polynomial_features), ("linear_regression", linear_regression)]) @@ -275,7 +275,7 @@ plt.figure(figsize.xlim((0, 1)) plt.ylim((-2, 2)) plt.legend(loc="best") - plt.title("Degree {}\nMSE = {:.2e}(+/- {:.2e})".format( + plt.title("Degree {}\nMSE = {:.2e}(+/- {:.2e})".format( degrees[i], -scores.mean(), scores.std())) plt.show() diff --git a/doc/pub/week38/html/._week38-bs011.html b/doc/pub/week38/html/._week38-bs011.html index 708b153de..f7f17a6b6 100644 --- a/doc/pub/week38/html/._week38-bs011.html +++ b/doc/pub/week38/html/._week38-bs011.html @@ -236,7 +236,7 @@ with their respective energies. import scipy.linalg as scl from sklearn.model_selection import train_test_split import tqdm -sns.set(color_codes=True) +sns.set(color_codes=True) cmap_args=dict(vmin=-1., vmax=1., cmap='seismic') L = 40 diff --git a/doc/pub/week38/html/._week38-bs013.html b/doc/pub/week38/html/._week38-bs013.html index 93870ee18..52ab2937a 100644 --- a/doc/pub/week38/html/._week38-bs013.html +++ b/doc/pub/week38/html/._week38-bs013.html @@ -245,7 +245,7 @@ X_test_own = np

def ols_inv(x: np.ndarray, y: np.ndarray) -> np.ndarray:
-    return scl.inv(x.T @ x) @ (x.T @ y)
+    return scl.inv(x.T @ x) @ (x.T @ y)
 beta = ols_inv(X_train_own, y_train)
 

diff --git a/doc/pub/week38/html/._week38-bs014.html b/doc/pub/week38/html/._week38-bs014.html index b4c1426de..d47ba9fac 100644 --- a/doc/pub/week38/html/._week38-bs014.html +++ b/doc/pub/week38/html/._week38-bs014.html @@ -247,7 +247,7 @@ linear system as an equation would reduce this down to

def ols_svd(x: np.ndarray, y: np.ndarray) -> np.ndarray:
     u, s, v = scl.svd(x)
-    return v.T @ scl.pinv(scl.diagsvd(s, u.shape[0], v.shape[0])) @ u.T @ y
+    return v.T @ scl.pinv(scl.diagsvd(s, u.shape[0], v.shape[0])) @ u.T @ y
 

diff --git a/doc/pub/week38/html/._week38-bs015.html b/doc/pub/week38/html/._week38-bs015.html index cef66a352..3e428028f 100644 --- a/doc/pub/week38/html/._week38-bs015.html +++ b/doc/pub/week38/html/._week38-bs015.html @@ -236,7 +236,7 @@ We will look at a system of \( L = 40 \) spins with a coupling constant of \( J from sklearn.model_selection import train_test_split import sklearn.linear_model as skl import tqdm -sns.set(color_codes=True) +sns.set(color_codes=True) cmap_args=dict(vmin=-1., vmax=1., cmap='seismic') L = 40 diff --git a/doc/pub/week38/html/._week38-bs019.html b/doc/pub/week38/html/._week38-bs019.html index ce094b57c..79f24a7a9 100644 --- a/doc/pub/week38/html/._week38-bs019.html +++ b/doc/pub/week38/html/._week38-bs019.html @@ -229,7 +229,7 @@ colors = { lambdas, train_errors[key], colors[key], - label="Train {0}".format(key), + label="Train {0}".format(key), linewidth=4.0 ) @@ -238,7 +238,7 @@ colors = { lambdas, test_errors[key], colors[key] + "--", - label="Test {0}".format(key), + label="Test {0}".format(key), linewidth=4.0 ) plt.legend(loc="best", fontsize=18) diff --git a/doc/pub/week38/html/._week38-bs020.html b/doc/pub/week38/html/._week38-bs020.html index 6048c5960..9b74762a1 100644 --- a/doc/pub/week38/html/._week38-bs020.html +++ b/doc/pub/week38/html/._week38-bs020.html @@ -206,7 +206,7 @@ MathJax.Hub.Config({

Friday September 18: Intro to Logistic Regression

-Video of Lecture and link to handwritten notes. +Video of Lecture and link to handwritten notes.

diff --git a/doc/pub/week38/html/._week38-bs027.html b/doc/pub/week38/html/._week38-bs027.html index 1ba595ea8..393e72063 100644 --- a/doc/pub/week38/html/._week38-bs027.html +++ b/doc/pub/week38/html/._week38-bs027.html @@ -246,7 +246,7 @@ DATA_ID = " return os.path.join(DATA_ID, dat_id) def save_fig(fig_id): - plt.savefig(image_path(fig_id) + ".png", format='png') + plt.savefig(image_path(fig_id) + ".png", format='png') infile = open(data_path("chddata.csv"),'r') diff --git a/doc/pub/week38/html/._week38-bs030.html b/doc/pub/week38/html/._week38-bs030.html index d30786436..0d82b723f 100644 --- a/doc/pub/week38/html/._week38-bs030.html +++ b/doc/pub/week38/html/._week38-bs030.html @@ -229,7 +229,7 @@ ax = fig..plot(z, sigma) ax.set_ylim([-0.1, 1.1]) ax.set_xlim([-5,5]) -ax.grid(True) +ax.grid(True) ax.set_xlabel('z') ax.set_title('sigmoid function') @@ -245,7 +245,7 @@ ax = fig..plot(z, step) ax.set_ylim([-0.5, 1.5]) ax.set_xlim([-5,5]) -ax.grid(True) +ax.grid(True) ax.set_xlabel('z') ax.set_title('step function') @@ -260,7 +260,7 @@ ax = fig..plot(z, t) ax.set_ylim([-1.0, 1.0]) ax.set_xlim([-2*mt.pi,2*mt.pi]) -ax.grid(True) +ax.grid(True) ax.set_xlabel('z') ax.set_title('tanh function') diff --git a/doc/pub/week38/html/._week38-bs039.html b/doc/pub/week38/html/._week38-bs039.html index 344968bcc..e8ed9d4cb 100644 --- a/doc/pub/week38/html/._week38-bs039.html +++ b/doc/pub/week38/html/._week38-bs039.html @@ -223,12 +223,12 @@ classification. cancer = load_breast_cancer() X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0) -print(X_train.shape) -print(X_test.shape) +print(X_train.shape) +print(X_test.shape) # Logistic Regression logreg = LogisticRegression(solver='lbfgs') logreg.fit(X_train, y_train) -print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) +print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) #now scale the data from sklearn.preprocessing import StandardScaler scaler = StandardScaler() @@ -237,7 +237,7 @@ X_train_scaled = scaler= scaler.transform(X_test) # Logistic Regression logreg.fit(X_train_scaled, y_train) -print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) +print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))

diff --git a/doc/pub/week38/html/._week38-bs040.html b/doc/pub/week38/html/._week38-bs040.html index 957f0bc92..c8f853e98 100644 --- a/doc/pub/week38/html/._week38-bs040.html +++ b/doc/pub/week38/html/._week38-bs040.html @@ -243,7 +243,7 @@ correlation_matrix = cancerpd# use the heatmap function from seaborn to plot the correlation matrix # annot = True to print the values inside the square plt.figure(figsize=(15,8)) -sns.heatmap(data=correlation_matrix, annot=True) +sns.heatmap(data=correlation_matrix, annot=True) plt.show()

diff --git a/doc/pub/week38/html/._week38-bs042.html b/doc/pub/week38/html/._week38-bs042.html index ea7f33026..37d57fd69 100644 --- a/doc/pub/week38/html/._week38-bs042.html +++ b/doc/pub/week38/html/._week38-bs042.html @@ -217,12 +217,12 @@ MathJax.Hub.Config({ cancer = load_breast_cancer() X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0) -print(X_train.shape) -print(X_test.shape) +print(X_train.shape) +print(X_test.shape) # Logistic Regression logreg = LogisticRegression(solver='lbfgs') logreg.fit(X_train, y_train) -print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) +print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) #now scale the data from sklearn.preprocessing import StandardScaler scaler = StandardScaler() @@ -231,20 +231,20 @@ X_train_scaled = scaler= scaler.transform(X_test) # Logistic Regression logreg.fit(X_train_scaled, y_train) -print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) +print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import cross_validate #Cross validation accuracy = cross_validate(logreg,X_test_scaled,y_test,cv=10)['test_score'] -print(accuracy) -print("Test set accuracy with Logistic Regression and scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) +print(accuracy) +print("Test set accuracy with Logistic Regression and scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) import scikitplot as skplt y_pred = logreg.predict(X_test_scaled) -skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True) +skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True) plt.show() y_probas = logreg.predict_proba(X_test_scaled) skplt.metrics.plot_roc(y_test, y_probas) diff --git a/doc/pub/week38/html/week38-bs.html b/doc/pub/week38/html/week38-bs.html index e0f7360df..f03c8cdf8 100644 --- a/doc/pub/week38/html/week38-bs.html +++ b/doc/pub/week38/html/week38-bs.html @@ -222,7 +222,7 @@ MathJax.Hub.Config({

[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

-

Sep 18, 2020

+

Sep 19, 2020


diff --git a/doc/pub/week38/html/week38-reveal.html b/doc/pub/week38/html/week38-reveal.html index 44f13e13f..6f3707bea 100644 --- a/doc/pub/week38/html/week38-reveal.html +++ b/doc/pub/week38/html/week38-reveal.html @@ -148,7 +148,7 @@ MathJax.Hub.Config({

[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

 
-

Sep 18, 2020

+

Sep 19, 2020


@@ -456,21 +456,21 @@ polydegree = np.zeros(maxdegree) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2) for degree in range(maxdegree): - model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False)) + model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False)) y_pred = np.empty((y_test.shape[0], n_boostraps)) for i in range(n_boostraps): x_, y_ = resample(x_train, y_train) y_pred[:, i] = model.fit(x_, y_).predict(x_test).ravel() polydegree[degree] = degree - error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) ) - bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 ) - variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) ) - print('Polynomial degree:', degree) - print('Error:', error[degree]) - print('Bias^2:', bias[degree]) - print('Var:', variance[degree]) - print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree])) + error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) ) + bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 ) + variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) ) + print('Polynomial degree:', degree) + print('Error:', error[degree]) + print('Bias^2:', bias[degree]) + print('Var:', variance[degree]) + print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree])) plt.plot(polydegree, error, label='Error') plt.plot(polydegree, bias, label='bias') @@ -508,7 +508,7 @@ plt.show() training data. """ -print(__doc__) +print(__doc__) import numpy as np import matplotlib.pyplot as plt @@ -535,7 +535,7 @@ plt.figure(figsize=(14, False) + include_bias=False) linear_regression = LinearRegression() pipeline = Pipeline([("polynomial_features", polynomial_features), ("linear_regression", linear_regression)]) @@ -641,7 +641,7 @@ with their respective energies. import scipy.linalg as scl from sklearn.model_selection import train_test_split import tqdm -sns.set(color_codes=True) +sns.set(color_codes=True) cmap_args=dict(vmin=-1., vmax=1., cmap='seismic') L = 40 @@ -768,7 +768,7 @@ X_test_own = np.concatenate(

def ols_inv(x: np.ndarray, y: np.ndarray) -> np.ndarray:
-    return scl.inv(x.T @ x) @ (x.T @ y)
+    return scl.inv(x.T @ x) @ (x.T @ y)
 beta = ols_inv(X_train_own, y_train)
 
@@ -825,7 +825,7 @@ linear system as an equation would reduce this down to
def ols_svd(x: np.ndarray, y: np.ndarray) -> np.ndarray:
     u, s, v = scl.svd(x)
-    return v.T @ scl.pinv(scl.diagsvd(s, u.shape[0], v.shape[0])) @ u.T @ y
+    return v.T @ scl.pinv(scl.diagsvd(s, u.shape[0], v.shape[0])) @ u.T @ y
 

@@ -903,7 +903,7 @@ We will look at a system of \( L = 40 \) spins with a coupling constant of \( J from sklearn.model_selection import train_test_split import sklearn.linear_model as skl import tqdm -sns.set(color_codes=True) +sns.set(color_codes=True) cmap_args=dict(vmin=-1., vmax=1., cmap='seismic') L = 40 @@ -1197,7 +1197,7 @@ other models for all values of \( \lambda \).

Friday September 18: Intro to Logistic Regression

-Video of Lecture and link to handwritten notes. +Video of Lecture and link to handwritten notes. @@ -1384,7 +1384,7 @@ DATA_ID = "DataFiles/" return os.path.join(DATA_ID, dat_id) def save_fig(fig_id): - plt.savefig(image_path(fig_id) + ".png", format='png') + plt.savefig(image_path(fig_id) + ".png", format='png') infile = open(data_path("chddata.csv"),'r') @@ -1500,7 +1500,7 @@ ax = fig.add_subplot(111) ax.plot(z, sigma) ax.set_ylim([-0.1, 1.1]) ax.set_xlim([-5,5]) -ax.grid(True) +ax.grid(True) ax.set_xlabel('z') ax.set_title('sigmoid function') @@ -1516,7 +1516,7 @@ ax = fig.add_subplot(111) ax.plot(z, step) ax.set_ylim([-0.5, 1.5]) ax.set_xlim([-5,5]) -ax.grid(True) +ax.grid(True) ax.set_xlabel('z') ax.set_title('step function') @@ -1531,7 +1531,7 @@ ax = fig.add_subplot(111) ax.plot(z, t) ax.set_ylim([-1.0, 1.0]) ax.set_xlim([-2*mt.pi,2*mt.pi]) -ax.grid(True) +ax.grid(True) ax.set_xlabel('z') ax.set_title('tanh function') @@ -1793,12 +1793,12 @@ classification. cancer = load_breast_cancer() X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0) -print(X_train.shape) -print(X_test.shape) +print(X_train.shape) +print(X_test.shape) # Logistic Regression logreg = LogisticRegression(solver='lbfgs') logreg.fit(X_train, y_train) -print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) +print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) #now scale the data from sklearn.preprocessing import StandardScaler scaler = StandardScaler() @@ -1807,7 +1807,7 @@ X_train_scaled = scaler.transform(X_train) X_test_scaled = scaler.transform(X_test) # Logistic Regression logreg.fit(X_train_scaled, y_train) -print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) +print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) @@ -1853,7 +1853,7 @@ correlation_matrix = cancerpd.corr().round(1 # use the heatmap function from seaborn to plot the correlation matrix # annot = True to print the values inside the square plt.figure(figsize=(15,8)) -sns.heatmap(data=correlation_matrix, annot=True) +sns.heatmap(data=correlation_matrix, annot=True) plt.show() @@ -1913,12 +1913,12 @@ applications. This will be discussed later this semester (0) -print(X_train.shape) -print(X_test.shape) +print(X_train.shape) +print(X_test.shape) # Logistic Regression logreg = LogisticRegression(solver='lbfgs') logreg.fit(X_train, y_train) -print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) +print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) #now scale the data from sklearn.preprocessing import StandardScaler scaler = StandardScaler() @@ -1927,20 +1927,20 @@ X_train_scaled = scaler.transform(X_train) X_test_scaled = scaler.transform(X_test) # Logistic Regression logreg.fit(X_train_scaled, y_train) -print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) +print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import cross_validate #Cross validation accuracy = cross_validate(logreg,X_test_scaled,y_test,cv=10)['test_score'] -print(accuracy) -print("Test set accuracy with Logistic Regression and scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) +print(accuracy) +print("Test set accuracy with Logistic Regression and scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) import scikitplot as skplt y_pred = logreg.predict(X_test_scaled) -skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True) +skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True) plt.show() y_probas = logreg.predict_proba(X_test_scaled) skplt.metrics.plot_roc(y_test, y_probas) diff --git a/doc/pub/week38/html/week38-solarized.html b/doc/pub/week38/html/week38-solarized.html index 81ca55ffa..b89fb8c97 100644 --- a/doc/pub/week38/html/week38-solarized.html +++ b/doc/pub/week38/html/week38-solarized.html @@ -144,7 +144,7 @@ MathJax.Hub.Config({

[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

-

Sep 18, 2020

+

Sep 19, 2020












@@ -428,21 +428,21 @@ polydegree = np.zeros(maxdegree) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2) for degree in range(maxdegree): - model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False)) + model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False)) y_pred = np.empty((y_test.shape[0], n_boostraps)) for i in range(n_boostraps): x_, y_ = resample(x_train, y_train) y_pred[:, i] = model.fit(x_, y_).predict(x_test).ravel() polydegree[degree] = degree - error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) ) - bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 ) - variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) ) - print('Polynomial degree:', degree) - print('Error:', error[degree]) - print('Bias^2:', bias[degree]) - print('Var:', variance[degree]) - print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree])) + error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) ) + bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 ) + variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) ) + print('Polynomial degree:', degree) + print('Error:', error[degree]) + print('Bias^2:', bias[degree]) + print('Var:', variance[degree]) + print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree])) plt.plot(polydegree, error, label='Error') plt.plot(polydegree, bias, label='bias') @@ -479,7 +479,7 @@ plt.show() training data. """ -print(__doc__) +print(__doc__) import numpy as np import matplotlib.pyplot as plt @@ -506,7 +506,7 @@ plt.figure(figsize=(14, False) + include_bias=False) linear_regression = LinearRegression() pipeline = Pipeline([("polynomial_features", polynomial_features), ("linear_regression", linear_regression)]) @@ -608,7 +608,7 @@ with their respective energies. import scipy.linalg as scl from sklearn.model_selection import train_test_split import tqdm -sns.set(color_codes=True) +sns.set(color_codes=True) cmap_args=dict(vmin=-1., vmax=1., cmap='seismic') L = 40 @@ -724,7 +724,7 @@ X_test_own = np.concatenate(

def ols_inv(x: np.ndarray, y: np.ndarray) -> np.ndarray:
-    return scl.inv(x.T @ x) @ (x.T @ y)
+    return scl.inv(x.T @ x) @ (x.T @ y)
 beta = ols_inv(X_train_own, y_train)
 

@@ -774,7 +774,7 @@ linear system as an equation would reduce this down to

def ols_svd(x: np.ndarray, y: np.ndarray) -> np.ndarray:
     u, s, v = scl.svd(x)
-    return v.T @ scl.pinv(scl.diagsvd(s, u.shape[0], v.shape[0])) @ u.T @ y
+    return v.T @ scl.pinv(scl.diagsvd(s, u.shape[0], v.shape[0])) @ u.T @ y
 

@@ -850,7 +850,7 @@ We will look at a system of \( L = 40 \) spins with a coupling constant of \( J from sklearn.model_selection import train_test_split import sklearn.linear_model as skl import tqdm -sns.set(color_codes=True) +sns.set(color_codes=True) cmap_args=dict(vmin=-1., vmax=1., cmap='seismic') L = 40 @@ -1133,7 +1133,7 @@ other models for all values of \( \lambda \).

Friday September 18: Intro to Logistic Regression

-Video of Lecture and link to handwritten notes. +Video of Lecture and link to handwritten notes.

@@ -1316,7 +1316,7 @@ DATA_ID = "DataFiles/" return os.path.join(DATA_ID, dat_id) def save_fig(fig_id): - plt.savefig(image_path(fig_id) + ".png", format='png') + plt.savefig(image_path(fig_id) + ".png", format='png') infile = open(data_path("chddata.csv"),'r') @@ -1427,7 +1427,7 @@ ax = fig.add_subplot(111) ax.plot(z, sigma) ax.set_ylim([-0.1, 1.1]) ax.set_xlim([-5,5]) -ax.grid(True) +ax.grid(True) ax.set_xlabel('z') ax.set_title('sigmoid function') @@ -1443,7 +1443,7 @@ ax = fig.add_subplot(111) ax.plot(z, step) ax.set_ylim([-0.5, 1.5]) ax.set_xlim([-5,5]) -ax.grid(True) +ax.grid(True) ax.set_xlabel('z') ax.set_title('step function') @@ -1458,7 +1458,7 @@ ax = fig.add_subplot(111) ax.plot(z, t) ax.set_ylim([-1.0, 1.0]) ax.set_xlim([-2*mt.pi,2*mt.pi]) -ax.grid(True) +ax.grid(True) ax.set_xlabel('z') ax.set_title('tanh function') @@ -1685,12 +1685,12 @@ classification. cancer = load_breast_cancer() X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0) -print(X_train.shape) -print(X_test.shape) +print(X_train.shape) +print(X_test.shape) # Logistic Regression logreg = LogisticRegression(solver='lbfgs') logreg.fit(X_train, y_train) -print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) +print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) #now scale the data from sklearn.preprocessing import StandardScaler scaler = StandardScaler() @@ -1699,7 +1699,7 @@ X_train_scaled = scaler.transform(X_train) X_test_scaled = scaler.transform(X_test) # Logistic Regression logreg.fit(X_train_scaled, y_train) -print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) +print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))











@@ -1744,7 +1744,7 @@ correlation_matrix = cancerpd.corr().round(1 # use the heatmap function from seaborn to plot the correlation matrix # annot = True to print the values inside the square plt.figure(figsize=(15,8)) -sns.heatmap(data=correlation_matrix, annot=True) +sns.heatmap(data=correlation_matrix, annot=True) plt.show()

@@ -1803,12 +1803,12 @@ applications. This will be discussed later this semester (0) -print(X_train.shape) -print(X_test.shape) +print(X_train.shape) +print(X_test.shape) # Logistic Regression logreg = LogisticRegression(solver='lbfgs') logreg.fit(X_train, y_train) -print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) +print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) #now scale the data from sklearn.preprocessing import StandardScaler scaler = StandardScaler() @@ -1817,20 +1817,20 @@ X_train_scaled = scaler.transform(X_train) X_test_scaled = scaler.transform(X_test) # Logistic Regression logreg.fit(X_train_scaled, y_train) -print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) +print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import cross_validate #Cross validation accuracy = cross_validate(logreg,X_test_scaled,y_test,cv=10)['test_score'] -print(accuracy) -print("Test set accuracy with Logistic Regression and scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) +print(accuracy) +print("Test set accuracy with Logistic Regression and scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) import scikitplot as skplt y_pred = logreg.predict(X_test_scaled) -skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True) +skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True) plt.show() y_probas = logreg.predict_proba(X_test_scaled) skplt.metrics.plot_roc(y_test, y_probas) diff --git a/doc/pub/week38/html/week38.html b/doc/pub/week38/html/week38.html index e3e6d2912..11e3f424d 100644 --- a/doc/pub/week38/html/week38.html +++ b/doc/pub/week38/html/week38.html @@ -149,7 +149,7 @@ MathJax.Hub.Config({

[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

-

Sep 18, 2020

+

Sep 19, 2020












@@ -433,21 +433,21 @@ polydegree = np x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2) for degree in range(maxdegree): - model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False)) + model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False)) y_pred = np.empty((y_test.shape[0], n_boostraps)) for i in range(n_boostraps): x_, y_ = resample(x_train, y_train) y_pred[:, i] = model.fit(x_, y_).predict(x_test).ravel() polydegree[degree] = degree - error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) ) - bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 ) - variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) ) - print('Polynomial degree:', degree) - print('Error:', error[degree]) - print('Bias^2:', bias[degree]) - print('Var:', variance[degree]) - print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree])) + error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) ) + bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 ) + variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) ) + print('Polynomial degree:', degree) + print('Error:', error[degree]) + print('Bias^2:', bias[degree]) + print('Var:', variance[degree]) + print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree])) plt.plot(polydegree, error, label='Error') plt.plot(polydegree, bias, label='bias') @@ -484,7 +484,7 @@ plt.show() training data. """ -print(__doc__) +print(__doc__) import numpy as np import matplotlib.pyplot as plt @@ -511,7 +511,7 @@ plt.figure(figsize.setp(ax, xticks=(), yticks=()) polynomial_features = PolynomialFeatures(degree=degrees[i], - include_bias=False) + include_bias=False) linear_regression = LinearRegression() pipeline = Pipeline([("polynomial_features", polynomial_features), ("linear_regression", linear_regression)]) @@ -530,7 +530,7 @@ plt.figure(figsize.xlim((0, 1)) plt.ylim((-2, 2)) plt.legend(loc="best") - plt.title("Degree {}\nMSE = {:.2e}(+/- {:.2e})".format( + plt.title("Degree {}\nMSE = {:.2e}(+/- {:.2e})".format( degrees[i], -scores.mean(), scores.std())) plt.show() @@ -613,7 +613,7 @@ with their respective energies. import scipy.linalg as scl from sklearn.model_selection import train_test_split import tqdm -sns.set(color_codes=True) +sns.set(color_codes=True) cmap_args=dict(vmin=-1., vmax=1., cmap='seismic') L = 40 @@ -729,7 +729,7 @@ X_test_own = np

def ols_inv(x: np.ndarray, y: np.ndarray) -> np.ndarray:
-    return scl.inv(x.T @ x) @ (x.T @ y)
+    return scl.inv(x.T @ x) @ (x.T @ y)
 beta = ols_inv(X_train_own, y_train)
 

@@ -779,7 +779,7 @@ linear system as an equation would reduce this down to

def ols_svd(x: np.ndarray, y: np.ndarray) -> np.ndarray:
     u, s, v = scl.svd(x)
-    return v.T @ scl.pinv(scl.diagsvd(s, u.shape[0], v.shape[0])) @ u.T @ y
+    return v.T @ scl.pinv(scl.diagsvd(s, u.shape[0], v.shape[0])) @ u.T @ y
 

@@ -855,7 +855,7 @@ We will look at a system of \( L = 40 \) spins with a coupling constant of \( J from sklearn.model_selection import train_test_split import sklearn.linear_model as skl import tqdm -sns.set(color_codes=True) +sns.set(color_codes=True) cmap_args=dict(vmin=-1., vmax=1., cmap='seismic') L = 40 @@ -1109,7 +1109,7 @@ colors = { lambdas, train_errors[key], colors[key], - label="Train {0}".format(key), + label="Train {0}".format(key), linewidth=4.0 ) @@ -1118,7 +1118,7 @@ colors = { lambdas, test_errors[key], colors[key] + "--", - label="Test {0}".format(key), + label="Test {0}".format(key), linewidth=4.0 ) plt.legend(loc="best", fontsize=18) @@ -1138,7 +1138,7 @@ other models for all values of \( \lambda \).

Friday September 18: Intro to Logistic Regression

-Video of Lecture and link to handwritten notes. +Video of Lecture and link to handwritten notes.

@@ -1321,7 +1321,7 @@ DATA_ID = " return os.path.join(DATA_ID, dat_id) def save_fig(fig_id): - plt.savefig(image_path(fig_id) + ".png", format='png') + plt.savefig(image_path(fig_id) + ".png", format='png') infile = open(data_path("chddata.csv"),'r') @@ -1432,7 +1432,7 @@ ax = fig..plot(z, sigma) ax.set_ylim([-0.1, 1.1]) ax.set_xlim([-5,5]) -ax.grid(True) +ax.grid(True) ax.set_xlabel('z') ax.set_title('sigmoid function') @@ -1448,7 +1448,7 @@ ax = fig..plot(z, step) ax.set_ylim([-0.5, 1.5]) ax.set_xlim([-5,5]) -ax.grid(True) +ax.grid(True) ax.set_xlabel('z') ax.set_title('step function') @@ -1463,7 +1463,7 @@ ax = fig..plot(z, t) ax.set_ylim([-1.0, 1.0]) ax.set_xlim([-2*mt.pi,2*mt.pi]) -ax.grid(True) +ax.grid(True) ax.set_xlabel('z') ax.set_title('tanh function') @@ -1690,12 +1690,12 @@ classification. cancer = load_breast_cancer() X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0) -print(X_train.shape) -print(X_test.shape) +print(X_train.shape) +print(X_test.shape) # Logistic Regression logreg = LogisticRegression(solver='lbfgs') logreg.fit(X_train, y_train) -print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) +print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) #now scale the data from sklearn.preprocessing import StandardScaler scaler = StandardScaler() @@ -1704,7 +1704,7 @@ X_train_scaled = scaler= scaler.transform(X_test) # Logistic Regression logreg.fit(X_train_scaled, y_train) -print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) +print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))











@@ -1749,7 +1749,7 @@ correlation_matrix = cancerpd# use the heatmap function from seaborn to plot the correlation matrix # annot = True to print the values inside the square plt.figure(figsize=(15,8)) -sns.heatmap(data=correlation_matrix, annot=True) +sns.heatmap(data=correlation_matrix, annot=True) plt.show()

@@ -1808,12 +1808,12 @@ applications. This will be discussed later this semester (= load_breast_cancer() X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0) -print(X_train.shape) -print(X_test.shape) +print(X_train.shape) +print(X_test.shape) # Logistic Regression logreg = LogisticRegression(solver='lbfgs') logreg.fit(X_train, y_train) -print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) +print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) #now scale the data from sklearn.preprocessing import StandardScaler scaler = StandardScaler() @@ -1822,20 +1822,20 @@ X_train_scaled = scaler= scaler.transform(X_test) # Logistic Regression logreg.fit(X_train_scaled, y_train) -print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) +print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import cross_validate #Cross validation accuracy = cross_validate(logreg,X_test_scaled,y_test,cv=10)['test_score'] -print(accuracy) -print("Test set accuracy with Logistic Regression and scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) +print(accuracy) +print("Test set accuracy with Logistic Regression and scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) import scikitplot as skplt y_pred = logreg.predict(X_test_scaled) -skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True) +skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True) plt.show() y_probas = logreg.predict_proba(X_test_scaled) skplt.metrics.plot_roc(y_test, y_probas) diff --git a/doc/pub/week38/ipynb/ipynb-week38-src.tar.gz b/doc/pub/week38/ipynb/ipynb-week38-src.tar.gz index be1c1f6c8d1dd46a0ec62aa04e16067ceb04fd5d..01254d1cae7336db632f04b45ecdacd4a551ebf6 100644 GIT binary patch literal 191 zcmV;w06_mAiwFRKXJ%gj1MSbv3c@f92k@Qu6nTQt+O6{{xPu2l#24sT=IXSaZHMmd z-3RDN@iIi{@A4-kgk+zs*4r#{cNfft5W?dahD>=fCRt4Nh|&ZoOBszhONnP-etUi+AETp4>%+CSM{AqT_9bSjig0yyh)dk$6h32MeQ2NU>V<++ zJAUkcCh}b5yPn$Ok2#Z;d!MpfwnOuD-0Z9i`tv^ft*l%2#bLEm_SJVG;a^Yrbe*63 zrC` \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: **Sep 18, 2020**\n", + "Date: **Sep 19, 2020**\n", "\n", "Copyright 1999-2020, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n", "\n", @@ -1436,7 +1436,7 @@ "\n", "## Friday September 18: Intro to Logistic Regression\n", "\n", - "[Video of Lecture](https://www.uio.no/studier/emner/matnat/fys/FYS-STK4155/h20/forelesningsvideoer/LectureSeptember18.mp4?vrtx=view-as-webpage) and [link to handwritten notes](https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/NotesSeptember18.pdf).\n", + "[Video of Lecture](https://www.uio.no/studier/emner/matnat/fys/FYS-STK3155/h20/forelesningsvideoer/LectureSeptember18.mp4?vrtx=view-as-webpage) and [link to handwritten notes](https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/NotesSeptember18.pdf).\n", "\n", "\n", "\n", @@ -2369,5 +2369,5 @@ ], "metadata": {}, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 }