1. 程式人生 > >一、降維——機器學習筆記——降維(特徵提取)

一、降維——機器學習筆記——降維(特徵提取)

目錄

2、示例

 一、為什麼要降維


維數災難:在給定精度下,準確地對某些變數的函式進行估計,所需樣本量會隨著樣本維數的增加而呈指數形式增長。
降維的意義:克服維數災難,獲取本質特徵,節省儲存空間,去除無用噪聲,實現資料視覺化


1、降維的分類

資料降維分為特徵選擇和特徵提取兩種方法,此文介紹的是特徵提取方法,即經已有特徵的某種變換獲取約簡特徵。

  • By only keeping the most relevant variables from the original dataset (this technique is called feature selection)
  • By finding a smaller set of new variables, each being a combination of the input variables, containing basically the same information as the input variables (this technique is called dimensionality reduction)

2、示例

例項一:假如現在我們只有兩個variables,為了瞭解這兩者之間的關係,可以使用散點圖將它們兩者的關係畫出:

那如果我們有100variables時,如果我們要看每個變數之間的關係,那需要100(100-1)/2 = 5000個圖,而且把它們分開來看也沒什麼意義?

示例二:假如我們現在有兩個意義相似的variables——Kg (X1) and Pound (X2),如果兩個變數都使用會存在共線性,所以在這種情況下只使用一個變數就行。可以將二維資料降維一維


二、第一部分,線性降維方法  


假設資料集取樣來自高維空間的一個全域性線性的子空間,即構成資料的各變數之間是獨立無關的。
···通過特徵的線性組合來降維
···本質上是把資料投影到低維線性子空間
···線性方法相對比較簡單且容易計算
···適用於具有全域性線性結構的資料集

1、【PCA】主成分分析


    基本思想:構造原變數的一系列線性組合形成幾個綜合指標,以去除資料的相關性,並使低維資料最大程度保持原始高維資料的方差資訊。
主成分個數的確定:
    貢獻率:第i個主成分的方差在全部方差中所佔比重,反映第i個主成分所提取的總資訊的份額。
    累計貢獻率:前k個主成分在全部方差中所佔比重
    主成分個數的確定:累計貢獻率>0.85
相關係數矩陣or協方差陣?
當涉及變數的量綱不同或取值範圍相差較大的指標時,應考慮從相關係數矩陣出發進行主成分分析;

對同度量或取值範圍相差不大的資料,從協方差陣出發.

相關係數矩陣消除了量綱的影響。


---------R實現
princomp函式
princomp(x, cor = FALSE, scores = TRUE, ...)
cor 邏輯值,該值指示是否計算應使用相關矩陣(TRUE)或協方差 (FALSE)矩陣
scores 邏輯值,該值指示是否計算主成分得分

--------返回值

loadings 是一個矩陣,每列是一個特徵向量,即原始特徵的旋轉系數
scores 所提供的資料在各個主成分上的得分

2、【LDA】判別分析

至多能把C類資料降維到C-1維子空間

---------R實現

library(MASS)
> params <- lda(y~x1+x2+x3, data=d)
##第一個引數是判別式的形式,第二個引數是用來訓練的樣本資料。lda命令執行後,會輸出構成判別式的各個係數。
> predict(params, newdata)
##使用predict命令對未分類的樣本進行判別。第一個引數是上一階段lda命令的結果,第二個引數是用來分類的樣本資料。自此,整個fisher判別過程完成。

3、【MDS】多維尺度分析

當 n 個研究物件之間的相似性(或距離)給定時,確定這些物件在低維空間中的表示,並使其儘可能與原先的相似性(或距離)“大體匹配”,使得由降維所引起的任何變形達到最小。
將研究物件在一個低維(二維或三維)的空間形象地表示出來(感知圖),簡單明瞭地說明各研究物件之間的相對關係。

-----------R示例

city<-read.csv('airline.csv',header=TRUE)
city1<-city[,-1]#這個資料集的第一列是名字,先把它去掉
for (i in 1:9)
for (j in (i+1):10)
city1[i,j]=city1[j,i] #把上三角部分補足
rownames(city1)<-colnames(city1) #再把行名加回來
city2<-as.dist(city1, diag = TRUE, upper = TRUE)#轉換為dist型別
city3<-as.matrix(city2) #轉化為矩陣
citys<-cmdscale(city3,k=2) #計算MDS,為視覺化,取前兩個主座標
plot(citys[,1],citys[,2],type='n') #繪圖
text(citys[,1],citys[,2],labels(city2),cex=.7) #標上城市名字
#看看圖和真實的地圖方向是反的,修改一下:
plot(-citys[,1],-citys[,2],type='n')
text(-citys[,1],-citys[,2],labels(city2),cex=.7)

 

三、第二部分,非線性降維方法


資料的各個屬性間是強相關的


1、【流形學習】


流形是線性子空間的一種非線性推廣,流形學習是一種非線性的維數約簡方法
假設:高維資料位於或近似位於潛在的低維流行上
思想:保持高維資料與低維資料的某個“不變特徵量”而找到低維特徵表示
以不變特徵量分為:
·····Isomap:測地距離
·····LLE:區域性重構係數
·····LE:資料領域關係

2、【ISOMAP】等距特徵對映


基本思想:通過保持高維資料的測地距離與低維資料的歐式距離的不變性來找到低維特徵表示
測地距離:離得較近的點間的測地距離用歐氏距離代替;離得遠的點間的測地距離用最短路徑逼近

3、【LLE】區域性線性嵌入


假設:取樣資料所在的低維流形在區域性是線性的,即每個取樣點可以用它的近鄰點線性表示

基本思想:通過保持高維資料與低維資料間的區域性領域幾何結構,區域性重構係數來實現降維

四、總結什麼時候使用哪種降維技術

  • Missing Value Ratio: If the dataset has too many missing values, we use this approach to reduce the number of variables. We can drop the variables having a large number of missing values in them
  • Low Variance filter: We apply this approach to identify and drop constant variables from the dataset. The target variable is not unduly affected by variables with low variance, and hence these variables can be safely dropped
  • High Correlation filter: A pair of variables having high correlation increases multicollinearity in the dataset. So, we can use this technique to find highly correlated features and drop them accordingly
  • Random Forest: This is one of the most commonly used techniques which tells us the importance of each feature present in the dataset. We can find the importance of each feature and keep the top most features, resulting in dimensionality reduction
  • Both Backward Feature Elimination and Forward Feature Selection techniques take a lot of computational time and are thus generally used on smaller datasets
  • Factor Analysis: This technique is best suited for situations where we have highly correlated set of variables. It divides the variables based on their correlation into different groups, and represents each group with a factor
  • Principal Component Analysis: This is one of the most widely used techniques for dealing with linear data. It divides the data into a set of components which try to explain as much variance as possible
  • Independent Component Analysis: We can use ICA to transform the data into independent components which describe the data using less number of components
  • ISOMAP: We use this technique when the data is strongly non-linear
  • t-SNE: This technique also works well when the data is strongly non-linear. It works extremely well for visualizations as well
  • UMAP: This technique works well for high dimensional data. Its run-time is shorter as compared to t-SNE

本文為轉載文章,原文地址:https://www.douban.com/note/469279998/