Python Tutorial: Unsupervised Machine Learning
2019-09-01 22:03 nuswgg 阅读(196) 评论(0) 编辑 收藏 举报7.1 KMeans Clustering
iris = pd.read_csv('/Users/iris.csv')
iris["Species"] = np.where(iris["Target"] == 0, "Setosa",
np.where(iris["Target"] == 1, "Versicolor", "Virginica"))
features = pd.concat([iris["PetalLength"], iris["PetalWidth"],
iris["SepalLength"], iris["SepalWidth"]], axis = 1)
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters = 3, random_state = 29).fit(features)
print(pd.crosstab(index = iris["Species"], columns = kmeans.labels_))
7.2 Spectral Clustering
from sklearn.cluster import SpectralClustering
spectral = SpectralClustering(n_clusters = 3,random_state = 29).fit(features)
print(pd.crosstab(index = iris["Species"], columns = spectral.labels_))
7.3 Ward Hierarchical Clustering
from sklearn.cluster import AgglomerativeClustering
aggl = AgglomerativeClustering(n_clusters = 3).fit(features)
print(pd.crosstab(index = iris["Species"], columns = aggl.labels_))
7.3 Ward Hierarchical Clustering
from sklearn.cluster import AgglomerativeClustering
aggl = AgglomerativeClustering(n_clusters = 3).fit(features)
print(pd.crosstab(index = iris["Species"], columns = aggl.labels_))
7.4 DBSCAN
from sklearn.cluster import DBSCAN
dbscan = DBSCAN().fit(features)
print(pd.crosstab(index = iris["Species"], columns = dbscan.labels_))
7.5 Self-organizing map
from pyclustering.nnet import som
sm = som.som(4,4)
sm.train(features.as_matrix(), 100)
sm.show_distance_matrix()