sklearn KMeans 分类

网友投稿 261 2022-12-01

sklearn KMeans 分类

import itertoolsimport numpy as npimport matplotlib.pyplot as pltfrom sklearn.cluster import KMeansnp.random.seed(1)# Set the number of samples, the means and# variances of each of the three simulated clusterssamples = 100mu = [(7, 5), (8, 12), (1, 10)]cov = [ [[0.5, 0], [0, 1.0]], [[2.0, 0], [0, 3.5]], [[3, 0], [0, 5]],]# Generate a list of the 2D cluster pointsnorm_dists = [ np.random.multivariate_normal(m, c, samples) for m, c in zip(mu, cov)]X = np.array(list(itertools.chain(*norm_dists)))# Apply the K-Means Algorithm for k=3, which is# equal to the number of true Gaussian clusterskm3 = KMeans(n_clusters=3)km3.fit(X)km3_labels = km3.labels_# Apply the K-Means Algorithm for k=4, which is# larger than the number of true Gaussian clusterskm4 = KMeans(n_clusters=4)km4.fit(X)km4_labels = km4.labels_# Create a subplot comparing k=3 and k=4# for the K-Means Algorithmfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14,6))ax1.scatter(X[:, 0], X[:, 1], c=km3_labels.astype(np.float))ax1.set_xlabel("$x_1$")ax1.set_ylabel("$x_2$")ax1.set_title("K-Means with $k=3$")ax2.scatter(X[:, 0], X[:, 1], c=km4_labels.astype(np.float))ax2.set_xlabel("$x_1$")ax2.set_ylabel("$x_2$")ax2.set_title("K-Means with $k=4$")plt.show()

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:图文详解OkHttp的超时时间
下一篇:jqdata
相关文章

 发表评论

暂时没有评论,来抢沙发吧~