当前位置 : 主页 > 编程语言 > python >

Python kmean

来源:互联网 收集:自由互联 发布时间:2022-06-15
# -*- coding: utf-8 -*- from sklearn.cluster import KMeans from sklearn.externals import joblib import numpy feature=numpy.random.rand(40,20) #调用kmeans类 clf = KMeans(n_clusters=9) s = clf.fit(feature) print s #9个中心 print clf.clu

# -*- coding: utf-8 -*-

from sklearn.cluster import KMeans

from sklearn.externals import joblib

import numpy


feature =numpy.random.rand(40,20)


#调用kmeans类

clf = KMeans(n_clusters=9)

s = clf.fit(feature)

print s


#9个中心

print clf.cluster_centers_


#每个样本所属的簇

print clf.labels_


#用来评估簇的个数是否合适,距离越小说明簇分的越好,选取临界点的簇个数

print clf.inertia_


#进行预测

print clf.predict(feature)


#保存模型

joblib.dump(clf , 'c:/km.pkl')


#载入保存的模型

clf = joblib.load('c:/km.pkl')

'''

#用来评估簇的个数是否合适,距离越小说明簇分的越好,选取临界点的簇个数

for i in range(5,30,1):
clf = KMeans(n_clusters=i)
s = clf.fit(feature)
print i , clf.inertia_
'''



上一篇:numpy
下一篇:没有了
网友评论