diff --git a/doc/LectureNotes/schedule.md b/doc/LectureNotes/schedule.md index e65587b6a..5f08e2068 100644 --- a/doc/LectureNotes/schedule.md +++ b/doc/LectureNotes/schedule.md @@ -134,6 +134,7 @@ For the reading assignments we use the following abbreviations: ### Week 43 October 25-29 - Lab Wednesday: Work on project 2 - Lecture Thursday: Recurrent Neural Networks + - Video of Lecture at https://www.uio.no/studier/emner/matnat/fys/FYS-STK3155/h21/forelesningsvideoer/LectureOctober28.mp4?vrtx=view-as-webpage - Lecture Friday: Recurrent Neural Networks and principal component analysis (PCA) - Reading recommendations: - See lecture notes for week 43 at https://compphysics.github.io/MachineLearning/doc/web/course.html. diff --git a/doc/pub/week43/html/week43-bs.html b/doc/pub/week43/html/week43-bs.html index c9b21698a..910521043 100644 --- a/doc/pub/week43/html/week43-bs.html +++ b/doc/pub/week43/html/week43-bs.html @@ -97,19 +97,48 @@ doconce format html week43.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'introducing-the-covariance-and-correlation-functions'), - ('Correlation Matrix', 2, None, 'correlation-matrix'), + ('More on the covariance', 2, None, 'more-on-the-covariance'), + ('Reminding ourselves about Linear Regression', + 2, + None, + 'reminding-ourselves-about-linear-regression'), + ('Simple Example', 2, None, 'simple-example'), + ('The Correlation Matrix', 2, None, 'the-correlation-matrix'), + ('Numpy Functionality', 2, None, 'numpy-functionality'), + ('Correlation Matrix again', 2, None, 'correlation-matrix-again'), + ('Using Pandas', 2, None, 'using-pandas'), + ('And then the Franke Function', + 2, + None, + 'and-then-the-franke-function'), + ('Lnks with the Design Matrix', + 2, + None, + 'lnks-with-the-design-matrix'), + ('Computing the Expectation Values', + 2, + None, + 'computing-the-expectation-values'), ('Towards the PCA theorem', 2, None, 'towards-the-pca-theorem'), + ('More on the PCA Theorem', 2, None, 'more-on-the-pca-theorem'), ('The Algorithm before the Theorem', - 3, + 2, None, 'the-algorithm-before-the-theorem'), - ('Writing our own PCA code', 3, None, 'writing-our-own-pca-code'), + ('Writing our own PCA code', 2, None, 'writing-our-own-pca-code'), + ('Implementing it', 2, None, 'implementing-it'), + ('First Step', 2, None, 'first-step'), + ('Scaling', 2, None, 'scaling'), + ('Centered Data', 2, None, 'centered-data'), + ('Exploring', 2, None, 'exploring'), ('Diagonalize the sample covariance matrix to obtain the ' 'principal components', - 3, + 2, None, 'diagonalize-the-sample-covariance-matrix-to-obtain-the-principal-components'), + ('Collecting all Steps', 2, None, 'collecting-all-steps'), ('Classical PCA Theorem', 2, None, 'classical-pca-theorem'), + ('The PCA Theorem', 2, None, 'the-pca-theorem'), ('Geometric Interpretation and link with Singular Value ' 'Decomposition', 2, @@ -117,7 +146,7 @@ doconce format html week43.do.txt --html_style=bootstrap --pygments_html_style=d 'geometric-interpretation-and-link-with-singular-value-decomposition'), ('PCA and scikit-learn', 2, None, 'pca-and-scikit-learn'), ('Back to the Cancer Data', 2, None, 'back-to-the-cancer-data'), - ('Incremental PCA', 3, None, 'incremental-pca'), + ('Incremental PCA', 2, None, 'incremental-pca'), ('Randomized PCA', 3, None, 'randomized-pca'), ('Kernel PCA', 3, None, 'kernel-pca'), ('Other techniques', 2, None, 'other-techniques')]} @@ -183,16 +212,33 @@ MathJax.Hub.Config({
The covariance takes values between zero and infinity and may thus lead to problems with loss of numerical precision for particularly large values. It is common to scale the covariance matrix by @@ -2254,6 +2303,8 @@ $$
In the above example this is the function we constructed using pandas.
+ +In our derivation of the various regression algorithms like Ordinary Least Squares or Ridge regression we defined the design/feature matrix \( \boldsymbol{X} \) as
@@ -2282,8 +2333,11 @@ $$ \boldsymbol{x}_i^T = \begin{bmatrix}x_{0,i} & x_{1,i} & x_{2,i}& \dots & \dots x_{n-1,i}\end{bmatrix}. $$ + + +With these definitions, we can now rewrite our \( 2\times 2 \) -correaltion/covariance matrix in terms of a moe general design/feature +correlation/covariance matrix in terms of a moe general design/feature matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \). This leads to a \( p\times p \) covariance matrix for the vectors \( \boldsymbol{x}_i \) with \( i=0,1,\dots,p-1 \)
@@ -2299,6 +2353,10 @@ $$ \end{bmatrix}, $$ + + +and the correlation matrix
$$ \boldsymbol{K}[\boldsymbol{x}] = \begin{bmatrix} @@ -2311,6 +2369,10 @@ $$ \end{bmatrix}, $$ + + +The Numpy function np.cov calculates the covariance elements using the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have the exact mean values. The following simple function uses the @@ -2369,7 +2431,7 @@ C = np.c -
The previous example can be converted into the correlation matrix by simply scaling the matrix elements with the variances. We should also @@ -2428,6 +2490,9 @@ this matrix we easily see that it is a positive definite matrix.
The above procedure with numpy can be made more compact if we use pandas.
+ +We whow here how we can set up the correlation matrix using pandas, as done in this simple code
@@ -2464,6 +2529,10 @@ correlation_matrix = XpdAnd then the Franke Function +We expand this model to the Franke function discussed above.
@@ -2536,9 +2605,12 @@ columns since all matrix elements in the design matrix were set to one (we are fitting the function in terms of a polynomial of degree \( n \)). We would however not include the intercept and wee can simply drop these elements and construct a correlation -matrix without them. +matrix without them by centering our matrix elements by subtracting the mean of each column. + +We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \( \boldsymbol{X} \) as
$$ \boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}= \mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}]. @@ -2554,6 +2626,10 @@ x_{10} & x_{11}\\ \end{bmatrix}. $$ + + +If we then compute the expectation value
$$ \mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}=\begin{bmatrix} @@ -2603,6 +2679,10 @@ $$ \boldsymbol{S}_i\lambda_i = \boldsymbol{C}[\boldsymbol{x}]\boldsymbol{S}_i. $$ + + +In the derivation of the PCA theorem we will assume that the eigenvalues are ordered in descending order, that is \( \lambda_0 > \lambda_1 > \dots > \lambda_{p-1} \).
@@ -2619,7 +2699,9 @@ dimensions where the eigenvalues are very small. If \( p \) is very large, we could then aim at reducing \( p \) to \( l < < p \) and handle only \( l \) features/predictors. -Here's how we would proceed in setting up the algorithm for the PCA, see also discussion below here.
We will use a simple example first with two-dimensional data drawn from a multivariate normal distribution with the following mean and covariance matrix (we have fixed these quantities but will play around with them below): @@ -2659,6 +2742,8 @@ We will generate \( n = 10000 \) points \( X = \{ x_1, \ldots, x_N \} \) from this distribution, and store them in the \( 1000 \times 2 \) matrix \( \boldsymbol{X} \). This is our design matrix where we have forced the covariance and mean values to take specific values.
+ +The following Python code aids in setting up the data and writing out the design matrix. Note that the function multivariate returns also the covariance discussed above and that it is defined by dividing by \( n-1 \) instead of \( n \).
@@ -2694,6 +2779,9 @@ X = np.rNow we are going to implement the PCA algorithm. We will break it down into various substeps.
+ +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
$$ \mu_n = \frac{1}{n} \sum_{i=1}^n x_i @@ -2736,6 +2824,9 @@ X_centered = X + + +Alternatively, we could use the functions we discussed earlier for scaling the data set. That is, we could have used the StandardScaler function in Scikit-Learn, a function which ensures @@ -2747,6 +2838,9 @@ while the non-diagonal ones need to be divided by \( 2\sqrt{2} \) for our specific case.
+ +Now we are going to use the mean centered data to compute the sample covariance of the data by using the following equation
$$ \begin{equation*} @@ -2819,10 +2913,16 @@ 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? Try to change the covariance and the mean values. For example, try to make the variance of the first element much larger than that of the second diagonal element. Try also to shrink the covariance (the non-diagonal elements) and see how the data points are distributed.
-Now we are ready to solve for the principal components! To do so we diagonalize the sample covariance matrix \( \Sigma \). We can use the @@ -2845,6 +2945,9 @@ $$
where \( v_0 \) is the first principal component.
+ +Collecting all these steps we can write our own PCA function and compare this with the functionality included in Scikit-Learn.
@@ -2898,6 +3001,8 @@ X2Dsl = pca.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?
+ +We assume now that we have a design matrix \( \boldsymbol{X} \) which has been @@ -2916,6 +3021,9 @@ orthogonal projection of the data onto the columns spanned by the eigenvectors of the covariance(correlations matrix).
+ +To show the PCA theorem let us start with the assumption that there is one vector \( \boldsymbol{s}_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
We are almost there, we have obtained a relation between minimizing @@ -2971,6 +3079,8 @@ chapter 12.4 and discussion therein.
For more details, see for example Vidal, Ma and Sastry, chapter 2.
+ +For a detailed demonstration of the geometric interpretation, see Vidal, Ma and Sastry, section 2.1.2.
@@ -3060,6 +3170,9 @@ X2D = X_centered + + +Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The
@@ -3242,7 +3355,10 @@ X_reduced = pca
- One problem with the preceding implementation of PCA is that it requires the whole training set to fit in
memory in order for the SVD algorithm to run. Fortunately, Incremental PCA (IPCA) algorithms have
@@ -3293,6 +3409,9 @@ X_reduced = rbf_pcaOther techniques
There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn. The covariance takes values between zero and infinity and may thus
lead to problems with loss of numerical precision for particularly
large values. It is common to scale the covariance matrix by
@@ -2257,7 +2260,10 @@ $$
In the above example this is the function we constructed using pandas. In our derivation of the various regression algorithms like Ordinary Least Squares or Ridge regression
we defined the design/feature matrix \( \boldsymbol{X} \) as
With these definitions, we can now rewrite our \( 2\times 2 \)
-correaltion/covariance matrix in terms of a moe general design/feature
+correlation/covariance matrix in terms of a moe general design/feature
matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \). This leads to a \( p\times p \)
covariance matrix for the vectors \( \boldsymbol{x}_i \) with \( i=0,1,\dots,p-1 \)
and the correlation matrix The Numpy function np.cov calculates the covariance elements using
the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have
@@ -2385,7 +2402,7 @@ C = np.cov(W)
The previous example can be converted into the correlation matrix by
simply scaling the matrix elements with the variances. We should also
@@ -2443,6 +2460,10 @@ this matrix we easily see that it is a positive definite matrix.
The above procedure with numpy can be made more compact if we use pandas. We whow here how we can set up the correlation matrix using pandas, as done in this simple code We expand this model to the Franke function discussed above. We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \( \boldsymbol{X} \) as If we then compute the expectation value In the derivation of the PCA theorem we will assume that the eigenvalues are ordered in descending order, that is
\( \lambda_0 > \lambda_1 > \dots > \lambda_{p-1} \).
@@ -2654,7 +2691,10 @@ dimensions where the eigenvalues are very small. If \( p \) is very large,
we could then aim at reducing \( p \) to \( l < < p \) and handle only \( l \)
features/predictors.
Here's how we would proceed in setting up the algorithm for the PCA, see also discussion below here.
- We will use a simple example first with two-dimensional data
drawn from a multivariate normal distribution with the following mean and covariance matrix (we have fixed these quantities but will play around with them below):
@@ -2699,7 +2741,10 @@ $$
We will generate \( n = 10000 \) points \( X = \{ x_1, \ldots, x_N \} \) from
this distribution, and store them in the \( 1000 \times 2 \) matrix \( \boldsymbol{X} \). This is our design matrix where we have forced the covariance and mean values to take specific values.
The following Python code aids in setting up the data and writing out the design matrix.
Note that the function multivariate returns also the covariance discussed above and that it is defined by dividing by \( n-1 \) instead of \( n \).
Now we are going to implement the PCA algorithm. We will break it down into various substeps. 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 Alternatively, we could use the functions we discussed
earlier for scaling the data set. That is, we could have used the
StandardScaler function in Scikit-Learn, a function which ensures
@@ -2791,6 +2843,10 @@ 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.
Now we are going to use the mean centered data to compute the sample covariance of the data by using the following equation 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? Try to change the covariance and the mean values. For example, try to make the variance of the first element much larger than that of the second diagonal element. Try also to shrink the covariance (the non-diagonal elements) and see how the data points are distributed.
Now we are ready to solve for the principal components! To do so we
diagonalize the sample covariance matrix \( \Sigma \). We can use the
@@ -2894,6 +2957,10 @@ $$
where \( v_0 \) is the first principal component. Collecting all these steps we can write our own PCA function and
compare this with the functionality included in Scikit-Learn.
@@ -2948,6 +3015,9 @@ X2Dsl = pca.fit_transform(X)
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? We assume now that we have a design matrix \( \boldsymbol{X} \) which has been
@@ -2965,6 +3035,10 @@ low-dimensional encoding of the data is then given by a set of vectors
orthogonal projection of the data onto the columns spanned by the
eigenvectors of the covariance(correlations matrix).
To show the PCA theorem let us start with the assumption that there is one vector \( \boldsymbol{s}_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 For more details, see for example Vidal, Ma and Sastry, chapter 2. For a detailed demonstration of the geometric interpretation, see Vidal, Ma and Sastry, section 2.1.2. Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The
@@ -3301,7 +3381,10 @@ X_reduced = pca.fit_transform(X)
- One problem with the preceding implementation of PCA is that it requires the whole training set to fit in
memory in order for the SVD algorithm to run. Fortunately, Incremental PCA (IPCA) algorithms have
@@ -3352,6 +3435,9 @@ X_reduced = rbf_pca.fit_transform(X)
+ There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn. The covariance takes values between zero and infinity and may thus
lead to problems with loss of numerical precision for particularly
large values. It is common to scale the covariance matrix by
@@ -2210,6 +2242,8 @@ $$
In the above example this is the function we constructed using pandas. In our derivation of the various regression algorithms like Ordinary Least Squares or Ridge regression
we defined the design/feature matrix \( \boldsymbol{X} \) as
With these definitions, we can now rewrite our \( 2\times 2 \)
-correaltion/covariance matrix in terms of a moe general design/feature
+correlation/covariance matrix in terms of a moe general design/feature
matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \). This leads to a \( p\times p \)
covariance matrix for the vectors \( \boldsymbol{x}_i \) with \( i=0,1,\dots,p-1 \)
and the correlation matrix The Numpy function np.cov calculates the covariance elements using
the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have
the exact mean values. The following simple function uses the
@@ -2325,7 +2370,7 @@ C = np.cov(W)
The previous example can be converted into the correlation matrix by
simply scaling the matrix elements with the variances. We should also
@@ -2384,6 +2429,9 @@ this matrix we easily see that it is a positive definite matrix.
The above procedure with numpy can be made more compact if we use pandas. We whow here how we can set up the correlation matrix using pandas, as done in this simple code We expand this model to the Franke function discussed above.Incremental PCA
+
+
+
+Incremental PCA
Oct 28, 2021
+Oct 29, 2021
@@ -2228,7 +2228,10 @@ $$
\end{bmatrix}.
$$
+
+More on the covariance
Reminding ourselves about Linear Regression
+Simple Example
+The Correlation Matrix
@@ -2324,6 +2337,10 @@ $$
\end{bmatrix},
$$
+Numpy Functionality
Correlation Matrix
+Correlation Matrix again
Using Pandas
And then the Franke Function
Lnks with the Design Matrix
@@ -2573,6 +2602,10 @@ x_{10} & x_{11}\\
\end{bmatrix}.
$$
+Computing the Expectation Values
@@ -2637,6 +2670,10 @@ $$
\boldsymbol{S}_i\lambda_i = \boldsymbol{C}[\boldsymbol{x}]\boldsymbol{S}_i.
$$
+More on the PCA Theorem
The Algorithm before theorem
+The Algorithm before theorem
@@ -2681,8 +2721,10 @@ $$
-Writing our own PCA code
+Writing our own PCA code
Implementing it
First Step
@@ -2780,7 +2829,10 @@ X_centered = X - X.mean(axis=0)
+Scaling
Centered Data
@@ -2865,11 +2921,18 @@ plt.show()
+Exploring
Diagonalize the sample covariance matrix to obtain the principal components
+Diagonalize the sample covariance matrix to obtain the principal components
Collecting all Steps
Classical PCA Theorem
The PCA Theorem
Geometric Interpretation and link with Singular Value Decomposition
PCA and scikit-learn
Incremental PCA
+Incremental PCA
Other techniques
Oct 28, 2021
+Oct 29, 2021
@@ -2185,6 +2214,9 @@ $$
\end{bmatrix}.
$$
+
+
+More on the covariance
+Reminding ourselves about Linear Regression
+Simple Example
+The Correlation Matrix
+
+Numpy Functionality
+
-Correlation Matrix
+Correlation Matrix again
+Using Pandas
+
+And then the Franke Function
+
We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \( \boldsymbol{X} \) as
$$ \boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}= \mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}]. @@ -2510,6 +2565,10 @@ x_{10} & x_{11}\\ \end{bmatrix}. $$ + +If we then compute the expectation value
$$ \mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}=\begin{bmatrix} @@ -2559,6 +2618,10 @@ $$ \boldsymbol{S}_i\lambda_i = \boldsymbol{C}[\boldsymbol{x}]\boldsymbol{S}_i. $$ + +In the derivation of the PCA theorem we will assume that the eigenvalues are ordered in descending order, that is \( \lambda_0 > \lambda_1 > \dots > \lambda_{p-1} \).
@@ -2575,7 +2638,9 @@ dimensions where the eigenvalues are very small. If \( p \) is very large, we could then aim at reducing \( p \) to \( l < < p \) and handle only \( l \) features/predictors. -Here's how we would proceed in setting up the algorithm for the PCA, see also discussion below here.
We will use a simple example first with two-dimensional data drawn from a multivariate normal distribution with the following mean and covariance matrix (we have fixed these quantities but will play around with them below): @@ -2615,6 +2681,8 @@ We will generate \( n = 10000 \) points \( X = \{ x_1, \ldots, x_N \} \) from this distribution, and store them in the \( 1000 \times 2 \) matrix \( \boldsymbol{X} \). This is our design matrix where we have forced the covariance and mean values to take specific values.
+The following Python code aids in setting up the data and writing out the design matrix. Note that the function multivariate returns also the covariance discussed above and that it is defined by dividing by \( n-1 \) instead of \( n \).
@@ -2650,6 +2718,9 @@ 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.
+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
$$ \mu_n = \frac{1}{n} \sum_{i=1}^n x_i @@ -2692,6 +2763,9 @@ X_centered = X - X.mean(axis=0) + +Alternatively, we could use the functions we discussed earlier for scaling the data set. That is, we could have used the StandardScaler function in Scikit-Learn, a function which ensures @@ -2703,6 +2777,9 @@ while the non-diagonal ones need to be divided by \( 2\sqrt{2} \) for our specific case.
+Now we are going to use the mean centered data to compute the sample covariance of the data by using the following equation
$$ \begin{equation*} @@ -2775,10 +2852,16 @@ 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? Try to change the covariance and the mean values. For example, try to make the variance of the first element much larger than that of the second diagonal element. Try also to shrink the covariance (the non-diagonal elements) and see how the data points are distributed.
-Now we are ready to solve for the principal components! To do so we diagonalize the sample covariance matrix \( \Sigma \). We can use the @@ -2801,6 +2884,9 @@ $$
where \( v_0 \) is the first principal component.
+Collecting all these steps we can write our own PCA function and compare this with the functionality included in Scikit-Learn.
@@ -2854,6 +2940,8 @@ X2Dsl = pca.fit_transform(X)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?
+ +We assume now that we have a design matrix \( \boldsymbol{X} \) which has been @@ -2872,6 +2960,9 @@ orthogonal projection of the data onto the columns spanned by the eigenvectors of the covariance(correlations matrix).
+To show the PCA theorem let us start with the assumption that there is one vector \( \boldsymbol{s}_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
We are almost there, we have obtained a relation between minimizing @@ -2927,6 +3018,8 @@ chapter 12.4 and discussion therein.
For more details, see for example Vidal, Ma and Sastry, chapter 2.
+ +For a detailed demonstration of the geometric interpretation, see Vidal, Ma and Sastry, section 2.1.2.
@@ -3016,6 +3109,9 @@ X2D = X_centered.dot(W2) + + +Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The @@ -3198,7 +3294,10 @@ X_reduced = pca.fit_transform(X) -
One problem with the preceding implementation of PCA is that it requires the whole training set to fit in
memory in order for the SVD algorithm to run. Fortunately, Incremental PCA (IPCA) algorithms have
@@ -3249,6 +3348,9 @@ X_reduced = rbf_pca.fit_transform(X)
+
+
+
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 9d5cddd98..054bfe344 100644 --- a/doc/pub/week43/html/week43.html +++ b/doc/pub/week43/html/week43.html @@ -201,19 +201,48 @@ div.toc p,a { 2, None, 'introducing-the-covariance-and-correlation-functions'), - ('Correlation Matrix', 2, None, 'correlation-matrix'), + ('More on the covariance', 2, None, 'more-on-the-covariance'), + ('Reminding ourselves about Linear Regression', + 2, + None, + 'reminding-ourselves-about-linear-regression'), + ('Simple Example', 2, None, 'simple-example'), + ('The Correlation Matrix', 2, None, 'the-correlation-matrix'), + ('Numpy Functionality', 2, None, 'numpy-functionality'), + ('Correlation Matrix again', 2, None, 'correlation-matrix-again'), + ('Using Pandas', 2, None, 'using-pandas'), + ('And then the Franke Function', + 2, + None, + 'and-then-the-franke-function'), + ('Lnks with the Design Matrix', + 2, + None, + 'lnks-with-the-design-matrix'), + ('Computing the Expectation Values', + 2, + None, + 'computing-the-expectation-values'), ('Towards the PCA theorem', 2, None, 'towards-the-pca-theorem'), + ('More on the PCA Theorem', 2, None, 'more-on-the-pca-theorem'), ('The Algorithm before the Theorem', - 3, + 2, None, 'the-algorithm-before-the-theorem'), - ('Writing our own PCA code', 3, None, 'writing-our-own-pca-code'), + ('Writing our own PCA code', 2, None, 'writing-our-own-pca-code'), + ('Implementing it', 2, None, 'implementing-it'), + ('First Step', 2, None, 'first-step'), + ('Scaling', 2, None, 'scaling'), + ('Centered Data', 2, None, 'centered-data'), + ('Exploring', 2, None, 'exploring'), ('Diagonalize the sample covariance matrix to obtain the ' 'principal components', - 3, + 2, None, 'diagonalize-the-sample-covariance-matrix-to-obtain-the-principal-components'), + ('Collecting all Steps', 2, None, 'collecting-all-steps'), ('Classical PCA Theorem', 2, None, 'classical-pca-theorem'), + ('The PCA Theorem', 2, None, 'the-pca-theorem'), ('Geometric Interpretation and link with Singular Value ' 'Decomposition', 2, @@ -221,7 +250,7 @@ div.toc p,a { 'geometric-interpretation-and-link-with-singular-value-decomposition'), ('PCA and scikit-learn', 2, None, 'pca-and-scikit-learn'), ('Back to the Cancer Data', 2, None, 'back-to-the-cancer-data'), - ('Incremental PCA', 3, None, 'incremental-pca'), + ('Incremental PCA', 2, None, 'incremental-pca'), ('Randomized PCA', 3, None, 'randomized-pca'), ('Kernel PCA', 3, None, 'kernel-pca'), ('Other techniques', 2, None, 'other-techniques')]} @@ -262,7 +291,7 @@ MathJax.Hub.Config({The covariance takes values between zero and infinity and may thus lead to problems with loss of numerical precision for particularly large values. It is common to scale the covariance matrix by @@ -2287,6 +2319,8 @@ $$
In the above example this is the function we constructed using pandas.
+In our derivation of the various regression algorithms like Ordinary Least Squares or Ridge regression we defined the design/feature matrix \( \boldsymbol{X} \) as
@@ -2315,8 +2349,11 @@ $$ \boldsymbol{x}_i^T = \begin{bmatrix}x_{0,i} & x_{1,i} & x_{2,i}& \dots & \dots x_{n-1,i}\end{bmatrix}. $$ + +With these definitions, we can now rewrite our \( 2\times 2 \) -correaltion/covariance matrix in terms of a moe general design/feature +correlation/covariance matrix in terms of a moe general design/feature matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \). This leads to a \( p\times p \) covariance matrix for the vectors \( \boldsymbol{x}_i \) with \( i=0,1,\dots,p-1 \)
@@ -2332,6 +2369,10 @@ $$ \end{bmatrix}, $$ + +and the correlation matrix
$$ \boldsymbol{K}[\boldsymbol{x}] = \begin{bmatrix} @@ -2344,6 +2385,10 @@ $$ \end{bmatrix}, $$ + +The Numpy function np.cov calculates the covariance elements using
the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have
the exact mean values. The following simple function uses the
@@ -2402,7 +2447,7 @@ C = np.c
-
The previous example can be converted into the correlation matrix by simply scaling the matrix elements with the variances. We should also @@ -2461,6 +2506,9 @@ this matrix we easily see that it is a positive definite matrix.
The above procedure with numpy can be made more compact if we use pandas.
+We whow here how we can set up the correlation matrix using pandas, as done in this simple code
@@ -2497,6 +2545,10 @@ correlation_matrix = XpdAnd then the Franke Function +We expand this model to the Franke function discussed above.
@@ -2569,9 +2621,12 @@ columns since all matrix elements in the design matrix were set to one (we are fitting the function in terms of a polynomial of degree \( n \)). We would however not include the intercept and wee can simply drop these elements and construct a correlation -matrix without them. +matrix without them by centering our matrix elements by subtracting the mean of each column. +We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \( \boldsymbol{X} \) as
$$ \boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}= \mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}]. @@ -2587,6 +2642,10 @@ x_{10} & x_{11}\\ \end{bmatrix}. $$ + +If we then compute the expectation value
$$ \mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}=\begin{bmatrix} @@ -2636,6 +2695,10 @@ $$ \boldsymbol{S}_i\lambda_i = \boldsymbol{C}[\boldsymbol{x}]\boldsymbol{S}_i. $$ + +In the derivation of the PCA theorem we will assume that the eigenvalues are ordered in descending order, that is \( \lambda_0 > \lambda_1 > \dots > \lambda_{p-1} \).
@@ -2652,7 +2715,9 @@ dimensions where the eigenvalues are very small. If \( p \) is very large, we could then aim at reducing \( p \) to \( l < < p \) and handle only \( l \) features/predictors. -Here's how we would proceed in setting up the algorithm for the PCA, see also discussion below here.
We will use a simple example first with two-dimensional data drawn from a multivariate normal distribution with the following mean and covariance matrix (we have fixed these quantities but will play around with them below): @@ -2692,6 +2758,8 @@ We will generate \( n = 10000 \) points \( X = \{ x_1, \ldots, x_N \} \) from this distribution, and store them in the \( 1000 \times 2 \) matrix \( \boldsymbol{X} \). This is our design matrix where we have forced the covariance and mean values to take specific values.
+The following Python code aids in setting up the data and writing out the design matrix. Note that the function multivariate returns also the covariance discussed above and that it is defined by dividing by \( n-1 \) instead of \( n \).
@@ -2727,6 +2795,9 @@ X = np.rNow we are going to implement the PCA algorithm. We will break it down into various substeps.
+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
$$ \mu_n = \frac{1}{n} \sum_{i=1}^n x_i @@ -2769,6 +2840,9 @@ X_centered = X + +Alternatively, we could use the functions we discussed earlier for scaling the data set. That is, we could have used the StandardScaler function in Scikit-Learn, a function which ensures @@ -2780,6 +2854,9 @@ while the non-diagonal ones need to be divided by \( 2\sqrt{2} \) for our specific case.
+Now we are going to use the mean centered data to compute the sample covariance of the data by using the following equation
$$ \begin{equation*} @@ -2852,10 +2929,16 @@ 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? Try to change the covariance and the mean values. For example, try to make the variance of the first element much larger than that of the second diagonal element. Try also to shrink the covariance (the non-diagonal elements) and see how the data points are distributed.
-Now we are ready to solve for the principal components! To do so we diagonalize the sample covariance matrix \( \Sigma \). We can use the @@ -2878,6 +2961,9 @@ $$
where \( v_0 \) is the first principal component.
+Collecting all these steps we can write our own PCA function and compare this with the functionality included in Scikit-Learn.
@@ -2931,6 +3017,8 @@ X2Dsl = pca.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?
+ +We assume now that we have a design matrix \( \boldsymbol{X} \) which has been @@ -2949,6 +3037,9 @@ orthogonal projection of the data onto the columns spanned by the eigenvectors of the covariance(correlations matrix).
+To show the PCA theorem let us start with the assumption that there is one vector \( \boldsymbol{s}_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
We are almost there, we have obtained a relation between minimizing @@ -3004,6 +3095,8 @@ chapter 12.4 and discussion therein.
For more details, see for example Vidal, Ma and Sastry, chapter 2.
+ +For a detailed demonstration of the geometric interpretation, see Vidal, Ma and Sastry, section 2.1.2.
@@ -3093,6 +3186,9 @@ X2D = X_centered + + +Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The
@@ -3275,7 +3371,10 @@ X_reduced = pca
- One problem with the preceding implementation of PCA is that it requires the whole training set to fit in
memory in order for the SVD algorithm to run. Fortunately, Incremental PCA (IPCA) algorithms have
@@ -3326,6 +3425,9 @@ X_reduced = rbf_pcaOther techniques
There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn.Incremental PCA
+
+
+
+Incremental PCA