diff --git a/doc/pub/DimRed/html/._DimRed-bs000.html b/doc/pub/DimRed/html/._DimRed-bs000.html index db2c29bdc..f4b4f0e52 100644 --- a/doc/pub/DimRed/html/._DimRed-bs000.html +++ b/doc/pub/DimRed/html/._DimRed-bs000.html @@ -224,7 +224,7 @@ MathJax.Hub.Config({
-
diff --git a/doc/pub/DimRed/html/._DimRed-bs018.html b/doc/pub/DimRed/html/._DimRed-bs018.html index 2df856349..e7381a724 100644 --- a/doc/pub/DimRed/html/._DimRed-bs018.html +++ b/doc/pub/DimRed/html/._DimRed-bs018.html @@ -221,8 +221,8 @@ We will generate \( n = 1000 \) points \( X = \{ x_1, \ldots, x_N \} \) from this distribution, and store them in the \( 1000 \times 2 \) matrix \( \boldsymbol{X} \).
-The following Python code aids in setting up the data - +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 \).
@@ -237,7 +237,7 @@ X = np.r print(X)
-Make thereafter a small Python code which writes out the data. Note that the function multivariate returns also the covariance discussed above and that it is defined by dividing by \( n-1 \) instead of \( n \). +Try to add to this code your own calculation of the covariance matrix.
Now we are going to implement the PCA algorithm. We will break it down into various substeps. @@ -258,7 +258,7 @@ $$ When you are done with these steps, print out \( \mu_n \) to verify it is close to \( \mu \) and plot your mean centered data to verify it is centered at the origin! Compare your code with the functionality from Scikit-Learn discussed above. - +The following code elements perform these operations using pandas or your own functionality for doing so.
@@ -272,6 +272,12 @@ X_centered = X
# test that we get the same as Pandas
print(X_centered-df)
+
+Alternatively, you could also have used 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 that for each feature/predictor we study the mean value is
+zero and the variance is one (every column in the design/feature
+matrix).
Finally, collect all these steps and write your own PCA function and
-compare this with the functionality included in Scikit-Learn.
-Have the input be the data and have the output be the principal components and their associated eigenvalues, sorted in descending order. Can you think of a way to make it more efficient than the algorithm outlined above?
+compare this with the functionality included in Scikit-Learn. Have
+the input be the data and have the output be the principal components
+and their associated eigenvalues, sorted in descending order. Can you
+think of a way to make it more efficient than the algorithm outlined
+above?
+
+The code here outlines some of the elements you could include in this analysis.
diff --git a/doc/pub/DimRed/html/DimRed-bs.html b/doc/pub/DimRed/html/DimRed-bs.html
index db2c29bdc..f4b4f0e52 100644
--- a/doc/pub/DimRed/html/DimRed-bs.html
+++ b/doc/pub/DimRed/html/DimRed-bs.html
@@ -224,7 +224,7 @@ MathJax.Hub.Config({
-
diff --git a/doc/pub/DimRed/html/DimRed-reveal.html b/doc/pub/DimRed/html/DimRed-reveal.html
index 7024e385a..4c8a61a41 100644
--- a/doc/pub/DimRed/html/DimRed-reveal.html
+++ b/doc/pub/DimRed/html/DimRed-reveal.html
@@ -148,7 +148,7 @@ MathJax.Hub.Config({
@@ -1027,8 +1027,8 @@ We will generate \( n = 1000 \) points \( X = \{ x_1, \ldots, x_N \} \) from
this distribution, and store them in the \( 1000 \times 2 \) matrix \( \boldsymbol{X} \).
-The following Python code aids in setting up the data
-
+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 \).
@@ -1043,7 +1043,7 @@ X = np.random.multivariate_normal(mean, cov, n)
print(X)
-Make thereafter a small Python code which writes out the data. Note that the function multivariate returns also the covariance discussed above and that it is defined by dividing by \( n-1 \) instead of \( n \).
+Try to add to this code your own calculation of the covariance matrix.
Now we are going to implement the PCA algorithm. We will break it down into various substeps.
@@ -1068,7 +1068,7 @@ $$
When you are done with these steps, print out \( \mu_n \) to verify it is
close to \( \mu \) and plot your mean centered data to verify it is
centered at the origin! Compare your code with the functionality from Scikit-Learn discussed above.
-
+The following code elements perform these operations using pandas or your own functionality for doing so.
@@ -1082,6 +1082,12 @@ X_centered = X - X.mean(axis=0)
# test that we get the same as Pandas
print(X_centered-df)
+
+Alternatively, you could also have used 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 that for each feature/predictor we study the mean value is
+zero and the variance is one (every column in the design/feature
+matrix).
Finally, collect all these steps and write your own PCA function and
-compare this with the functionality included in Scikit-Learn.
-Have the input be the data and have the output be the principal components and their associated eigenvalues, sorted in descending order. Can you think of a way to make it more efficient than the algorithm outlined above?
+compare this with the functionality included in Scikit-Learn. Have
+the input be the data and have the output be the principal components
+and their associated eigenvalues, sorted in descending order. Can you
+think of a way to make it more efficient than the algorithm outlined
+above?
+
+The code here outlines some of the elements you could include in this analysis.
diff --git a/doc/pub/DimRed/html/DimRed-solarized.html b/doc/pub/DimRed/html/DimRed-solarized.html
index 0edd74866..b46a53c3d 100644
--- a/doc/pub/DimRed/html/DimRed-solarized.html
+++ b/doc/pub/DimRed/html/DimRed-solarized.html
@@ -179,7 +179,7 @@ MathJax.Hub.Config({
-
-The following Python code aids in setting up the data
-
+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 \).
@@ -1023,7 +1023,7 @@ X = np.random.multivariate_normal(mean, cov, n)
print(X)
-Make thereafter a small Python code which writes out the data. Note that the function multivariate returns also the covariance discussed above and that it is defined by dividing by \( n-1 \) instead of \( n \).
+Try to add to this code your own calculation of the covariance matrix.
Now we are going to implement the PCA algorithm. We will break it down into various substeps.
@@ -1044,7 +1044,7 @@ $$
When you are done with these steps, print out \( \mu_n \) to verify it is
close to \( \mu \) and plot your mean centered data to verify it is
centered at the origin! Compare your code with the functionality from Scikit-Learn discussed above.
-
+The following code elements perform these operations using pandas or your own functionality for doing so.
@@ -1058,6 +1058,12 @@ X_centered = X - X.mean(axis=0)
# test that we get the same as Pandas
print(X_centered-df)
+
+Alternatively, you could also have used 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 that for each feature/predictor we study the mean value is
+zero and the variance is one (every column in the design/feature
+matrix).
Finally, collect all these steps and write your own PCA function and
-compare this with the functionality included in Scikit-Learn.
-Have the input be the data and have the output be the principal components and their associated eigenvalues, sorted in descending order. Can you think of a way to make it more efficient than the algorithm outlined above?
+compare this with the functionality included in Scikit-Learn. Have
+the input be the data and have the output be the principal components
+and their associated eigenvalues, sorted in descending order. Can you
+think of a way to make it more efficient than the algorithm outlined
+above?
+
+The code here outlines some of the elements you could include in this analysis.
diff --git a/doc/pub/DimRed/html/DimRed.html b/doc/pub/DimRed/html/DimRed.html
index c0500aa5f..3d025d9fe 100644
--- a/doc/pub/DimRed/html/DimRed.html
+++ b/doc/pub/DimRed/html/DimRed.html
@@ -184,7 +184,7 @@ MathJax.Hub.Config({
-
-The following Python code aids in setting up the data
-
+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 \).
@@ -1028,7 +1028,7 @@ X = np.r
print(X)
-Make thereafter a small Python code which writes out the data. Note that the function multivariate returns also the covariance discussed above and that it is defined by dividing by \( n-1 \) instead of \( n \).
+Try to add to this code your own calculation of the covariance matrix.
Now we are going to implement the PCA algorithm. We will break it down into various substeps.
@@ -1049,7 +1049,7 @@ $$
When you are done with these steps, print out \( \mu_n \) to verify it is
close to \( \mu \) and plot your mean centered data to verify it is
centered at the origin! Compare your code with the functionality from Scikit-Learn discussed above.
-
+The following code elements perform these operations using pandas or your own functionality for doing so.
@@ -1063,6 +1063,12 @@ X_centered = X
# test that we get the same as Pandas
print(X_centered-df)
+
+Alternatively, you could also have used 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 that for each feature/predictor we study the mean value is
+zero and the variance is one (every column in the design/feature
+matrix).
Finally, collect all these steps and write your own PCA function and
-compare this with the functionality included in Scikit-Learn.
-Have the input be the data and have the output be the principal components and their associated eigenvalues, sorted in descending order. Can you think of a way to make it more efficient than the algorithm outlined above?
+compare this with the functionality included in Scikit-Learn. Have
+the input be the data and have the output be the principal components
+and their associated eigenvalues, sorted in descending order. Can you
+think of a way to make it more efficient than the algorithm outlined
+above?
+
+The code here outlines some of the elements you could include in this analysis.
diff --git a/doc/pub/DimRed/ipynb/DimRed.ipynb b/doc/pub/DimRed/ipynb/DimRed.ipynb
index 97b75d4b2..5707585db 100644
--- a/doc/pub/DimRed/ipynb/DimRed.ipynb
+++ b/doc/pub/DimRed/ipynb/DimRed.ipynb
@@ -10,7 +10,7 @@
" \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: **Dec 30, 2019**\n",
+ "Date: **Dec 31, 2019**\n",
"\n",
"Copyright 1999-2019, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
"\n",
@@ -1093,7 +1093,8 @@
"We will generate $n = 1000$ points $X = \\{ x_1, \\ldots, x_N \\}$ from\n",
"this distribution, and store them in the $1000 \\times 2$ matrix $\\boldsymbol{X}$.\n",
"\n",
- "The following Python code aids in setting up the data"
+ "The following Python code aids in setting up the data and writing out the design matrix.\n",
+ "Note that the function **multivariate** returns also the covariance discussed above and that it is defined by dividing by $n-1$ instead of $n$."
]
},
{
@@ -1119,7 +1120,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "Make thereafter a small Python code which writes out the data. Note that the function **multivariate** returns also the covariance discussed above and that it is defined by dividing by $n-1$ instead of $n$.\n",
+ "Try to add to this code your own calculation of the covariance matrix.\n",
"\n",
"Now we are going to implement the PCA algorithm. We will break it down into various substeps.\n",
"\n",
@@ -1159,7 +1160,8 @@
"source": [
"When you are done with these steps, print out $\\mu_n$ to verify it is\n",
"close to $\\mu$ and plot your mean centered data to verify it is\n",
- "centered at the origin! Compare your code with the functionality from **Scikit-Learn** discussed above."
+ "centered at the origin! Compare your code with the functionality from **Scikit-Learn** discussed above.\n",
+ "The following code elements perform these operations using **pandas** or your own functionality for doing so."
]
},
{
@@ -1185,6 +1187,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
+ "Alternatively, you could also have used the functions we discussed earlier for scaling the data set. \n",
+ "That is, we could have used the **StandardScaler** function in **Scikit-Learn**, a function which \n",
+ "ensures that for each feature/predictor we study the mean value is\n",
+ "zero and the variance is one (every column in the design/feature\n",
+ "matrix).\n",
+ "\n",
"### Compute the sample covariance\n",
"\n",
"Now we are going to use the mean centered data to compute the sample covariance of the data. Recall it is given by:"
@@ -1240,8 +1248,13 @@
"where $v_0$ is the first principal component. What do you observe?\n",
"\n",
"Finally, collect all these steps and write your own PCA function and\n",
- "compare this with the functionality included in **Scikit-Learn**.\n",
- "Have the input be the data and have the output be the principal components and their associated eigenvalues, sorted in descending order. Can you think of a way to make it more efficient than the algorithm outlined above?"
+ "compare this with the functionality included in **Scikit-Learn**. Have\n",
+ "the input be the data and have the output be the principal components\n",
+ "and their associated eigenvalues, sorted in descending order. Can you\n",
+ "think of a way to make it more efficient than the algorithm outlined\n",
+ "above?\n",
+ "\n",
+ "The code here outlines some of the elements you could include in this analysis."
]
},
{
diff --git a/doc/pub/DimRed/ipynb/ipynb-DimRed-src.tar.gz b/doc/pub/DimRed/ipynb/ipynb-DimRed-src.tar.gz
index 965253985..ed525379a 100644
Binary files a/doc/pub/DimRed/ipynb/ipynb-DimRed-src.tar.gz and b/doc/pub/DimRed/ipynb/ipynb-DimRed-src.tar.gz differ
diff --git a/doc/pub/DimRed/pdf/DimRed-minted.pdf b/doc/pub/DimRed/pdf/DimRed-minted.pdf
index 0cea4334d..243dbec69 100644
Binary files a/doc/pub/DimRed/pdf/DimRed-minted.pdf and b/doc/pub/DimRed/pdf/DimRed-minted.pdf differ
diff --git a/doc/src/DimRed/DimRed.do.txt b/doc/src/DimRed/DimRed.do.txt
index 54b80f7b5..0d9e62d79 100644
--- a/doc/src/DimRed/DimRed.do.txt
+++ b/doc/src/DimRed/DimRed.do.txt
@@ -776,8 +776,8 @@ Note that the mean refers to each column of data.
We will generate $n = 1000$ points $X = \{ x_1, \ldots, x_N \}$ from
this distribution, and store them in the $1000 \times 2$ matrix $\bm{X}$.
-The following Python code aids in setting up the data
-
+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$.
!bc pycod
import numpy as np
import pandas as pd
@@ -791,7 +791,7 @@ print(X)
!ec
-Make thereafter a small Python code which writes out the data. Note that the function _multivariate_ returns also the covariance discussed above and that it is defined by dividing by $n-1$ instead of $n$.
+Try to add to this code your own calculation of the covariance matrix.
Now we are going to implement the PCA algorithm. We will break it down into various substeps.
@@ -812,7 +812,7 @@ and the mean-centered data $\bar{X} = \{ \bar{x}_1, \ldots, \bar{x}_n \}$ takes
When you are done with these steps, print out $\mu_n$ to verify it is
close to $\mu$ and plot your mean centered data to verify it is
centered at the origin! Compare your code with the functionality from _Scikit-Learn_ discussed above.
-
+The following code elements perform these operations using _pandas_ or your own functionality for doing so.
!bc pycod
df = pd.DataFrame(X)
# Pandas does the centering for us
@@ -824,7 +824,11 @@ X_centered = X - X.mean(axis=0)
# test that we get the same as Pandas
print(X_centered-df)
!ec
-
+Alternatively, you could also have used 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 that for each feature/predictor we study the mean value is
+zero and the variance is one (every column in the design/feature
+matrix).
=== Compute the sample covariance ===
@@ -858,9 +862,13 @@ x_i \approx \tilde{x}_i := \mu_n + \langle x_i, v_0 \rangle v_0
where $v_0$ is the first principal component. What do you observe?
Finally, collect all these steps and write your own PCA function and
-compare this with the functionality included in _Scikit-Learn_.
-Have the input be the data and have the output be the principal components and their associated eigenvalues, sorted in descending order. Can you think of a way to make it more efficient than the algorithm outlined above?
+compare this with the functionality included in _Scikit-Learn_. Have
+the input be the data and have the output be the principal components
+and their associated eigenvalues, sorted in descending order. Can you
+think of a way to make it more efficient than the algorithm outlined
+above?
+The code here outlines some of the elements you could include in this analysis.
!bc pycod
#Now we do an SVD
U, s, V = np.linalg.svd(X_centered)
Compute the sample covariance
@@ -312,9 +318,14 @@ where \( v_0 \) is the first principal component. What do you observe?
Dec 30, 2019
Dec 31, 2019
-Dec 30, 2019
Dec 31, 2019
Compute the sample covariance
@@ -1125,9 +1131,14 @@ where \( v_0 \) is the first principal component. What do you observe?
Dec 30, 2019
Dec 31, 2019
@@ -1007,8 +1007,8 @@ We will generate \( n = 1000 \) points \( X = \{ x_1, \ldots, x_N \} \) from
this distribution, and store them in the \( 1000 \times 2 \) matrix \( \boldsymbol{X} \).
Compute the sample covariance
@@ -1098,9 +1104,14 @@ where \( v_0 \) is the first principal component. What do you observe?
Dec 30, 2019
Dec 31, 2019
@@ -1012,8 +1012,8 @@ We will generate \( n = 1000 \) points \( X = \{ x_1, \ldots, x_N \} \) from
this distribution, and store them in the \( 1000 \times 2 \) matrix \( \boldsymbol{X} \).
Compute the sample covariance
@@ -1103,9 +1109,14 @@ where \( v_0 \) is the first principal component. What do you observe?