some update of pca

This commit is contained in:
mhjensen
2020-01-01 19:57:12 +01:00
parent 6b703367e3
commit e57c0fc5e7
8 changed files with 25 additions and 24 deletions
+5 -4
View File
@@ -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_.