Python机器学习:KMeans聚类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python机器学习:KMeans聚类相关的知识,希望对你有一定的参考价值。
Python进行KMeans聚类是比较简单的,首先需要import numpy,从sklearn.cluster中import KMeans模块:
import numpy as np from sklearn.cluster import KMeans
然后读取txt文件,获取相应的数据并转换成numpy array:
X = [] f = open(‘rktj4.txt‘) for v in f: regex = re.compile(‘\\s+‘) X.append([float(regex.split(v)[3]), float(regex.split(v)[6])]) X = np.array(X)
设置类的数量,并聚类:
n_clusters = 5
cls = KMeans(n_clusters).fit(X)
完整代码:
import numpy as np from sklearn.cluster import KMeans import matplotlib.pyplot as plt import re X = [] f = open(‘rktj4.txt‘) for v in f: regex = re.compile(‘\\s+‘) X.append([float(regex.split(v)[3]), float(regex.split(v)[6])]) X = np.array(X) n_clusters = 5 cls = KMeans(n_clusters).fit(X) cls.labels_ markers = [‘^‘,‘x‘,‘o‘,‘*‘,‘+‘] for i in range(n_clusters): members = cls.labels_ == i plt.scatter(X[members, 0], X[members, 1], s=60, marker=markers[i], c=‘b‘, alpha=0.5) print plt.title(‘‘) plt.show()
运行结果:
以上是关于Python机器学习:KMeans聚类的主要内容,如果未能解决你的问题,请参考以下文章
吴裕雄 python 机器学习——K均值聚类KMeans模型
机器学习Python详细实现基于欧式Euclidean切比雪夫Chebyshew曼哈顿Manhattan距离的Kmeans聚类