如何利用Python的特征聚集进行降维?

2024-06-10 16:53:52 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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


Tags: 方法文档orgmoduleshttp网站链接html
1条回答
网友
1楼 · 发布于 2024-06-10 16:53:52

可以使用numpy数组或pandas数据帧作为sklearn.cluster.featureCongregation公司在

输出是一个numpy数组,行等于数据集中的行,列等于featureaggregation中的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()

enter image description here

相关问题 更多 >