some update of pca
This commit is contained in:
@@ -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 <b>Scikit-Learn</b> discussed above.
|
||||
The following code elements perform these operations using <b>pandas</b> or your own functionality for doing so.
|
||||
The following code elements perform these operations using <b>pandas</b> or using our own functionality for doing so. The latter, using <b>numpy</b> is rather simply through the <b>mean()</b> function.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
@@ -277,7 +277,7 @@ Alternatively, you could also have used the functions we discussed earlier for s
|
||||
That is, we could have used the <b>StandardScaler</b> function in <b>Scikit-Learn</b>, 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).
|
||||
matrix). You would then not get the same results, since we divide by the variance. For diagonal covariance matrix elements will then be one, while the non-diagonal ones will be divided by \( 2\sqrt{2} \) for our specific case.
|
||||
|
||||
<h3 id="___sec19" class="anchor">Compute the sample covariance </h3>
|
||||
|
||||
@@ -319,11 +319,11 @@ following tasks:
|
||||
|
||||
$$
|
||||
\begin{equation*}
|
||||
x_i \approx \tilde{x}_i := \mu_n + \langle x_i, v_0 \rangle v_0
|
||||
x_i \approx \tilde{x}_i = \mu_n + \langle x_i, v_0 \rangle v_0
|
||||
\end{equation*}
|
||||
$$
|
||||
|
||||
where \( v_0 \) is the first principal component. What do you observe?
|
||||
where \( v_0 \) is the first principal component.
|
||||
|
||||
<p>
|
||||
Collecting all these steps we can write our own PCA function and
|
||||
|
||||
@@ -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 <b>Scikit-Learn</b> discussed above.
|
||||
The following code elements perform these operations using <b>pandas</b> or your own functionality for doing so.
|
||||
The following code elements perform these operations using <b>pandas</b> or using our own functionality for doing so. The latter, using <b>numpy</b> is rather simply through the <b>mean()</b> function.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1087,7 +1087,7 @@ Alternatively, you could also have used the functions we discussed earlier for s
|
||||
That is, we could have used the <b>StandardScaler</b> function in <b>Scikit-Learn</b>, 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).
|
||||
matrix). You would then not get the same results, since we divide by the variance. For diagonal covariance matrix elements will then be one, while the non-diagonal ones will be divided by \( 2\sqrt{2} \) for our specific case.
|
||||
|
||||
<h3 id="___sec19">Compute the sample covariance </h3>
|
||||
|
||||
@@ -1131,12 +1131,12 @@ following tasks:
|
||||
<p> <br>
|
||||
$$
|
||||
\begin{equation*}
|
||||
x_i \approx \tilde{x}_i := \mu_n + \langle x_i, v_0 \rangle v_0
|
||||
x_i \approx \tilde{x}_i = \mu_n + \langle x_i, v_0 \rangle v_0
|
||||
\end{equation*}
|
||||
$$
|
||||
<p> <br>
|
||||
|
||||
where \( v_0 \) is the first principal component. What do you observe?
|
||||
where \( v_0 \) is the first principal component.
|
||||
|
||||
<p>
|
||||
Collecting all these steps we can write our own PCA function and
|
||||
|
||||
@@ -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 <b>Scikit-Learn</b> discussed above.
|
||||
The following code elements perform these operations using <b>pandas</b> or your own functionality for doing so.
|
||||
The following code elements perform these operations using <b>pandas</b> or using our own functionality for doing so. The latter, using <b>numpy</b> is rather simply through the <b>mean()</b> function.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1063,7 +1063,7 @@ Alternatively, you could also have used the functions we discussed earlier for s
|
||||
That is, we could have used the <b>StandardScaler</b> function in <b>Scikit-Learn</b>, 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).
|
||||
matrix). You would then not get the same results, since we divide by the variance. For diagonal covariance matrix elements will then be one, while the non-diagonal ones will be divided by \( 2\sqrt{2} \) for our specific case.
|
||||
|
||||
<h3 id="___sec19">Compute the sample covariance </h3>
|
||||
|
||||
@@ -1105,11 +1105,11 @@ following tasks:
|
||||
|
||||
$$
|
||||
\begin{equation*}
|
||||
x_i \approx \tilde{x}_i := \mu_n + \langle x_i, v_0 \rangle v_0
|
||||
x_i \approx \tilde{x}_i = \mu_n + \langle x_i, v_0 \rangle v_0
|
||||
\end{equation*}
|
||||
$$
|
||||
|
||||
where \( v_0 \) is the first principal component. What do you observe?
|
||||
where \( v_0 \) is the first principal component.
|
||||
|
||||
<p>
|
||||
Collecting all these steps we can write our own PCA function and
|
||||
|
||||
@@ -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 <b>Scikit-Learn</b> discussed above.
|
||||
The following code elements perform these operations using <b>pandas</b> or your own functionality for doing so.
|
||||
The following code elements perform these operations using <b>pandas</b> or using our own functionality for doing so. The latter, using <b>numpy</b> is rather simply through the <b>mean()</b> function.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
@@ -1068,7 +1068,7 @@ Alternatively, you could also have used the functions we discussed earlier for s
|
||||
That is, we could have used the <b>StandardScaler</b> function in <b>Scikit-Learn</b>, 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).
|
||||
matrix). You would then not get the same results, since we divide by the variance. For diagonal covariance matrix elements will then be one, while the non-diagonal ones will be divided by \( 2\sqrt{2} \) for our specific case.
|
||||
|
||||
<h3 id="___sec19">Compute the sample covariance </h3>
|
||||
|
||||
@@ -1110,11 +1110,11 @@ following tasks:
|
||||
|
||||
$$
|
||||
\begin{equation*}
|
||||
x_i \approx \tilde{x}_i := \mu_n + \langle x_i, v_0 \rangle v_0
|
||||
x_i \approx \tilde{x}_i = \mu_n + \langle x_i, v_0 \rangle v_0
|
||||
\end{equation*}
|
||||
$$
|
||||
|
||||
where \( v_0 \) is the first principal component. What do you observe?
|
||||
where \( v_0 \) is the first principal component.
|
||||
|
||||
<p>
|
||||
Collecting all these steps we can write our own PCA function and
|
||||
|
||||
@@ -1161,7 +1161,7 @@
|
||||
"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.\n",
|
||||
"The following code elements perform these operations using **pandas** or your own functionality for doing so."
|
||||
"The following code elements perform these operations using **pandas** or using our own functionality for doing so. The latter, using **numpy** is rather simply through the **mean()** function."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1191,7 +1191,7 @@
|
||||
"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",
|
||||
"matrix). You would then not get the same results, since we divide by the variance. For diagonal covariance matrix elements will then be one, while the non-diagonal ones will be divided by $2\\sqrt{2}$ for our specific case.\n",
|
||||
"\n",
|
||||
"### Compute the sample covariance\n",
|
||||
"\n",
|
||||
@@ -1257,7 +1257,7 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"x_i \\approx \\tilde{x}_i := \\mu_n + \\langle x_i, v_0 \\rangle v_0\n",
|
||||
"x_i \\approx \\tilde{x}_i = \\mu_n + \\langle x_i, v_0 \\rangle v_0\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
@@ -1265,7 +1265,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"where $v_0$ is the first principal component. What do you observe?\n",
|
||||
"where $v_0$ is the first principal component. \n",
|
||||
"\n",
|
||||
"Collecting all these steps we can write our own PCA function and\n",
|
||||
"compare this with the functionality included in **Scikit-Learn**. \n",
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -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.
|
||||
The following code elements perform these operations using _pandas_ or using our own functionality for doing so. The latter, using _numpy_ is rather simply through the _mean()_ function.
|
||||
!bc pycod
|
||||
df = pd.DataFrame(X)
|
||||
# Pandas does the centering for us
|
||||
@@ -828,7 +828,7 @@ Alternatively, you could also have used the functions we discussed earlier for s
|
||||
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).
|
||||
matrix). You would then not get the same results, since we divide by the variance. For diagonal covariance matrix elements will then be one, while the non-diagonal ones will be divided by $2\sqrt{2}$ for our specific case.
|
||||
|
||||
=== Compute the sample covariance ===
|
||||
|
||||
@@ -860,12 +860,13 @@ following tasks:
|
||||
* We plot the mean centered data and lines along the first and second principal components
|
||||
* Then we project the mean centered data onto the first and second principal components, and plot the projected data.
|
||||
* Finally, we approximate the data as
|
||||
|
||||
!bt
|
||||
\begin{equation*}
|
||||
x_i \approx \tilde{x}_i := \mu_n + \langle x_i, v_0 \rangle v_0
|
||||
x_i \approx \tilde{x}_i = \mu_n + \langle x_i, v_0 \rangle v_0
|
||||
\end{equation*}
|
||||
!et
|
||||
where $v_0$ is the first principal component. What do you observe?
|
||||
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_.
|
||||
|
||||
Reference in New Issue
Block a user