k - means算法怎么在python中使用

  介绍

本篇文章为大家展示了k - means算法怎么在python中使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

<强> k - means算法进行聚类分析

km =, KMeans (=n_clusters  3)   km.fit (X)   centers =km.cluster_centers_   print(中心)

<强>三个簇的中心点坐标为:

[[5.006 - 3.428]

[6.81276596 - 3.07446809]

[5.77358491 - 2.69245283]]

<强>比较一下k - means聚类结果和实际样本之间的差别:

predicted_labels =km.labels_   无花果,axes =, plt.subplots (1,, 2,, figsize=(16日8))   轴[0].scatter (X (:,, 0), X:,, 1,, c=y),提出=plt.cm.Set1,,   ,,,,,,,edgecolor=& # 39; k # 39;,, s=150)   轴[1].scatter (X (:,, 0), X:,, 1,, c=predicted_labels,提出=plt.cm.Set1,   ,,,,,,,edgecolor=& # 39; k # 39;,, s=150)   轴[0].set_xlabel (& # 39; Sepal 长度# 39;,,字形大?16)   轴[0].set_ylabel (& # 39; Sepal 宽度# 39;,,字形大?16)   轴[1].set_xlabel (& # 39; Sepal 长度# 39;,,字形大?16)   轴[1].set_ylabel (& # 39; Sepal 宽度# 39;,,字形大?16)   轴[0].tick_params(方向=& # 39;上# 39;,,=10,长度,宽度=5,,颜色=& # 39;k # 39;,, labelsize=20)   轴[1].tick_params(方向=& # 39;上# 39;,,=10,长度,宽度=5,,颜色=& # 39;k # 39;,, labelsize=20)   轴[0].set_title(& # 39;实际# 39;,,字形大?18)   轴[1].set_title(& # 39;预测# 39;,,字形大?18)

k - means算法实例扩展内容:

#, - *安康;编码:,utf-8  - *安康;   “““Excercise  9.4“““   import  numpy  as  np   import  pandas  as  pd   import  matplotlib.pyplot  as  plt   import 系统   import 随机      data =, pd.read_csv (filepath_or_buffer =, & # 39; . ./数据/watermelon4.0.csv& # 39;,, sep =, & # 39;, & # 39;)[[“密度“,“含糖率“]]. values      # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #,K-means  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #,   k =, int (sys.argv [1])   # Randomly  choose  k  samples 得到data  as  mean 向量   时间=mean_vectors  random.sample(数据、k)      def  dist (p1, p2):   总和,return  np.sqrt (((p1-p2) * (p1-p2)))   while 真正的:   print  mean_vectors=,,clusters  map  ((lambda  x: [x]), mean_vectors),   ,for  sample 拷贝数据:   distances 才能=,地图((m: lambda  dist(示例中,m)),, mean_vectors),   min_index 才能=,distances.index(最低(距离))   集群(min_index)才能.append(样本)=,new_mean_vectors  []   ,for  c, v 拷贝zip(集群,mean_vectors):   new_mean_vector 才能=,len (c)和(c)/# If 从而才能difference  betweenthe  new  mean  vector 以及,old  mean  vector  is  less  than  0.0001   #才能then  do  not  updata 从而mean 向量   if 才能;所有(np.divide ((new_mean_vector-v), v), & lt;, np.array ([0.0001, 0.0001]),):   ,,new_mean_vectors.append (v),   其他的才能:   ,,new_mean_vectors.append (new_mean_vector),   ,if  np.array_equal (mean_vectors new_mean_vectors):   ,打破   ,其他的:   时间=mean_vectors 才能;new_mean_vectors       # Show 从而,clustering 结果   total_colors =, (& # 39; " # 39; & # 39; y # 39;, & # 39;舌鳎# 39;,& # 39;b # 39;, & # 39; c # 39;, & # 39;猴# 39;,& # 39;k # 39;】   时间=colors  random.sample (total_colors k)   for 集群,color 拷贝zip(集群、颜色):=,density 地图(arr lambda 加勒比海盗:[0],集群)=,sugar_content 地图(arr lambda 加勒比海盗:[1],集群)   ,plt.scatter(密度、sugar_content c =,颜色)   plt.show ()

上述内容就是k - means算法怎么在python中使用,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

k - means算法怎么在python中使用