diff --git a/doc/pub/DimRed/html/._DimRed-bs018.html b/doc/pub/DimRed/html/._DimRed-bs018.html index 26757403b..a7cce97b1 100644 --- a/doc/pub/DimRed/html/._DimRed-bs018.html +++ b/doc/pub/DimRed/html/._DimRed-bs018.html @@ -229,7 +229,7 @@ Note that the function multivariate returns also the covariance discussed
import numpy as np
import pandas as pd
from IPython.display import display
-n = 100
+n = 10000
mean = (-1, 2)
cov = [[4, 2], [2, 2]]
X = np.random.multivariate_normal(mean, cov, n)
@@ -313,14 +313,15 @@ 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?
Now we are ready to solve for the principal components! To do so we -diagonalize the sample covariance matrix \( \Sigma_n \). We can use the +diagonalize the sample covariance matrix \( \Sigma \). We can use the function np.linalg.eig to do so. It will return the eigenvalues and -eigenvectors of \( \Sigma_n \). Once we have these we can perform the +eigenvectors of \( \Sigma \). Once we have these we can perform the following tasks:
-The code here outlines some of the elements we could include in the analysis. Feel free to extend upon this. +The code here outlines some of the elements we could include in the +analysis. Feel free to extend upon this in order to address the above +questions. +
-
#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)
+# diagonalize and obtain eigenvalues, not necessarily sorted
+EigValues, EigVectors = np.linalg.eig(Cov)
+# sort eigenvectors and eigenvalues
+#permute = EigValues.argsort()
+#EigValues = EigValues[permute]
+#EigVectors = EigVectors[:,permute]
+print("Eigenvalues of Covariance matrix")
+for i in range(2):
+ print(EigValues[i])
+FirstEigvector = EigVectors[:,0]
+SecondEigvector = EigVectors[:,1]
+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("Check that we get the same")
-print(X2D-X2Dsl)
-
+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, based on the above, to address the questions above.
+
diff --git a/doc/pub/DimRed/html/DimRed-reveal.html b/doc/pub/DimRed/html/DimRed-reveal.html
index 5a9a90a8f..367451ae8 100644
--- a/doc/pub/DimRed/html/DimRed-reveal.html
+++ b/doc/pub/DimRed/html/DimRed-reveal.html
@@ -1035,7 +1035,7 @@ Note that the function multivariate returns also the covariance discussed
import numpy as np
import pandas as pd
from IPython.display import display
-n = 100
+n = 10000
mean = (-1, 2)
cov = [[4, 2], [2, 2]]
X = np.random.multivariate_normal(mean, cov, n)
@@ -1125,14 +1125,15 @@ 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
Now we are ready to solve for the principal components! To do so we
-diagonalize the sample covariance matrix \( \Sigma_n \). We can use the
+diagonalize the sample covariance matrix \( \Sigma \). We can use the
function np.linalg.eig to do so. It will return the eigenvalues and
-eigenvectors of \( \Sigma_n \). Once we have these we can perform the
+eigenvectors of \( \Sigma \). Once we have these we can perform the
following tasks:
-The code here outlines some of the elements we could include in the analysis. Feel free to extend upon this. +The code here outlines some of the elements we could include in the +analysis. Feel free to extend upon this in order to address the above +questions. +
-
#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)
+# diagonalize and obtain eigenvalues, not necessarily sorted
+EigValues, EigVectors = np.linalg.eig(Cov)
+# sort eigenvectors and eigenvalues
+#permute = EigValues.argsort()
+#EigValues = EigValues[permute]
+#EigVectors = EigVectors[:,permute]
+print("Eigenvalues of Covariance matrix")
+for i in range(2):
+ print(EigValues[i])
+FirstEigvector = EigVectors[:,0]
+SecondEigvector = EigVectors[:,1]
+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("Check that we get the same")
-print(X2D-X2Dsl)
-
+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, based on the above, to address the questions above.
diff --git a/doc/pub/DimRed/html/DimRed-solarized.html b/doc/pub/DimRed/html/DimRed-solarized.html
index 0c1ae2da3..29d7191d6 100644
--- a/doc/pub/DimRed/html/DimRed-solarized.html
+++ b/doc/pub/DimRed/html/DimRed-solarized.html
@@ -1015,7 +1015,7 @@ Note that the function multivariate returns also the covariance discussed
import numpy as np
import pandas as pd
from IPython.display import display
-n = 100
+n = 10000
mean = (-1, 2)
cov = [[4, 2], [2, 2]]
X = np.random.multivariate_normal(mean, cov, n)
@@ -1099,14 +1099,15 @@ 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
Now we are ready to solve for the principal components! To do so we
-diagonalize the sample covariance matrix \( \Sigma_n \). We can use the
+diagonalize the sample covariance matrix \( \Sigma \). We can use the
function np.linalg.eig to do so. It will return the eigenvalues and
-eigenvectors of \( \Sigma_n \). Once we have these we can perform the
+eigenvectors of \( \Sigma \). Once we have these we can perform the
following tasks:
-The code here outlines some of the elements we could include in the analysis. Feel free to extend upon this. +The code here outlines some of the elements we could include in the +analysis. Feel free to extend upon this in order to address the above +questions. +
-
#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)
+# diagonalize and obtain eigenvalues, not necessarily sorted
+EigValues, EigVectors = np.linalg.eig(Cov)
+# sort eigenvectors and eigenvalues
+#permute = EigValues.argsort()
+#EigValues = EigValues[permute]
+#EigVectors = EigVectors[:,permute]
+print("Eigenvalues of Covariance matrix")
+for i in range(2):
+ print(EigValues[i])
+FirstEigvector = EigVectors[:,0]
+SecondEigvector = EigVectors[:,1]
+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("Check that we get the same")
-print(X2D-X2Dsl)
-
+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, based on the above, to address the questions above.
+
diff --git a/doc/pub/DimRed/html/DimRed.html b/doc/pub/DimRed/html/DimRed.html
index 479e67b4f..8960e958f 100644
--- a/doc/pub/DimRed/html/DimRed.html
+++ b/doc/pub/DimRed/html/DimRed.html
@@ -1020,7 +1020,7 @@ Note that the function multivariate returns also the covariance discussed
import numpy as np
import pandas as pd
from IPython.display import display
-n = 100
+n = 10000
mean = (-1, 2)
cov = [[4, 2], [2, 2]]
X = np.random.multivariate_normal(mean, cov, n)
@@ -1104,14 +1104,15 @@ 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
Now we are ready to solve for the principal components! To do so we
-diagonalize the sample covariance matrix \( \Sigma_n \). We can use the
+diagonalize the sample covariance matrix \( \Sigma \). We can use the
function np.linalg.eig to do so. It will return the eigenvalues and
-eigenvectors of \( \Sigma_n \). Once we have these we can perform the
+eigenvectors of \( \Sigma \). Once we have these we can perform the
following tasks:
-The code here outlines some of the elements we could include in the analysis. Feel free to extend upon this. +The code here outlines some of the elements we could include in the +analysis. Feel free to extend upon this in order to address the above +questions. +
-
#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)
+# diagonalize and obtain eigenvalues, not necessarily sorted
+EigValues, EigVectors = np.linalg.eig(Cov)
+# sort eigenvectors and eigenvalues
+#permute = EigValues.argsort()
+#EigValues = EigValues[permute]
+#EigVectors = EigVectors[:,permute]
+print("Eigenvalues of Covariance matrix")
+for i in range(2):
+ print(EigValues[i])
+FirstEigvector = EigVectors[:,0]
+SecondEigvector = EigVectors[:,1]
+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("Check that we get the same")
-print(X2D-X2Dsl)
-
+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, based on the above, to address the questions above.
+
diff --git a/doc/pub/DimRed/ipynb/DimRed.ipynb b/doc/pub/DimRed/ipynb/DimRed.ipynb
index bded76346..2301b9649 100644
--- a/doc/pub/DimRed/ipynb/DimRed.ipynb
+++ b/doc/pub/DimRed/ipynb/DimRed.ipynb
@@ -1108,7 +1108,7 @@
"import numpy as np\n",
"import pandas as pd\n",
"from IPython.display import display\n",
- "n = 100\n",
+ "n = 10000\n",
"mean = (-1, 2)\n",
"cov = [[4, 2], [2, 2]]\n",
"X = np.random.multivariate_normal(mean, cov, n)"
@@ -1258,14 +1258,14 @@
"metadata": {},
"source": [
"Depending on the number of points $n$, we will get results that are close to the covariance values defined above.\n",
- "\n",
+ "The plot shows how the data are clustered around a line with slope close to one. Is this expected?\n",
"\n",
"### Diagonalize the sample covariance matrix to obtain the principal components\n",
"\n",
"Now we are ready to solve for the principal components! To do so we\n",
- "diagonalize the sample covariance matrix $\\Sigma_n$. We can use the\n",
+ "diagonalize the sample covariance matrix $\\Sigma$. We can use the\n",
"function **np.linalg.eig** to do so. It will return the eigenvalues and\n",
- "eigenvectors of $\\Sigma_n$. Once we have these we can perform the \n",
+ "eigenvectors of $\\Sigma$. Once we have these we can perform the \n",
"following tasks:\n",
"\n",
"* We compute the percentage of the total variance captured by the first principal component\n",
@@ -1295,7 +1295,9 @@
"Collecting all these steps we can write our own PCA function and\n",
"compare this with the functionality included in **Scikit-Learn**. \n",
"\n",
- "The code here outlines some of the elements we could include in the analysis. Feel free to extend upon this."
+ "The code here outlines some of the elements we could include in the\n",
+ "analysis. Feel free to extend upon this in order to address the above\n",
+ "questions."
]
},
{
@@ -1306,20 +1308,26 @@
},
"outputs": [],
"source": [
- "#Now we do an SVD\n",
- "U, s, V = np.linalg.svd(X_centered)\n",
- "c1 = V.T[:, 0]\n",
- "c2 = V.T[:, 1]\n",
- "W2 = V.T[:, :2]\n",
- "X2D = X_centered.dot(W2)\n",
- "print(X2D)\n",
+ "# diagonalize and obtain eigenvalues, not necessarily sorted\n",
+ "EigValues, EigVectors = np.linalg.eig(Cov)\n",
+ "# sort eigenvectors and eigenvalues\n",
+ "#permute = EigValues.argsort()\n",
+ "#EigValues = EigValues[permute]\n",
+ "#EigVectors = EigVectors[:,permute]\n",
+ "print(\"Eigenvalues of Covariance matrix\")\n",
+ "for i in range(2):\n",
+ " print(EigValues[i])\n",
+ "FirstEigvector = EigVectors[:,0]\n",
+ "SecondEigvector = EigVectors[:,1]\n",
+ "print(\"First eigenvector\")\n",
+ "print(FirstEigvector)\n",
+ "print(\"Second eigenvector\")\n",
+ "print(SecondEigvector)\n",
"#thereafter we do a PCA with Scikit-learn\n",
"from sklearn.decomposition import PCA\n",
"pca = PCA(n_components = 2)\n",
"X2Dsl = pca.fit_transform(X)\n",
- "print(\"Check that we get the same\")\n",
- "print(X2D-X2Dsl)\n",
- "\n",
+ "print(\"Eigenvector of largest eigenvalue\")\n",
"print(pca.components_.T[:, 0])"
]
},
@@ -1327,6 +1335,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
+ "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, based on the above, to address the questions above. \n",
+ "\n",
"## Classical PCA Theorem\n",
"\n",
"We assume now that we have a design matrix $\\boldsymbol{X}$ which has been\n",
diff --git a/doc/pub/DimRed/ipynb/ipynb-DimRed-src.tar.gz b/doc/pub/DimRed/ipynb/ipynb-DimRed-src.tar.gz
index 603419a5e..88a78300f 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 6c4542cb3..c79cb2584 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 0ad06f689..0b7bc1eb0 100644
--- a/doc/src/DimRed/DimRed.do.txt
+++ b/doc/src/DimRed/DimRed.do.txt
@@ -782,7 +782,7 @@ Note that the function _multivariate_ returns also the covariance discussed abov
import numpy as np
import pandas as pd
from IPython.display import display
-n = 100
+n = 10000
mean = (-1, 2)
cov = [[4, 2], [2, 2]]
X = np.random.multivariate_normal(mean, cov, n)
@@ -858,14 +858,14 @@ plt.show()
!ec
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 ===
Now we are ready to solve for the principal components! To do so we
-diagonalize the sample covariance matrix $\Sigma_n$. We can use the
+diagonalize the sample covariance matrix $\Sigma$. We can use the
function _np.linalg.eig_ to do so. It will return the eigenvalues and
-eigenvectors of $\Sigma_n$. Once we have these we can perform the
+eigenvectors of $\Sigma$. Once we have these we can perform the
following tasks:
* We compute the percentage of the total variance captured by the first principal component
@@ -883,27 +883,35 @@ 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_.
-The code here outlines some of the elements we could include in the analysis. Feel free to extend upon this.
+The code here outlines some of the elements we could include in the
+analysis. Feel free to extend upon this in order to address the above
+questions.
+
!bc pycod
-#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)
+# diagonalize and obtain eigenvalues, not necessarily sorted
+EigValues, EigVectors = np.linalg.eig(Cov)
+# sort eigenvectors and eigenvalues
+#permute = EigValues.argsort()
+#EigValues = EigValues[permute]
+#EigVectors = EigVectors[:,permute]
+print("Eigenvalues of Covariance matrix")
+for i in range(2):
+ print(EigValues[i])
+FirstEigvector = EigVectors[:,0]
+SecondEigvector = EigVectors[:,1]
+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("Check that we get the same")
-print(X2D-X2Dsl)
-
+print("Eigenvector of largest eigenvalue")
print(pca.components_.T[:, 0])
-
!ec
-
+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, based on the above, to address the questions above.
!split
===== Classical PCA Theorem =====
diff --git a/doc/src/DimRed/Programs/PCAsimple.py b/doc/src/DimRed/Programs/PCAsimple.py
index 9e7716348..c23d51e47 100644
--- a/doc/src/DimRed/Programs/PCAsimple.py
+++ b/doc/src/DimRed/Programs/PCAsimple.py
@@ -24,13 +24,9 @@ print(np.cov(X_centered.T))
x = X_centered[:,[0]]
y = X_centered[:,[1]]
Cov = np.zeros((2,2))
-cov_xy = np.sum(x.T@y)/(n-1.0)
-cov_xx = np.sum(x.T@x)/(n-1.0)
-cov_yy = np.sum(y.T@y)/(n-1.0)
-
-Cov[0,0]= cov_xx
-Cov[1,1]= cov_yy
-Cov[0,1]= cov_xy
+Cov[0,1] = np.sum(x.T@y)/(n-1.0)
+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)
@@ -39,24 +35,27 @@ plt.plot(x, y, 'x')
plt.axis('equal')
plt.show()
-
-
-
-"""
-#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)
+# diagonalize and obtain eigenvalues, not necessarily sorted
+EigValues, EigVectors = np.linalg.eig(Cov)
+# sort eigenvectors and eigenvalues
+#permute = EigValues.argsort()
+#EigValues = EigValues[permute]
+#EigVectors = EigVectors[:,permute]
+print("Eigenvalues of Covariance matrix")
+for i in range(2):
+ print(EigValues[i])
+FirstEigvector = EigVectors[:,0]
+SecondEigvector = EigVectors[:,1]
+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("Check that we get the same")
-print(X2D-X2Dsl)
-
+print("Eigenvector of largest eigenvalue")
print(pca.components_.T[:, 0])
-"""
+