Следующий шаг - визуализировать, где эти собственные эйгенвекторы прогнозируются в на DataSet (на PC1 vs. vs. верно?). < /p>
Как построить [PC1, PC2, PC3] Собственные векторы на 3D -графике набора данных о сниженном размере? Я не уверен, почему мой первый собственный вектор имеет более короткую длину. Должен ли я умножить на собственное значение? />https://plot.ly/ipython-notebooks/princ ... ikit-learn (although I don't want to use plotly. I want to stick with pandas, numpy, sklearn, matplotlib, scipy, and seaborn)
I've been following this tutorial for plotting Собственные векторы это кажется довольно простым: основной пример для PCA с matplotlib, но я не могу повторить результаты с моими данными. />
Я попытался сделать свой код как можно простым, чтобы следовать другими учебниками: < /strong> < /p>
Код: Выделить всё
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
from sklearn import decomposition
import seaborn as sns; sns.set_style("whitegrid", {'axes.grid' : False})
%matplotlib inline
np.random.seed(0)
# Iris dataset
DF_data = pd.DataFrame(load_iris().data,
index = ["iris_%d" % i for i in range(load_iris().data.shape[0])],
columns = load_iris().feature_names)
Se_targets = pd.Series(load_iris().target,
index = ["iris_%d" % i for i in range(load_iris().data.shape[0])],
name = "Species")
# Scaling mean = 0, var = 1
DF_standard = pd.DataFrame(StandardScaler().fit_transform(DF_data),
index = DF_data.index,
columns = DF_data.columns)
# Sklearn for Principal Componenet Analysis
# Dims
m = DF_standard.shape[1]
K = 2
# PCA (How I tend to set it up)
M_PCA = decomposition.PCA(n_components=m)
DF_PCA = pd.DataFrame(M_PCA.fit_transform(DF_standard),
columns=["PC%d" % k for k in range(1,m + 1)]).iloc[:,:K]
# Plot the eigenvectors
#https://stackoverflow.com/questions/18299523/basic-example-for-pca-with-matplotlib
# This is where stuff gets weird...
data = DF_standard
mu = data.mean(axis=0)
eigenvectors, eigenvalues = M_PCA.components_, M_PCA.explained_variance_ #eigenvectors, eigenvalues, V = np.linalg.svd(data.T, full_matrices=False)
projected_data = DF_PCA #np.dot(data, eigenvectors)
sigma = projected_data.std(axis=0).mean()
fig, ax = plt.subplots(figsize=(10,10))
ax.scatter(projected_data["PC1"], projected_data["PC2"])
for axis, color in zip(eigenvectors[:K], ["red","green"]):
# start, end = mu, mu + sigma * axis ### leads to "ValueError: too many values to unpack (expected 2)"
# So I tried this but I don't think it's correct
start, end = (mu)[:K], (mu + sigma * axis)[:K]
ax.annotate('', xy=end,xytext=start, arrowprops=dict(facecolor=color, width=1.0))
ax.set_aspect('equal')
plt.show()
Подробнее здесь: https://stackoverflow.com/questions/379 ... sition-pca