Note: This article is available in Chinese only. 本文暂无英文版本。
View original
先记录一下PCA实战需要用到的安装包(arch下,python2环境)#
python2-scikit-learn#
python2-numpy
python2-pandas
python2-matplotlib
python2-seaborn
几个和科学计算数据分析有关的重要的python库:Numpy、Matplotlib ,pandas
(之前数字图像处理课程都接触过了orz)
其中matplotlib 主要用于图像绘制
sklearn 是用于机器学习的python 模块
Seaborn也是用于图像绘制
str.fomat() 是 python2语法
format中的变量会按照str中{} 出现的顺序替换
1import matplotlib.pyplot as plt
2import numpy as np
3import pandas as pd
4from sklearn.datasets import fetch_mldata
5from sklearn.decomposition import PCA
6import seaborn as sns
7mnist = fetch_mldata("MNIST original")
8
9X = mnist.data / 255.0
10y = mnist.target
11
12#print X.shape, y.shape
13
14
15feat_cols = [ 'pixel'+str(i) for i in range(X.shape[1]) ]
16df = pd.DataFrame(X,columns=feat_cols) # transform into some pandas DS named DataFrame
17df['label'] = y # add a column named 'label',valued by variable y
18df['label'] = df['label'].apply(lambda i: str(i)) # df.apply (some funciton)
19X, y = None, None
20#print 'Size of the dataframe: {}' .format(df.shape) # str.format() , '{}' is replaced by some value
21
22# Plot the graph
23#print df
24
25#rndperm = np.random.permutation(df.shape[0])
26#plt.gray()
27#fig = plt.figure( figsize=(16,7) )
28#for i in range(0,30):
29# ax = fig.add_subplot(3,10,i+1, title='Digit: ' + str(df.loc[rndperm[i],'label']) )
30# ax.matshow(df.loc[rndperm[i],feat_cols].values.reshape((28,28)).astype(float))
31#plt.show()
32
33#print df
34
35n_components_pca = 3
36pca = PCA(n_components=n_components_pca)
37pca_result = pca.fit_transform(df[feat_cols].values)
38
39print 'kkkkk?'
40df['pca-one'] = pca_result[:,0]
41df['pca-two'] = pca_result[:,1]
42df['pca-three'] = pca_result[:,2]
43
44print 'Explained variation per principal component: {}'.format(pca.explained_variance_ratio_)
45
46
47pca_var_explained_df = pd.DataFrame({'principal component':np.arange(1,n_components_pca+1),
48 'variance_explained':pca.explained_variance_ratio_})
49print pca_var_explained_df.sum()
50
51
52ax = sns.barplot(x='principal component',
53 y='variance_explained',
54 data=pca_var_explained_df,
55 palette="Set1")
56ax.set_title('PCA - Variance explained')
57plt.show()PCA+kmeans 时间对比:
代码:
1import time
2import numpy as np
3from sklearn.cluster import KMeans
4from sklearn.decomposition import PCA
5vec_num=120
6data = np.random.rand(10000, vec_num) #生成一个随机数据,样本大小为10000, 特征数为120
7#print (data)
8
9t0 = time.clock()
10estimator = KMeans(n_clusters=4)#构造聚类器
11estimator.fit(data)#聚类
12print ("kmeans time:",time.clock()-t0)
13inertia = estimator.inertia_ # 某种度量,比如距离平方和,用来check k-means算法的效果
14print ("sum:",inertia/vec_num)
15
16com_num=10
17pca=PCA(n_components=com_num)
18newdata = pca.fit_transform(data)
19t0 = time.clock()
20estimator.fit(newdata)
21print("kmeans time after PCA:",time.clock()-t0)
22inertia = estimator.inertia_
23print ("sum after PCA",inertia/com_num)