如何使用Python的特征集聚进行降维?
How to use Python's feature agglomeration for dimensionality reduction?
我在 Python 中搜索了实现降维的方法,这是我得到的结果:http://scikit-learn.org/stable/modules/unsupervised_reduction.html。该网站显示的最后一种方法是特征集聚。我单击了 link 以查看该 python 方法的文档,但我仍然不确定如何使用它。
如果有人以前使用过Python的特征集聚方法,您能否解释一下它是如何工作的(输入、输出等)?谢谢!
您可以使用 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()
我在 Python 中搜索了实现降维的方法,这是我得到的结果:http://scikit-learn.org/stable/modules/unsupervised_reduction.html。该网站显示的最后一种方法是特征集聚。我单击了 link 以查看该 python 方法的文档,但我仍然不确定如何使用它。
如果有人以前使用过Python的特征集聚方法,您能否解释一下它是如何工作的(输入、输出等)?谢谢!
您可以使用 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()