待完成:scikit 聚类方法 可用源代码

2017-06-01  本文已影响0人  九剑至尊

kmeans聚类

from sklearn.cluster import KMeans
import numpy as np


a = np.loadtxt('data.txt')  
kmeans = KMeans(n_clusters=3, init='k-means++', n_init=10, max_iter=1000000, tol=0.0001, precompute_distances='auto', verbose=0, random_state=None, copy_x=True, n_jobs=1, algorithm='auto').fit(a)
F= open('3cluster_label.txt', 'w')
for i,el in enumerate(kmeans.labels_):
    F.write(str(el)+'\n')
F.close()

spec 谱聚类

import numpy as np
from sklearn import datasets
from sklearn.cluster import SpectralClustering
from sklearn import metrics


a = np.loadtxt('1 1.txt')  
#a = np.array([[1, 2], [1, 4], [1, 0],[4, 2], [4, 4], [4, 0]])
print (a.shape)
print (a[0][0])


y_pred = SpectralClustering(n_clusters=3, gamma=0.01).fit_predict(a)

print "Calinski-Harabasz Score", metrics.calinski_harabaz_score(a, y_pred) 
'''
for index, gamma in enumerate((0.01,0.1,1,10)):
    for index, k in enumerate((3,4,5,6)):
        y_pred = SpectralClustering(n_clusters=k, gamma=gamma).fit_predict(a)
        print "Calinski-Harabasz Score with gamma=", gamma, "n_clusters=", k,"score:", metrics.calinski_harabaz_score(a, y_pred) 
        
'''
F= open('3SpectralClustering_label_1_poi.txt', 'w')
for i,el in enumerate(y_pred):
    F.write(str(el)+'\n')
F.close()
上一篇下一篇

猜你喜欢

热点阅读