首页 > 文章列表 > 使用主成分分析(PCA)降低数据维度 - Python

使用主成分分析(PCA)降低数据维度 - Python

301 2023-09-07

机器学习算法中使用的任何数据集都可能有多个维度。然而,并非所有这些都有助于提供有效的输出,并且只会因为尺寸和复杂性的增加而导致 ML 模型表现不佳。因此,从数据集中消除此类特征变得很重要。为此,我们使用称为 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 标准化数据,并将 fit_transform() 方法应用于创建的 Pandas Dataframe。

在我们存储在一个单独的数据框中的标准化数据上调用fit()方法,以执行PCA分析。生成的数据再次存储在一个单独的数据框中,并按如下所示打印出来。

输出

使用主成分分析(PCA)降低数据维度 - Python

由于我们选择了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()

输出

使用主成分分析(PCA)降低数据维度 - Python

由于这次我们只选择了 3 个主成分,因此返回的输出只有 3 列代表每个成分。

结论

PCA不仅使数据集的组成部分彼此独立,而且通过减少数据集的特征数量来解决过拟合问题。然而,降维不仅仅限于PCA。还有其他方法,如 LDA - 线性判别分析和 GDA - 广义判别分析,有助于实现相同的目标。