国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python使用三種方法實現PCA算法

2020-02-16 11:04:24
字體:
來源:轉載
供稿:網友

主成分分析,即Principal Component Analysis(PCA),是多元統(tǒng)計中的重要內容,也廣泛應用于機器學習和其它領域。它的主要作用是對高維數據進行降維。PCA把原先的n個特征用數目更少的k個特征取代,新特征是舊特征的線性組合,這些線性組合最大化樣本方差,盡量使新的k個特征互不相關。關于PCA的更多介紹,請參考:https://en.wikipedia.org/wiki/Principal_component_analysis.

主成分分析(PCA) vs 多元判別式分析(MDA)

PCA和MDA都是線性變換的方法,二者關系密切。在PCA中,我們尋找數據集中最大化方差的成分,在MDA中,我們對類間最大散布的方向更感興趣。

一句話,通過PCA,我們將整個數據集(不帶類別標簽)映射到一個子空間中,在MDA中,我們致力于找到一個能夠最好區(qū)分各類的最佳子集。粗略來講,PCA是通過尋找方差最大的軸(在一類中,因為PCA把整個數據集當做一類),在MDA中,我們還需要最大化類間散布。

在通常的模式識別問題中,MDA往往在PCA后面。

PCA的主要算法如下:

    組織數據形式,以便于模型使用; 計算樣本每個特征的平均值; 每個樣本數據減去該特征的平均值(歸一化處理); 求協(xié)方差矩陣; 找到協(xié)方差矩陣的特征值和特征向量; 對特征值和特征向量重新排列(特征值從大到小排列); 對特征值求取累計貢獻率; 對累計貢獻率按照某個特定比例,選取特征向量集的字跡合; 對原始數據(第三步后)。

其中協(xié)方差矩陣的分解可以通過按對稱矩陣的特征向量來,也可以通過分解矩陣的SVD來實現,而在Scikit-learn中,也是采用SVD來實現PCA算法的。

本文將用三種方法來實現PCA算法,一種是原始算法,即上面所描述的算法過程,具體的計算方法和過程,可以參考:A tutorial on Principal Components Analysis, Lindsay I Smith. 一種是帶SVD的原始算法,在Python的Numpy模塊中已經實現了SVD算法,并且將特征值從大從小排列,省去了對特征值和特征向量重新排列這一步。最后一種方法是用Python的Scikit-learn模塊實現的PCA類直接進行計算,來驗證前面兩種方法的正確性。

用以上三種方法來實現PCA的完整的Python如下:

import numpy as npfrom sklearn.decomposition import PCAimport sys#returns choosing how many main factorsdef index_lst(lst, component=0, rate=0):  #component: numbers of main factors  #rate: rate of sum(main factors)/sum(all factors)  #rate range suggest: (0.8,1)  #if you choose rate parameter, return index = 0 or less than len(lst)  if component and rate:    print('Component and rate must choose only one!')    sys.exit(0)  if not component and not rate:    print('Invalid parameter for numbers of components!')    sys.exit(0)  elif component:    print('Choosing by component, components are %s......'%component)    return component  else:    print('Choosing by rate, rate is %s ......'%rate)    for i in range(1, len(lst)):      if sum(lst[:i])/sum(lst) >= rate:        return i    return 0def main():  # test data  mat = [[-1,-1,0,2,1],[2,0,0,-1,-1],[2,0,1,1,0]]    # simple transform of test data  Mat = np.array(mat, dtype='float64')  print('Before PCA transforMation, data is:/n', Mat)  print('/nMethod 1: PCA by original algorithm:')  p,n = np.shape(Mat) # shape of Mat   t = np.mean(Mat, 0) # mean of each column    # substract the mean of each column  for i in range(p):    for j in range(n):      Mat[i,j] = float(Mat[i,j]-t[j])        # covariance Matrix  cov_Mat = np.dot(Mat.T, Mat)/(p-1)    # PCA by original algorithm  # eigvalues and eigenvectors of covariance Matrix with eigvalues descending  U,V = np.linalg.eigh(cov_Mat)   # Rearrange the eigenvectors and eigenvalues  U = U[::-1]  for i in range(n):    V[i,:] = V[i,:][::-1]  # choose eigenvalue by component or rate, not both of them euqal to 0  Index = index_lst(U, component=2) # choose how many main factors  if Index:    v = V[:,:Index] # subset of Unitary matrix  else: # improper rate choice may return Index=0    print('Invalid rate choice./nPlease adjust the rate.')    print('Rate distribute follows:')    print([sum(U[:i])/sum(U) for i in range(1, len(U)+1)])    sys.exit(0)  # data transformation  T1 = np.dot(Mat, v)  # print the transformed data  print('We choose %d main factors.'%Index)  print('After PCA transformation, data becomes:/n',T1)    # PCA by original algorithm using SVD  print('/nMethod 2: PCA by original algorithm using SVD:')  # u: Unitary matrix, eigenvectors in columns   # d: list of the singular values, sorted in descending order  u,d,v = np.linalg.svd(cov_Mat)  Index = index_lst(d, rate=0.95) # choose how many main factors  T2 = np.dot(Mat, u[:,:Index]) # transformed data  print('We choose %d main factors.'%Index)  print('After PCA transformation, data becomes:/n',T2)    # PCA by Scikit-learn  pca = PCA(n_components=2) # n_components can be integer or float in (0,1)  pca.fit(mat) # fit the model  print('/nMethod 3: PCA by Scikit-learn:')  print('After PCA transformation, data becomes:')  print(pca.fit_transform(mat)) # transformed data      main()            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 武鸣县| 通渭县| 龙州县| 山东| 兖州市| 苗栗市| 罗定市| 辉南县| 瓦房店市| 深州市| 花垣县| 江津市| 卢氏县| 丰顺县| 东山县| 寻乌县| 丰镇市| 曲阜市| 澄迈县| 临高县| 镇江市| 孝义市| 顺平县| 鄂温| 樟树市| 阿拉善左旗| 陈巴尔虎旗| 酉阳| 当涂县| 日喀则市| 惠东县| 富宁县| 阿合奇县| 洛扎县| 河北区| 西贡区| 西宁市| 济源市| 五家渠市| 苍溪县| 新郑市|