大家好,我是小寒。
今天我们来介绍两种常见的降维方法 PCA 和 t-SNE。
降维是将高维数据转换为低维表示的过程,同时保留尽可能多的相关信息。降维可以帮助你:在二维或三维空间中可视化高维数据。
降低机器学习算法的计算成本和复杂性。
消除数据中的噪音和冗余。
PCA 和 t-SNE 是两种最广泛使用的降维技术,但它们具有不同的优点和缺点。PCA 是一种线性技术,试图找到数据中最大方差的方向,并将数据投影到较低维的子空间上。t-SNE 是一种非线性技术,试图在高维空间中找到相似数据点的簇,并将它们映射到低维空间,同时保留数据的局部结构。PCA
主成分分析(PCA)是一种线性降维技术,试图找到数据中最大方差的方向,并将数据投影到较低维的子空间上。PCA 的主要步骤如下:
将数据标准化。
计算数据的协方差矩阵,该矩阵衡量特征之间的成对相关性。
计算协方差矩阵的特征值和特征向量,它们表示主成分的大小和方向。
按降序对特征值进行排序,并选择与 k 个最大特征值相对应的前 k 个特征向量,其中 k 是缩减数据的所需维数。
通过将数据矩阵与特征向量矩阵相乘,将原始数据变换到新子空间中。
在 Python 中,你可以使用 scikit-learn 库对数据执行 PCA。
以下代码片段展示了如何从 sklearn.decomposition 模块导入 PCA 类,以及如何使用 fit_transform 方法拟合和转换数据。
# Import the PCA class
from sklearn.decomposition import PCA
# Create a PCA object with the desired number of components
pca = PCA(n_components=2)
# Fit and transform the data
data_reduced = pca.fit_transform(data)
data_reduced 变量将包含二维的缩减数据。t-SNE 是一种非线性降维技术,试图在高维空间中找到相似数据点的簇,并将它们映射到较低维空间,同时保留数据点的局部结构。
- 计算高维空间中数据点之间的成对相似度,使用高斯核来测量一个点是另一个点的邻居的概率。
- 计算低维空间中数据点之间的成对相似度,使用 students-t 分布来测量一个点与另一个点相邻的概率,。
- 使用梯度下降算法调整数据点在低维空间中的位置,最小化两个概率分布之间的 Kullback-Leibler 散度。
在 Python 中,你可以使用 scikit-learn 库对数据执行 t-SNE。以下代码片段展示了如何从 sklearn.manifold 模块导入 TSNE 类,以及如何使用 fit_transform 方法拟合和转换数据。
from sklearn.manifold import TSNE
# Create a TSNE object with the desired parameters
tsne = TSNE(n_components=2, perplexity=30, learning_rate=200, n_iter=1000)
# Fit and transform the data
data_reduced = tsne.fit_transform(data)
PCA 和 t-SNE 比较
以下代码片段展示了如何从 sklearn.datasets 和 sklearn.utils 模块导入数据集和绘图函数,以及如何使用 plt.scatter 方法绘制缩减后的数据。
# Import the datasets and the plotting function
from sklearn.datasets import load_iris, load_digits, fetch_olivetti_faces
import matplotlib.pyplot as plt
# Load the datasets
iris = load_iris()
# Plot the reduced data
def plot_reduced_data(data, labels, title):
# Create a figure and an axis
fig, ax = plt.subplots()
# Scatter the data points with different colors according to the labels
ax.scatter(data[:, 0], data[:, 1], c=labels, cmap=plt.cm.tab10, alpha=0.5)
# Set the title and the axis labels
ax.set_title(title)
ax.set_xlabel('Component 1')
ax.set_ylabel('Component 2')
# Show the plot
plt.show()
现在,让我们看看 PCA 和 t-SNE 在 iris 数据集上的表现如何。iris 数据集有四个维度,你将使用这两种技术将其减少到二维。以下代码片段展示了如何在 iris 数据集上执行 PCA 和 t-SNE,以及如何使用 plot_reduced_data 函数绘制缩减后的数据。
# Import the PCA and TSNE classes
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
# Get the data and the labels from the iris dataset
iris_data = iris.data
iris_labels = iris.target
# Perform PCA on the iris data
pca = PCA(n_components=2)
iris_data_pca = pca.fit_transform(iris_data)
# Perform t-SNE on the iris data
tsne = TSNE(n_components=2, perplexity=30, learning_rate=200, n_iter=1000)
iris_data_tsne = tsne.fit_transform(iris_data)
# Plot the reduced data using PCA and t-SNE
plot_reduced_data(iris_data_pca, iris_labels, 'PCA on Iris Dataset')
plot_reduced_data(iris_data_tsne, iris_labels, 't-SNE on Iris Dataset')
下图显示了 iris 数据集上 PCA 和 t-SNE 降维的结果。你可以看到,这两种技术都能够区分三类鸢尾花,但 t-SNE 似乎比 PCA 产生更紧凑、更清晰的聚类。
福利一
注意:免费整理了100 个数据分析相关的 python 库(还有精美的pdf版本)