sklearn PCA实践
PCA无需样本标签,属于无监督学习降维;LDA需要样本标签,属于有监督学习降维。
[url]https://blog.csdn.net/ainimao6666/article/details/64933677[/url]
[url]https://blog.csdn.net/Huangyi_906/article/details/76438885[/url]
class sklearn.decomposition.PCA(n_components=None, copy=True, whiten=False, svd_solver=’auto’, tol=0.0, iterated_power=’auto’, random_state=None)
其中参数:
n_components=None:指定降维后的维数。如果给定数在(0,1)之间,则为降维后占原维数的百分比。默认自动选择。
属性:
components_ :主成分组数
explained_variance_ratio_:每个主成分占方差比例
n_components_ :一个整数,指示主成分有多少个元素。
方法:
fit(x):训练模型
transform(x): 执行降维
fit_transform(x): 训练并降维
inverse_transform(x): 逆向操作,把降维的数据逆向转换回原来数据。
例子
[url]https://blog.csdn.net/puredreammer/article/details/52255025[/url]
[url]https://www.jianshu.com/p/8642d5ea5389[/url]
[img]http://dl2.iteye.com/upload/attachment/0130/7987/f886532d-6e86-35ec-a433-52e2cb279ec9.png[/img]
PCA无需样本标签,属于无监督学习降维;LDA需要样本标签,属于有监督学习降维。
[url]https://blog.csdn.net/ainimao6666/article/details/64933677[/url]
[url]https://blog.csdn.net/Huangyi_906/article/details/76438885[/url]
class sklearn.decomposition.PCA(n_components=None, copy=True, whiten=False, svd_solver=’auto’, tol=0.0, iterated_power=’auto’, random_state=None)
其中参数:
n_components=None:指定降维后的维数。如果给定数在(0,1)之间,则为降维后占原维数的百分比。默认自动选择。
属性:
components_ :主成分组数
explained_variance_ratio_:每个主成分占方差比例
n_components_ :一个整数,指示主成分有多少个元素。
方法:
fit(x):训练模型
transform(x): 执行降维
fit_transform(x): 训练并降维
inverse_transform(x): 逆向操作,把降维的数据逆向转换回原来数据。
例子
[url]https://blog.csdn.net/puredreammer/article/details/52255025[/url]
[url]https://www.jianshu.com/p/8642d5ea5389[/url]
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_classification
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from mpl_toolkits.mplot3d import Axes3D
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
pca = PCA(n_components=2)#n_components表示结果取几维
pca.fit(X) #训练模型
print(pca.explained_variance_ratio_) #每维对一致性的比率,越高表示这个维度更能表示降维后的效果[0.99244289 0.00755711]
X2 = pca.transform(X) #执行降维
print('X2==')
print(X2)
# [[ 1.38340578 0.2935787 ]
# [ 2.22189802 -0.25133484]
# [ 3.6053038 0.04224385]
# [-1.38340578 -0.2935787 ]
# [-2.22189802 0.25133484]
# [-3.6053038 -0.04224385]]
X4 = pca.fit_transform(X)#训练并降维
print('X4==')
print(X4)
# [[ 1.38340578 0.2935787 ]
# [ 2.22189802 -0.25133484]
# [ 3.6053038 0.04224385]
# [-1.38340578 -0.2935787 ]
# [-2.22189802 0.25133484]
# [-3.6053038 -0.04224385]]
X3 = pca.inverse_transform(X2)#逆向操作,把降维的数据逆向转换回原来数据。
print('X3==')
print(X3)
# [[-1. -1.]
# [-2. -1.]
# [-3. -2.]
# [ 1. 1.]
# [ 2. 1.]
# [ 3. 2.]]
pca = PCA(n_components=1)#降为一维
X5 = pca.fit_transform(X)
print('X5==')
print(X5)
# [[ 1.38340578]
# [ 2.22189802]
# [ 3.6053038 ]
# [-1.38340578]
# [-2.22189802]
# [-3.6053038 ]]
X6 = pca.inverse_transform(X5)#逆向操作,把降维的数据逆向转换回原来数据。
print('X6==')
print(X6)
# [[-1.15997501 -0.75383654]
# [-1.86304424 -1.21074232]
# [-3.02301925 -1.96457886]
# [ 1.15997501 0.75383654]
# [ 1.86304424 1.21074232]
# [ 3.02301925 1.96457886]]
#==============================================
xSrc = X[:,0];
ySrc = X[:,1];
print (xSrc)
# [-1 -2 -3 1 2 3]
print (ySrc)
# [-1 -1 -2 1 1 2]
plt.scatter(xSrc, ySrc,marker='o',c='r',alpha=0.5) #原坐标点的图
xInverse = X6[:,0];
yInverse = X6[:,1];
plt.scatter(xInverse, yInverse,marker='o',c='b',alpha=0.5) #降为一维后再变为二维的图
# x2Inverse = X4[:,0];
# y2Inverse = X4[:,1];
# plt.scatter(x2Inverse, y2Inverse,marker='o',c='g',alpha=0.5) #降为二维后再变为二维的图
plt.show()
[img]http://dl2.iteye.com/upload/attachment/0130/7987/f886532d-6e86-35ec-a433-52e2cb279ec9.png[/img]