使用PCA降低資料維度 - Python
機器學習演算法中使用的任何資料集都可能具有多個維度。然而,並非所有維度都有助於獲得有效的輸出,而只是由於資料集大小和複雜性的增加而導致機器學習模型效能下降。因此,從資料集中消除這些特徵變得非常重要。為此,我們使用一種稱為PCA的降維演算法。
PCA或主成分分析有助於去除那些無助於最佳化結果的資料集維度;從而建立一個更小、更簡單的,包含大部分原始有用資訊的資料集。PCA基於特徵提取的概念,該概念指出,當高維空間中的資料對映到低維空間中的資料時,後者的方差應該最大。
語法
pca = PCA(n_components = number)
這裡,PCA是執行降維的類,pca是由它建立的物件。它只接受一個引數 - 我們希望作為輸出的主成分數量。
此外,當與fit()、DataFrame()和head()函式一起使用時,它將返回具有主成分作為表格的新資料集,我們將在示例中看到。
演算法
步驟 1 - 匯入Python的sklearn和pandas庫以及相關的子模組。
步驟 2 - 載入所需的資料集並將其更改為pandas資料框。
步驟 3 - 使用Standard Scaler標準化特徵,並將新資料集儲存為pandas資料框。
步驟 4 - 在縮放後的資料集中使用PCA類並建立一個物件。另外,傳遞元件的數量並擬合併相應地顯示結果資料。
示例 1
在這個例子中,我們將使用Python sklearn庫中已有的load_diabetes資料集來檢視如何執行PCA。為此,我們將使用PCA類,但在那之前,我們需要處理和標準化資料集。
#import the required libraries from sklearn import datasets #to get the load_diabetes dataset from sklearn.preprocessing import StandardScaler #this standardizes the dimensions from sklearn.decomposition import PCA #to perform PCA from sklearn.datasets import load_diabetes #the dataset that we will use to perform PCA import pandas as pd #to work with the dataframes #load the dataset as pandas dataframe diabetes = datasets.load_diabetes() df = pd.DataFrame(diabetes['data'], columns = diabetes['feature_names']) df.head() #displays the data frame when run in a different cell #standardize the dimensions by creating the object of StandardScaler scalar = StandardScaler() scaled_data = pd.DataFrame(scalar.fit_transform(df)) scaled_data #displays the dataframe after standardization when run in a different cell #apply pca pca = PCA(n_components = 4) pca.fit(scaled_data) data_pca = pca.transform(scaled_data) data_pca = pd.DataFrame(data_pca,columns=['PC1','PC2','PC3', 'PC4']) data_pca.head()
載入糖尿病資料,它返回一個類似於字典的物件,從中將資料提取到Pandas Dataframe中。我們使用StandardScaler標準化資料,並對建立的Pandas Dataframe應用fit_transform()方法。
在儲存在單獨資料框中的標準化資料上呼叫fit()方法以執行PCA分析。結果資料再次儲存在單獨的資料框中,並如下所示列印。
輸出
由於我們選擇了4個主成分,因此返回的輸出具有4列,分別代表每個成分。
示例 2
在這個例子中,我們將從sklearn庫中使用load_wine資料集。此外,這次我們將主成分的數量設定為只有3個。
#import the required libraries from sklearn import datasets #to get the load_wine dataset from sklearn.preprocessing import StandardScaler #this standardizes the dimensions from sklearn.decomposition import PCA #to perform PCA from sklearn.datasets import load_wine #the dataset that we will use to perform PCA import pandas as pd #to work with the dataframes #load the dataset as pandas dataframe wine = datasets.load_wine() df = pd.DataFrame(wine['data'], columns = wine['feature_names']) df.head() #displays the dataframe when run in a different cell #standardize the dimensions by creating the object of StandardScaler scalar = StandardScaler() scaled_data = pd.DataFrame(scalar.fit_transform(df)) scaled_data #displays the dataframe after standardization when run in a different cell #apply pca pca = PCA(n_components = 3) pca.fit(scaled_data) data_pca = pca.transform(scaled_data) data_pca = pd.DataFrame(data_pca,columns=['PC1','PC2','PC3']) data_pca.head()
輸出
由於這次我們只選擇了3個主成分,因此返回的輸出只有3列,分別代表每個成分。
結論
PCA不僅使資料集的元件彼此獨立,而且透過減少資料集的特徵數量來解決過擬合問題。然而,降維並不僅限於PCA。還有其他方法,如LDA - 線性判別分析和GDA - 廣義判別分析,有助於實現相同的目標。