当記事は「統計学実践ワークブック(学術図書出版社)」の読解サポートにあたってChapter.22の「主成分分析」に関して演習問題を中心に解説を行います。実際に分析などを行うにあたってよく出てくる手法なので、演習を通して抑えておくと良いと思われました。
Contents
本章のまとめ
下記などで取り扱いを行なった。
https://www.hello-statisticians.com/explain-terms-cat/pca1.html
導出で用いられる数学の重要事項
二乗和の変形
「主成分分析」の「二次形式とベクトル」で詳しく取り扱った。
ベクトルでの微分
「主成分分析」の「スカラー関数のベクトルでの微分」で詳しく取り扱った。
主成分分析の導出・固有値の解釈
「主成分分析」の「主成分分析の導出」で詳しく取り扱った。
演習問題解説
例22.1
下記を実行することで、$x_1$の分散、$x_1,x_2$の共分散、相関係数の計算を行うことができる。
import numpy as np
X = np.array([[2., 2., 3., 1.], [9., 8., 10., 9.], [8., 3., 2., 7.], [7., 1., 3., 8.], [2., 9., 8., 2.], [5., 4., 5., 5.]])
Var_x1 = np.dot(X[:,0]-np.mean(X[:,0]),X[:,0]-np.mean(X[:,0]))/5.
Var_x2 = np.dot(X[:,1]-np.mean(X[:,1]),X[:,1]-np.mean(X[:,1]))/5.
Cov_x1_x2 = np.dot(X[:,0]-np.mean(X[:,0]),X[:,1]-np.mean(X[:,1]))/5.
print("Variance x1: {:.2f}".format(Var_x1))
print("Covariance x1-x2: {:.2f}".format(Cov_x1_x2))
print("Correlation x1-x2: {:.3f}".format(Cov_x1_x2/(Var_x1*Var_x2)**0.5))
・実行結果
> print("Variance x1: {:.2f}".format(Var_x1))
Variance x1: 9.10
> print("Covariance x1-x2: {:.2f}".format(Cov_x1_x2))
Covariance x1-x2: -0.70
> print("Correlation x1-x2: {:.3f}".format(Cov_x1_x2/(Var_x1*Var_x2)**0.5))
Correlation x1-x2: -0.071
また、標本分散共分散行列、標本相関行列は下記を実行することで得られる。
import numpy as np
X = np.array([[2., 2., 3., 1.], [9., 8., 10., 9.], [8., 3., 2., 7.], [7., 1., 3., 8.], [2., 9., 8., 2.], [5., 4., 5., 5.]])
X_ = X - np.mean(X,axis=0)
Cov_mat = np.dot(X_.T,X_)/5.
Variance_row = np.repeat(np.diag(Cov_mat)**0.5, 4).reshape([4,4])
Variance_col = np.repeat(np.diag(Cov_mat)**0.5, 4).reshape([4,4]).T
Cor_mat = Cov_mat/(Variance_row*Variance_col)
print("・Covariance matrix")
print(Cov_mat)
print("・Correlation matrix")
print(Cor_mat)
・実行結果
・Covariance matrix
[[ 9.1 -0.7 0.7 9.6 ]
[ -0.7 10.7 9.5 -0.6 ]
[ 0.7 9.5 10.16666667 1.33333333]
[ 9.6 -0.6 1.33333333 10.66666667]]
・Correlation matrix
[[ 1. -0.07093906 0.07277598 0.97439753]
[-0.07093906 1. 0.91084069 -0.05616231]
[ 0.07277598 0.91084069 1. 0.12803688]
[ 0.97439753 -0.05616231 0.12803688 1. ]]
例22.2
$[1]$
下記を実行することで、第$1$主成分、第$2$主成分の寄与率を計算することができる。
import numpy as np
lamb = np.array([20.2, 19.4, 0.85, 0.18])
print("contribution ratio of 1st principal component: {}".format(lamb[0]/np.sum(lamb)))
print("contribution ratio of 2nd principal component: {}".format(lamb[1]/np.sum(lamb)))
print("sum of 1st and 2nd: {}".format(lamb[0]/np.sum(lamb)+lamb[1]/np.sum(lamb)))
・実行結果
> print("contribution ratio of 1st principal component: {}".format(lamb[0]/np.sum(lamb)))
contribution ratio of 1st principal component: 0.497169579129
> print("contribution ratio of 2nd principal component: {}".format(lamb[1]/np.sum(lamb)))
contribution ratio of 2nd principal component: 0.477479694807
> print("sum of 1st and 2nd: {}".format(lamb[0]/np.sum(lamb)+lamb[1]/np.sum(lamb)))
sum of 1st and 2nd: 0.974649273936
上記の結果より、第$2$主成分までの寄与率で$97.5$%を占めることが読み取れるので、全体の結果を第$2$主成分までで要約することは合理的である。
$[2]$
下記のように計算できる。
$$
\large
\begin{align}
\frac{\sqrt{\lambda_{2}}u_{1,2}}{\sqrt{s_{1,1}}} = \frac{\sqrt{19.4} \times 0.564}{9.1} = 0.8234…
\end{align}
$$
問22.1
$[1]$
下記を実行することで計算を行うことができる。
import numpy as np
print(np.sqrt(2.26)*(-0.497))
print(np.sqrt(1.5)*(0.217))
・実行結果
> print(np.sqrt(2.26)*(-0.497))
-0.747154830005
> print(np.sqrt(1.5)*(0.217))
0.265769637092
$[2]$
累積寄与率を確認すると、第$1,2$主成分で全体の$54$%、第$3,4$主成分までで全体の$83$%の情報を取れることがわかる。詳細の考察に関してはワークブックにあるのでここでは省略する。
$[3]$
$A$が該当する。詳細の考察はワークブックがわかりやすいのでここでは省略する。
参考
・準1級関連まとめ
https://www.hello-statisticians.com/toukeikentei-semi1
・主成分分析の導出まとめ
https://www.hello-statisticians.com/explain-terms-cat/pca1.html
[…] 「統計学実践ワークブック」 演習問題 Ch.22 「主成分分析」https://www.hello-statisticians.com/explain-books-cat/stat_workbook/stat_workbook_ch22.html […]