如何使用 Python 的特征聚合进行降维?

Cyn*_*hia 4 python machine-learning feature-extraction dimensionality-reduction scikit-learn

我搜索了在 Python 中实现降维的方法,这是我得到的结果:http : //scikit-learn.org/stable/modules/unsupervised_reduction.html。该网站中显示的最后一种方法是特征聚合。我点击了该 python 方法的文档链接,但我仍然不确定如何使用它。

如果之前有人使用过 Python 的特征聚合方法,您是否可以解释它是如何工作的(输入、输出等)?谢谢!

小智 6

您可以使用 numpy 数组或 Pandas 数据帧作为 sklearn.cluster.FeatureAgglomeration 的输入

输出是一个 numpy 数组,行等于数据集中的行,列等于 FeatureAgglomeration 中设置的 n_clusters 参数。

from sklearn.cluster import FeatureAgglomeration
import pandas as pd
import matplotlib.pyplot as plt

#iris.data from https://archive.ics.uci.edu/ml/machine-learning-databases/iris/
iris=pd.read_csv('iris.data',sep=',',header=None)
#store labels
label=iris[4]
iris=iris.drop([4],1)

#set n_clusters to 2, the output will be two columns of agglomerated features ( iris has 4 features)
agglo=FeatureAgglomeration(n_clusters=2).fit_transform(iris)

#plotting
color=[]
for i in label:
    if i=='Iris-setosa':
        color.append('g')
    if  i=='Iris-versicolor':
        color.append('b')
    if i=='Iris-virginica':
        color.append('r')
plt.scatter(agglo[:,0],agglo[:,1],c=color)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明