私はmlab.PCA
クラスは、あなたが何をしたいのために適切であるとは思いません。このように、これは固有ベクトルになる
def center(self, x):
'center the data using the mean and sigma from training set a'
return (x - self.mu)/self.sigma
、pca.Wt
:
a = self.center(a)
U, s, Vh = np.linalg.svd(a, full_matrices=False)
center
方法はsigma
によって分割:
[[-0.70710678 -0.70710678]
[-0.70710678 0.70710678]]
特に、
PCA
クラスは、固有ベクトルを求める前にデータを再スケール
これらは垂直ですが、元のデータの主軸には直接関係しません。それらはマッサージされたデータに関して主軸です。
おそらく(mlab.PCA
クラスを使用せずに)あなたが直接欲しいものをコーディングする方が簡単かもしれません:
import numpy as np
import matplotlib.pyplot as plt
N = 1000
xTrue = np.linspace(0, 1000, N)
yTrue = 3 * xTrue
xData = xTrue + np.random.normal(0, 100, N)
yData = yTrue + np.random.normal(0, 100, N)
xData = np.reshape(xData, (N, 1))
yData = np.reshape(yData, (N, 1))
data = np.hstack((xData, yData))
mu = data.mean(axis=0)
data = data - mu
# data = (data - mu)/data.std(axis=0) # Uncommenting this reproduces mlab.PCA results
eigenvectors, eigenvalues, V = np.linalg.svd(data.T, full_matrices=False)
projected_data = np.dot(data, eigenvectors)
sigma = projected_data.std(axis=0).mean()
print(eigenvectors)
fig, ax = plt.subplots()
ax.scatter(xData, yData)
for axis in eigenvectors:
start, end = mu, mu + sigma * axis
ax.annotate(
'', xy=end, xycoords='data',
xytext=start, textcoords='data',
arrowprops=dict(facecolor='red', width=2.0))
ax.set_aspect('equal')
plt.show()
素晴らしい、ありがとう。それが私が探していたものです。 – Tyrax
1.618定数の意味はなんですか?それはどこから来ますか? – joaquin
@joaquin:そのほぼ[黄金比](http://en.wikipedia.org/wiki/Golden_ratio)。もちろん、好きな定数を選ぶこともできますが、それはよく見えます(http://en.wikipedia.org/wiki/Golden_ratio#Painting)。 – unutbu