博雅数据机器学习08
博雅数据机器学习08
PCA降维
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
%matplotlib inline
# pca降维过程
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)
# 计算第一主成分方差占比
fpc = pca.explained_variance_ratio_[0]
# 在低维空间中绘图
colors = ['green','c','orange'] # 色系选择
f,ax = plt.subplots(figsize=(6,6))
for color, label in zip(colors, y.unique()):
plt.scatter(X_pca[y == label, 0],
X_pca[y == label, 1],
color=color,
lw=2,
label=label)
plt.legend(loc="upper center")
plt.title("PCA of iris dataset")