<jupyter_start><jupyter_text>eigen vector共分散行列と、固有値、固有ベクトルの関係。 ライブラリのインポート<jupyter_code>%matplotlib inline
import numpy as np
import math, random
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>サンプルの生成<jupyter_code>pos = []         #サンプルの位置
sigma_x = 1.0    #xの標準偏差の設定
sigma_y = 0.5    #yの標準偏差の設定
num = 500        #サンプルの数

#設定した標準偏差を元にサンプルを生成
for i in range(num):
    pos.append(np.array([random.gauss(0, sigma_x), random.gauss(0, sigma_y)]))
    
#サンプルの描画関数
def draw(pos):
    fig = plt.figure(i, figsize=(8,8))
    sp = fig.add_subplot(111,aspect='equal')
    sp.set_xlim(-4.0,4.0)
    sp.set_ylim(-4.0,4.0)
    
    xs = [e[0] for e in pos]
    ys = [e[1] for e in pos]
    
    plt.scatter(xs,ys,s=30,marker="o",label="landmarks",alpha=0.3,color="skyblue")
draw(pos)<jupyter_output><empty_output><jupyter_text>平均値の算出<jupyter_code>mean_x = 0
mean_y = 0

for p in pos:
    mean_x += p[0] / num
    mean_y += p[1] / num
    
print("mean x =\n", mean_x, "\nmean y =\n", mean_y)<jupyter_output>mean x =
 -0.011988315949 
mean y =
 -0.00722422657548<jupyter_text>分散共分散行列の算出<jupyter_code>v_x = 0
v_y = 0
sigma_xy = 0

for p in pos:
    v_x += (mean_x - p[0]) ** 2 / num
    v_y += (mean_y - p[1]) ** 2 / num
    sigma_xy += (mean_x - p[0]) * (mean_y - p[1]) / num
    
cov = np.array([[v_x, sigma_xy],
                [sigma_xy, v_y]])

print("cov =\n",cov)<jupyter_output>cov =
 [[ 1.10599591 -0.04019545]
 [-0.04019545  0.26307484]]<jupyter_text>固有値、固有ベクトルの算出<jupyter_code>eigen = np.linalg.eig(cov)

eigen_val = eigen[0]

eigen_vec = eigen[1]

print("eigen value =\n",eigen_val,"\neigen vectors =\n",eigen_vec)
eigen_v = eigen[1:2][0]

v1 = math.sqrt(eigen_val[0]) * eigen_vec[0:2,0:1]
v2 = math.sqrt(eigen_val[1]) * eigen_vec[0:2,1:2]

print("vector_1 =\n", v1)
print("vector_2 =\n", v2)<jupyter_output>vector_1 =
 [[ 1.05138293]
 [-0.05002266]]
vector_2 =
 [[ 0.02428679]
 [ 0.5104631 ]]<jupyter_text>固有ベクトルの描画<jupyter_code>draw(pos)
v1_x, v1_y = v1 * 2
v2_x, v2_y = v2 * 2

plt.quiver(mean_x,mean_y,v1_x,v1_y,angles='xy',scale_units='xy',scale=1,color="blue",alpha=0.5)
plt.quiver(mean_x,mean_y,v2_x,v2_y,angles='xy',scale_units='xy',scale=1,color="red",alpha=0.5)<jupyter_output><empty_output><jupyter_text>サンプルの回転、移動<jupyter_code>t = math.pi / 6
c = math.cos(t)
s = math.sin(t)

rot = np.array([[c, -s],
                [s, c]])

for i in range(len(pos)):
    pos[i] = rot.dot(pos[i])
    pos[i] += np.array([1,1])
    
draw(pos)<jupyter_output><empty_output><jupyter_text>平均値、分散共分散行列の算出<jupyter_code>mean_x = 0
mean_y = 0

for p in pos:
    mean_x += p[0] / num
    mean_y += p[1] / num
    
print("mean x =\n", mean_x, "\nmean y =\n", mean_y)

v_x = 0
v_y = 0
sigma_xy = 0

for p in pos:
    v_x += (mean_x - p[0]) ** 2 / num
    v_y += (mean_y - p[1]) ** 2 / num
    sigma_xy += (mean_x - p[0]) * (mean_y - p[1]) / num
    
cov = np.array([[v_x, sigma_xy],
                [sigma_xy, v_y]])

print("cov =\n",cov)<jupyter_output>mean x =
 0.993229927127 
mean y =
 0.987749478288
cov =
 [[ 0.93007593  0.3448978 ]
 [ 0.3448978   0.43899483]]<jupyter_text>固有値、固有ベクトルの算出<jupyter_code>eigen = np.linalg.eig(cov)

eigen_val = eigen[0]

eigen_vec = eigen[1]

print("eigen value =\n",eigen_val,"\neigen vectors =\n",eigen_vec)
eigen_v = eigen[1:2][0]

v1 = math.sqrt(eigen_val[0]) * eigen_vec[0:2,0:1]
v2 = math.sqrt(eigen_val[1]) * eigen_vec[0:2,1:2]

print("vector_1 =\n", v1)
print("vector_2 =\n", v2)<jupyter_output>vector_1 =
 [[ 0.93553565]
 [ 0.48237057]]
vector_2 =
 [[-0.23419857]
 [ 0.45421741]]<jupyter_text>固有ベクトルの描画<jupyter_code>draw(pos)
v1_x, v1_y = v1 * 2
v2_x, v2_y = v2 * 2

plt.quiver(mean_x,mean_y,v1_x,v1_y,angles='xy',scale_units='xy',scale=1,color="blue",alpha=0.5)
plt.quiver(mean_x,mean_y,v2_x,v2_y,angles='xy',scale_units='xy',scale=1,color="red",alpha=0.5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18771153345482947, 'fraction_numerical': 0.08695652173913043, 'mean_word_length': 4.270233196159122, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '629019', 'license': 'MIT', 'n_tokens_mistral': 2123, 'n_tokens_neox': 2008, 'n_words': 283, 'path': 'eigen_vector.ipynb', 'repo_name': 'Kuwamai/probrobo_note', 'size': '3841'}