PCA特征降维

特征降维是无监督学习的另一个应用,目的有两点:其一就是我们经常会在时间项目中面对特征维度非常高的训练样本,二往往又无法借助自己的领域知识人工构建有效特征;其二就是在数据表现方面,我们无法用肉眼观测超过三个维度的特征。因此降低维度不仅仅重构了有效的低维度特征向量,同时也为数据展现提供了可能。在特征降低维度的算法中,主成分分析是最为经典和实用的特征降低维度的方法,特别在辅助图像识别方面有突出的表现。
例子
import numpy as np;
import pandas as pd;

digits_train = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/optdigits/optdigits.tra', header = None)
digits_test = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/optdigits/optdigits.tes', header = None)

X_digits = digits_train[np.arange(64)]
y_digits = digits_train[64]

from sklearn.decomposition import PCA

estimator = PCA(n_components = 2)
X_pca = estimator.fit_transform(X_digits)

from matplotlib import pyplot as plt;

def plot_pca_scatter():
colors = ['black', 'blue', 'purple', 'yellow', 'white', 'red', 'lime', 'cyan', 'orange', 'gray']
for i in xrange(len(colors)):
px = X_pca[:, 0][y_digits.as_matrix() == i]
py = X_pca[:, 1][y_digits.as_matrix() == i]
plt.scatter(px, py, c = colors[i])

plt.legend(np.arange(0, 10).astype(str))
plt.xlabel('Fitrst Princlple Component')
plt.ylabel('Second Principle Component')
plt.show()

plot_pca_scatter()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值