From 427bbed49832a69f93fa9af52486965599bca2fb Mon Sep 17 00:00:00 2001 From: mhjensen Date: Sat, 17 Oct 2020 22:09:23 +0200 Subject: [PATCH] updating week 43 --- doc/pub/week43/html/._week43-bs000.html | 144 +++++---- doc/pub/week43/html/._week43-bs001.html | 224 +++++-------- doc/pub/week43/html/._week43-bs002.html | 159 +++++----- doc/pub/week43/html/._week43-bs003.html | 198 +++++------- doc/pub/week43/html/._week43-bs004.html | 259 ++++++++------- doc/pub/week43/html/._week43-bs005.html | 189 +++++------ doc/pub/week43/html/._week43-bs006.html | 224 +++++++------ doc/pub/week43/html/._week43-bs007.html | 212 ++++++++----- doc/pub/week43/html/._week43-bs008.html | 228 +++++++------ doc/pub/week43/html/._week43-bs009.html | 208 ++++++------ doc/pub/week43/html/._week43-bs010.html | 200 ++++++------ doc/pub/week43/html/._week43-bs011.html | 218 +++++++------ doc/pub/week43/html/._week43-bs012.html | 317 ++++++------------- doc/pub/week43/html/._week43-bs013.html | 198 +++++++----- doc/pub/week43/html/._week43-bs014.html | 176 +++++----- doc/pub/week43/html/._week43-bs015.html | 315 +++++++++++++----- doc/pub/week43/html/._week43-bs016.html | 203 ++++++------ doc/pub/week43/html/._week43-bs017.html | 159 ++++++---- doc/pub/week43/html/._week43-bs018.html | 213 ++++++------- doc/pub/week43/html/._week43-bs019.html | 212 +++++++------ doc/pub/week43/html/._week43-bs020.html | 180 +++++------ doc/pub/week43/html/._week43-bs021.html | 198 +++++++----- doc/pub/week43/html/._week43-bs022.html | 175 +++++----- doc/pub/week43/html/._week43-bs023.html | 183 ++++++----- doc/pub/week43/html/._week43-bs024.html | 179 ++++++----- doc/pub/week43/html/._week43-bs025.html | 153 ++++----- doc/pub/week43/html/._week43-bs026.html | 159 +++++----- doc/pub/week43/html/week43-bs.html | 144 +++++---- doc/pub/week43/html/week43-reveal.html | 148 +++++---- doc/pub/week43/html/week43-solarized.html | 218 +++++++------ doc/pub/week43/html/week43.html | 218 +++++++------ doc/pub/week43/ipynb/ipynb-week43-src.tar.gz | Bin 196 -> 191 bytes doc/pub/week43/ipynb/week43.ipynb | 20 +- doc/src/week43/week43.do.txt | 16 +- 34 files changed, 3221 insertions(+), 3026 deletions(-) diff --git a/doc/pub/week43/html/._week43-bs000.html b/doc/pub/week43/html/._week43-bs000.html index f460349c9..2ced98fa9 100644 --- a/doc/pub/week43/html/._week43-bs000.html +++ b/doc/pub/week43/html/._week43-bs000.html @@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source - + -Week 43: Dimensionality Reduction +Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods @@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source @@ -128,7 +130,7 @@ MathJax.Hub.Config({ - Week 43: Dimensionality Reduction + Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods

We expand this model to the Franke function discussed above. @@ -528,7 +550,7 @@ We expand this model to the Franke function discussed above.

-

Correlation Matrix with Pandas and the Franke function

+

Correlation Matrix with Pandas and the Franke function

@@ -575,7 +597,7 @@ Xpd = pd.DataFrame(X) # subtract the mean values and set up the covariance matrix Xpd = Xpd - Xpd.mean() covariance_matrix = Xpd.cov() -print(covariance_matrix) +print(covariance_matrix)

We note here that the covariance is zero for the first rows and @@ -591,7 +613,7 @@ matrix without these elements.

-

Rewriting the Covariance and/or Correlation Matrix

+

Rewriting the Covariance and/or Correlation Matrix

We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \( \boldsymbol{X} \) as @@ -644,7 +666,7 @@ It is easy to generalize this to a matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\t

-

Towards the PCA theorem

+

Towards the PCA theorem

We have that the covariance matrix (the correlation matrix involves a simple rescaling) is given as @@ -703,7 +725,7 @@ features/predictors.

-

The Algorithm before theorem

+

The Algorithm before theorem

Here's how we would proceed in setting up the algorithm for the PCA, see also discussion below here. @@ -736,7 +758,7 @@ $$

-

Writing our own PCA code

+

Writing our own PCA code

We will use a simple example first with two-dimensional data @@ -771,7 +793,7 @@ X = np.random.multivariate_normal(mean, cov, n)

Now we are going to implement the PCA algorithm. We will break it down into various substeps. -

Compute the sample mean and center the data

+

Compute the sample mean and center the data

The first step of PCA is to compute the sample mean of the data and use it to center the data. Recall that the sample mean is @@ -812,7 +834,7 @@ variance. The diagonal covariance matrix elements will then be one, while the non-diagonal ones need to be divided by \( 2\sqrt{2} \) for our specific case. -

Compute the sample covariance

+

Compute the sample covariance

Now we are going to use the mean centered data to compute the sample covariance of the data by using the following equation @@ -829,8 +851,8 @@ We can write our own code or simply use either the functionaly of numpy o

-

print(df.cov())
-print(np.cov(X_centered.T))
+
print(df.cov())
+print(np.cov(X_centered.T))
 

Note that the way we define the covariance matrix here has a factor \( n-1 \) instead of \( n \). This is included in the cov() function by numpy and pandas. @@ -846,8 +868,8 @@ Cov[0,1] Cov[0,0] = np.sum(x.T@x)/(n-1.0) Cov[1,1] = np.sum(y.T@y)/(n-1.0) Cov[1,0]= Cov[0,1] -print("Centered covariance using own code") -print(Cov) +print("Centered covariance using own code") +print(Cov) plt.plot(x, y, 'x') plt.axis('equal') plt.show() @@ -856,7 +878,7 @@ plt.show() Depending on the number of points \( n \), we will get results that are close to the covariance values defined above. The plot shows how the data are clustered around a line with slope close to one. Is this expected? -

Diagonalize the sample covariance matrix to obtain the principal components

+

Diagonalize the sample covariance matrix to obtain the principal components

Now we are ready to solve for the principal components! To do so we @@ -899,21 +921,21 @@ EigValues, EigVectors = np.linalg.eig(Cov) #permute = EigValues.argsort() #EigValues = EigValues[permute] #EigVectors = EigVectors[:,permute] -print("Eigenvalues of Covariance matrix") +print("Eigenvalues of Covariance matrix") for i in range(2): - print(EigValues[i]) + print(EigValues[i]) FirstEigvector = EigVectors[:,0] SecondEigvector = EigVectors[:,1] -print("First eigenvector") -print(FirstEigvector) -print("Second eigenvector") -print(SecondEigvector) +print("First eigenvector") +print(FirstEigvector) +print("Second eigenvector") +print(SecondEigvector) #thereafter we do a PCA with Scikit-learn from sklearn.decomposition import PCA pca = PCA(n_components = 2) X2Dsl = pca.fit_transform(X) -print("Eigenvector of largest eigenvalue") -print(pca.components_.T[:, 0]) +print("Eigenvector of largest eigenvalue") +print(pca.components_.T[:, 0])

This code does not contain all the above elements, but it shows how we can use Scikit-Learn to extract the eigenvector which corresponds to the largest eigenvalue. Try to address the questions we pose before the above code. Try also to change the values of the covariance matrix by making one of the diagonal elements much larger than the other. What do you observe then? @@ -921,7 +943,7 @@ This code does not contain all the above elements, but it shows how we can use <

-

Classical PCA Theorem

+

Classical PCA Theorem

We assume now that we have a design matrix \( \boldsymbol{X} \) which has been @@ -956,7 +978,7 @@ The proof which follows will be updated by mid January 2020.

-

Proof of the PCA Theorem

+

Proof of the PCA Theorem

To show the PCA theorem let us start with the assumption that there is one vector \( \boldsymbol{w}_0 \) which corresponds to a solution which minimized the reconstruction error \( J \). This is an orthogonal vector. It means that we now approximate the reconstruction error in terms of \( \boldsymbol{w}_0 \) and \( \boldsymbol{z}_0 \) as @@ -985,7 +1007,7 @@ where the vectors on the rhs are known.

-

PCA Proof continued

+

PCA Proof continued

We have now found the unknown parameters \( z_{i0} \). These correspond to the projected coordinates and we can write @@ -1036,7 +1058,7 @@ of the projected data.

-

The final step

+

The final step

We could trivially maximize the variance of the projection (and @@ -1098,7 +1120,7 @@ Additional part of the proof for the other eigenvectors will be added by mid Jan

-

Geometric Interpretation and link with Singular Value Decomposition

+

Geometric Interpretation and link with Singular Value Decomposition

This material will be added by mid January 2020. @@ -1106,7 +1128,7 @@ This material will be added by mid January 2020.

-

Principal Component Analysis

+

Principal Component Analysis

Principal Component Analysis (PCA) is by far the most popular dimensionality reduction algorithm. @@ -1134,14 +1156,14 @@ display(df) # we center it ourselves X_centered = X - X.mean(axis=0) # Then check the difference between pandas and our own set up -print(X_centered-df) +print(X_centered-df) #Now we do an SVD U, s, V = np.linalg.svd(X_centered) c1 = V.T[:, 0] c2 = V.T[:, 1] W2 = V.T[:, :2] X2D = X_centered.dot(W2) -print(X2D) +print(X2D)

PCA assumes that the dataset is centered around the origin. Scikit-Learn’s PCA classes take care of centering @@ -1162,7 +1184,7 @@ X2D = X_centered.dot(W2)

-

PCA and scikit-learn

+

PCA and scikit-learn

Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The @@ -1175,7 +1197,7 @@ that it automatically takes care of centering the data): from sklearn.decomposition import PCA pca = PCA(n_components = 2) X2D = pca.fit_transform(X) -print(X2D) +print(X2D)

After fitting the PCA transformer to the dataset, you can access the principal components using the @@ -1194,7 +1216,7 @@ variance that lies along the axis of each principal component.

-

Back to the Cancer Data

+

Back to the Cancer Data

We can now repeat the above but applied to real data, in this case our breast cancer data. Here we compute performance scores on the training data using logistic regression.

@@ -1211,7 +1233,7 @@ X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,ra logreg = LogisticRegression() logreg.fit(X_train, y_train) -print("Train set accuracy from Logistic Regression: {:.2f}".format(logreg.score(X_train,y_train))) +print("Train set accuracy from Logistic Regression: {:.2f}".format(logreg.score(X_train,y_train))) # We scale the data from sklearn.preprocessing import StandardScaler scaler = StandardScaler() @@ -1220,14 +1242,14 @@ X_train_scaled = scaler.transform(X_train) X_test_scaled = scaler.transform(X_test) # Then perform again a log reg fit logreg.fit(X_train_scaled, y_train) -print("Train set accuracy scaled data: {:.2f}".format(logreg.score(X_train_scaled,y_train))) +print("Train set accuracy scaled data: {:.2f}".format(logreg.score(X_train_scaled,y_train))) #thereafter we do a PCA with Scikit-learn from sklearn.decomposition import PCA pca = PCA(n_components = 2) X2D_train = pca.fit_transform(X_train_scaled) # and finally compute the log reg fit and the score on the training data logreg.fit(X2D_train,y_train) -print("Train set accuracy scaled and PCA data: {:.2f}".format(logreg.score(X2D_train,y_train))) +print("Train set accuracy scaled and PCA data: {:.2f}".format(logreg.score(X2D_train,y_train)))

We see that our training data after the PCA decomposition has a performance similar to the non-scaled data. @@ -1235,7 +1257,7 @@ We see that our training data after the PCA decomposition has a performance simi

-

More on the PCA

+

More on the PCA

Instead of arbitrarily choosing the number of dimensions to reduce down to, it is generally preferable to @@ -1266,7 +1288,7 @@ X_reduced = pca.fit_transform(X)

-

Incremental PCA

+

Incremental PCA

One problem with the preceding implementation of PCA is that it requires the whole training set to fit in @@ -1278,7 +1300,7 @@ instances arrive).

-

Randomized PCA

+

Randomized PCA

Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic @@ -1289,7 +1311,7 @@ previous algorithms when \( d \) is much smaller than \( n \).

-

Kernel PCA

+

Kernel PCA

@@ -1315,7 +1337,7 @@ X_reduced = rbf_pca.fit_transform(X)

-

LLE

+

LLE

Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction @@ -1327,7 +1349,7 @@ these local relationships are best preserved (more details shortly).

-

Other techniques

+

Other techniques

There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn. diff --git a/doc/pub/week43/html/week43-solarized.html b/doc/pub/week43/html/week43-solarized.html index 2b2e33b7e..cb20c384f 100644 --- a/doc/pub/week43/html/week43-solarized.html +++ b/doc/pub/week43/html/week43-solarized.html @@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source - + -Week 43: Dimensionality Reduction +Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods @@ -61,64 +61,66 @@ div { text-align: justify; text-justify: inter-word; } @@ -144,7 +146,7 @@ MathJax.Hub.Config({ -

Week 43: Dimensionality Reduction

+

Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods

@@ -160,12 +162,32 @@ MathJax.Hub.Config({

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

-

Sep 16, 2020

+

Oct 17, 2020












-

Why should we think of reducing the dimensionality

+
    +
  • Thursday: Wrapping up Recurrent Neural Networks and solving differential equations.
  • +
  • Friday: Principal Component Analysis and Dimensionality Reduction
  • +
+ +Reading suggestions for both days: "Aurelien Geron's chapters 8 + +

+









+ +

Recurrent Neural Networks

+ +

+









+ +

Solving ODEs with Deep Learning

+ +

+









+ +

Why should we think of reducing the dimensionality

In addition to the plot of the features, we study now also the covariance (and the correlation matrix). @@ -204,12 +226,12 @@ plt.show() 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 -sns.heatmap(data=correlation_matrix, annot=True) +sns.heatmap(data=correlation_matrix, annot=True) plt.show() #print eigvalues of correlation matrix EigValues, EigVectors = np.linalg.eig(correlation_matrix) -print(EigValues) +print(EigValues)

In the above example we note two things. In the first plot we display @@ -249,7 +271,7 @@ applications.











-

Basic ideas of the Principal Component Analysis (PCA)

+

Basic ideas of the Principal Component Analysis (PCA)

The principal component analysis deals with the problem of fitting a @@ -271,7 +293,7 @@ We have a data set defined by a design/feature matrix \( \boldsymbol{X} \) (see









-

Introducing the Covariance and Correlation functions

+

Introducing the Covariance and Correlation functions

Before we discuss the PCA theorem, we need to remind ourselves about @@ -332,7 +354,7 @@ In the above example this is the function we constructed using pandas.











-

Correlation Function and Design/Feature Matrix

+

Correlation Function and Design/Feature Matrix

In our derivation of the various regression algorithms like Ordinary Least Squares or Ridge regression @@ -393,7 +415,7 @@ $$











-

Covariance Matrix Examples

+

Covariance Matrix Examples

The Numpy function np.cov calculates the covariance elements using @@ -426,17 +448,17 @@ covariance matrix through the np.linalg.eig() function. import numpy as np n = 100 x = np.random.normal(size=n) -print(np.mean(x)) +print(np.mean(x)) y = 4+3*x+np.random.normal(size=n) -print(np.mean(y)) +print(np.mean(y)) W = np.vstack((x, y)) C = np.cov(W) -print(C) +print(C)











-

Correlation Matrix

+

Correlation Matrix

The previous example can be converted into the correlation matrix by @@ -458,8 +480,8 @@ x = x - np.mean(x) y = y - np.mean(y) variance_x = np.sum(x@x)/n variance_y = np.sum(y@y)/n -print(variance_x) -print(variance_y) +print(variance_x) +print(variance_y) cov_xy = np.sum(x@y)/n cov_xx = np.sum(x@x)/n cov_yy = np.sum(y@y)/n @@ -468,7 +490,7 @@ C[0,0]= C[1,1]= cov_yy/variance_y C[0,1]= cov_xy/np.sqrt(variance_y*variance_x) C[1,0]= C[0,1] -print(C) +print(C)

We see that the matrix elements along the diagonal are one as they @@ -481,7 +503,7 @@ The above procedure with numpy can be made more compact if we use pand











-

Correlation Matrix with Pandas

+

Correlation Matrix with Pandas

We whow here how we can set up the correlation matrix using pandas, as done in this simple code @@ -496,11 +518,11 @@ x = x - np.mean(x) y = 4+3*x+np.random.normal(size=n) y = y - np.mean(y) X = (np.vstack((x, y))).T -print(X) +print(X) Xpd = pd.DataFrame(X) -print(Xpd) +print(Xpd) correlation_matrix = Xpd.corr() -print(correlation_matrix) +print(correlation_matrix)

We expand this model to the Franke function discussed above. @@ -508,7 +530,7 @@ We expand this model to the Franke function discussed above.











-

Correlation Matrix with Pandas and the Franke function

+

Correlation Matrix with Pandas and the Franke function

@@ -555,7 +577,7 @@ Xpd = pd.DataFrame(X) # subtract the mean values and set up the covariance matrix Xpd = Xpd - Xpd.mean() covariance_matrix = Xpd.cov() -print(covariance_matrix) +print(covariance_matrix)

We note here that the covariance is zero for the first rows and @@ -571,7 +593,7 @@ matrix without these elements.











-

Rewriting the Covariance and/or Correlation Matrix

+

Rewriting the Covariance and/or Correlation Matrix

We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \( \boldsymbol{X} \) as @@ -614,7 +636,7 @@ It is easy to generalize this to a matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\t











-

Towards the PCA theorem

+

Towards the PCA theorem

We have that the covariance matrix (the correlation matrix involves a simple rescaling) is given as @@ -665,7 +687,7 @@ features/predictors.











-

The Algorithm before theorem

+

The Algorithm before theorem

Here's how we would proceed in setting up the algorithm for the PCA, see also discussion below here. @@ -696,7 +718,7 @@ $$









-

Writing our own PCA code

+

Writing our own PCA code

We will use a simple example first with two-dimensional data @@ -729,7 +751,7 @@ X = np.random.multivariate_normal(mean, cov, n)

Now we are going to implement the PCA algorithm. We will break it down into various substeps. -

Compute the sample mean and center the data

+

Compute the sample mean and center the data

The first step of PCA is to compute the sample mean of the data and use it to center the data. Recall that the sample mean is @@ -766,7 +788,7 @@ variance. The diagonal covariance matrix elements will then be one, while the non-diagonal ones need to be divided by \( 2\sqrt{2} \) for our specific case. -

Compute the sample covariance

+

Compute the sample covariance

Now we are going to use the mean centered data to compute the sample covariance of the data by using the following equation @@ -781,8 +803,8 @@ We can write our own code or simply use either the functionaly of numpy o

-

print(df.cov())
-print(np.cov(X_centered.T))
+
print(df.cov())
+print(np.cov(X_centered.T))
 

Note that the way we define the covariance matrix here has a factor \( n-1 \) instead of \( n \). This is included in the cov() function by numpy and pandas. @@ -798,8 +820,8 @@ Cov[0,1] Cov[0,0] = np.sum(x.T@x)/(n-1.0) Cov[1,1] = np.sum(y.T@y)/(n-1.0) Cov[1,0]= Cov[0,1] -print("Centered covariance using own code") -print(Cov) +print("Centered covariance using own code") +print(Cov) plt.plot(x, y, 'x') plt.axis('equal') plt.show() @@ -808,7 +830,7 @@ plt.show() Depending on the number of points \( n \), we will get results that are close to the covariance values defined above. The plot shows how the data are clustered around a line with slope close to one. Is this expected? -

Diagonalize the sample covariance matrix to obtain the principal components

+

Diagonalize the sample covariance matrix to obtain the principal components

Now we are ready to solve for the principal components! To do so we @@ -850,21 +872,21 @@ EigValues, EigVectors = np.linalg.eig(Cov) #permute = EigValues.argsort() #EigValues = EigValues[permute] #EigVectors = EigVectors[:,permute] -print("Eigenvalues of Covariance matrix") +print("Eigenvalues of Covariance matrix") for i in range(2): - print(EigValues[i]) + print(EigValues[i]) FirstEigvector = EigVectors[:,0] SecondEigvector = EigVectors[:,1] -print("First eigenvector") -print(FirstEigvector) -print("Second eigenvector") -print(SecondEigvector) +print("First eigenvector") +print(FirstEigvector) +print("Second eigenvector") +print(SecondEigvector) #thereafter we do a PCA with Scikit-learn from sklearn.decomposition import PCA pca = PCA(n_components = 2) X2Dsl = pca.fit_transform(X) -print("Eigenvector of largest eigenvalue") -print(pca.components_.T[:, 0]) +print("Eigenvector of largest eigenvalue") +print(pca.components_.T[:, 0])

This code does not contain all the above elements, but it shows how we can use Scikit-Learn to extract the eigenvector which corresponds to the largest eigenvalue. Try to address the questions we pose before the above code. Try also to change the values of the covariance matrix by making one of the diagonal elements much larger than the other. What do you observe then? @@ -872,7 +894,7 @@ This code does not contain all the above elements, but it shows how we can use <











-

Classical PCA Theorem

+

Classical PCA Theorem

We assume now that we have a design matrix \( \boldsymbol{X} \) which has been @@ -905,7 +927,7 @@ The proof which follows will be updated by mid January 2020.











-

Proof of the PCA Theorem

+

Proof of the PCA Theorem

To show the PCA theorem let us start with the assumption that there is one vector \( \boldsymbol{w}_0 \) which corresponds to a solution which minimized the reconstruction error \( J \). This is an orthogonal vector. It means that we now approximate the reconstruction error in terms of \( \boldsymbol{w}_0 \) and \( \boldsymbol{z}_0 \) as @@ -928,7 +950,7 @@ where the vectors on the rhs are known.











-

PCA Proof continued

+

PCA Proof continued

We have now found the unknown parameters \( z_{i0} \). These correspond to the projected coordinates and we can write @@ -969,7 +991,7 @@ of the projected data.











-

The final step

+

The final step

We could trivially maximize the variance of the projection (and @@ -1023,7 +1045,7 @@ Additional part of the proof for the other eigenvectors will be added by mid Jan











-

Geometric Interpretation and link with Singular Value Decomposition

+

Geometric Interpretation and link with Singular Value Decomposition

This material will be added by mid January 2020. @@ -1031,7 +1053,7 @@ This material will be added by mid January 2020.











-

Principal Component Analysis

+

Principal Component Analysis

Principal Component Analysis (PCA) is by far the most popular dimensionality reduction algorithm. @@ -1059,14 +1081,14 @@ display(df) # we center it ourselves X_centered = X - X.mean(axis=0) # Then check the difference between pandas and our own set up -print(X_centered-df) +print(X_centered-df) #Now we do an SVD U, s, V = np.linalg.svd(X_centered) c1 = V.T[:, 0] c2 = V.T[:, 1] W2 = V.T[:, :2] X2D = X_centered.dot(W2) -print(X2D) +print(X2D)

PCA assumes that the dataset is centered around the origin. Scikit-Learn’s PCA classes take care of centering @@ -1086,7 +1108,7 @@ X2D = X_centered.dot(W2)

-

PCA and scikit-learn

+

PCA and scikit-learn

Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The @@ -1099,7 +1121,7 @@ that it automatically takes care of centering the data): from sklearn.decomposition import PCA pca = PCA(n_components = 2) X2D = pca.fit_transform(X) -print(X2D) +print(X2D)

After fitting the PCA transformer to the dataset, you can access the principal components using the @@ -1118,7 +1140,7 @@ variance that lies along the axis of each principal component.











-

Back to the Cancer Data

+

Back to the Cancer Data

We can now repeat the above but applied to real data, in this case our breast cancer data. Here we compute performance scores on the training data using logistic regression.

@@ -1135,7 +1157,7 @@ X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,ra logreg = LogisticRegression() logreg.fit(X_train, y_train) -print("Train set accuracy from Logistic Regression: {:.2f}".format(logreg.score(X_train,y_train))) +print("Train set accuracy from Logistic Regression: {:.2f}".format(logreg.score(X_train,y_train))) # We scale the data from sklearn.preprocessing import StandardScaler scaler = StandardScaler() @@ -1144,14 +1166,14 @@ X_train_scaled = scaler.transform(X_train) X_test_scaled = scaler.transform(X_test) # Then perform again a log reg fit logreg.fit(X_train_scaled, y_train) -print("Train set accuracy scaled data: {:.2f}".format(logreg.score(X_train_scaled,y_train))) +print("Train set accuracy scaled data: {:.2f}".format(logreg.score(X_train_scaled,y_train))) #thereafter we do a PCA with Scikit-learn from sklearn.decomposition import PCA pca = PCA(n_components = 2) X2D_train = pca.fit_transform(X_train_scaled) # and finally compute the log reg fit and the score on the training data logreg.fit(X2D_train,y_train) -print("Train set accuracy scaled and PCA data: {:.2f}".format(logreg.score(X2D_train,y_train))) +print("Train set accuracy scaled and PCA data: {:.2f}".format(logreg.score(X2D_train,y_train)))

We see that our training data after the PCA decomposition has a performance similar to the non-scaled data. @@ -1159,7 +1181,7 @@ We see that our training data after the PCA decomposition has a performance simi











-

More on the PCA

+

More on the PCA

Instead of arbitrarily choosing the number of dimensions to reduce down to, it is generally preferable to @@ -1189,7 +1211,7 @@ X_reduced = pca.fit_transform(X)











-

Incremental PCA

+

Incremental PCA

One problem with the preceding implementation of PCA is that it requires the whole training set to fit in @@ -1201,7 +1223,7 @@ instances arrive).











-

Randomized PCA

+

Randomized PCA

Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic @@ -1212,7 +1234,7 @@ previous algorithms when \( d \) is much smaller than \( n \).











-

Kernel PCA

+

Kernel PCA

@@ -1241,7 +1263,7 @@ X_reduced = rbf_pca.fit_transform(X)











-

LLE

+

LLE

Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction @@ -1253,7 +1275,7 @@ these local relationships are best preserved (more details shortly).











-

Other techniques

+

Other techniques

There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn. diff --git a/doc/pub/week43/html/week43.html b/doc/pub/week43/html/week43.html index 96f11921e..bb3c39260 100644 --- a/doc/pub/week43/html/week43.html +++ b/doc/pub/week43/html/week43.html @@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source - + -Week 43: Dimensionality Reduction +Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods