91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 10KB
    文件類型: .py
    金幣: 1
    下載: 2 次
    發(fā)布日期: 2021-11-17
  • 語言: Python
  • 標簽: Kmeans??python??

資源簡介

K-Means算法是典型的基于距離的聚類算法,其中k代表類簇個數,means代表類簇內數據對象的均值(這種均值是一種對類簇中心的描述),因此,K-Means算法又稱為k-均值算法。K-Means算法是一種基于劃分的聚類算法,以距離作為數據對象間相似性度量的標準,即數據對象間的距離越小,則它們的相似性越高,則它們越有可能在同一個類簇。數據對象間距離的計算有很多種,k-means算法通常采用歐氏距離來計算數據對象間的距離。該算法認為簇是由距離靠近的對象組成的,因此把得到緊湊且獨立的簇作為最終目標。

資源截圖

代碼片段和文件信息

import?numpy?as?np
import?matplotlib.pyplot?as?plt

#?先在四個中心點附近產生一堆數據
real_center?=?[(1?1)?(1?2)?(2?2)?(2?1)]
point_number?=?50

points_x?=?[]
points_y?=?[]

for?center?in?real_center:
????offset_x?offset_y?=?np.random.randn(point_number)?*?0.3?np.random.randn(point_number)?*?0.25
????x_val?y_val?=?center[0]?+?offset_x?center[1]?+?offset_y

????points_x.append(x_val)
????points_y.append(y_val)

points_x?=?np.concatenate(points_x)?#?將二維數組拼接成一個list
points_y?=?np.concatenate(points_y)

#?繪制點圖
plt.scatter(points_x?points_y?color=‘green‘?marker=‘+‘)
#?繪制中心點
center_x?center_y?=?zip(*real_center)
plt.scatter(center_x?center_y?color=‘red‘?marker=‘^‘)
plt.xlim(0?3)
plt.ylim(0?3)
plt.show()

#=================================================================================
#?第一步,隨機選擇?K?個點
K?=?4
p_list?=?np.stack([points_x?points_y]?axis=1)?#用np.stack將points_x和points_y拼接,變成(xy)的坐標形式???p_list.shape(2002)
index?=?np.random.choice(len(p_list)?size=K)?#在p_list中隨機選擇K個點的序列號
centeroid?=?p_list[index]?#取出指定序列號的點的坐標

#?以下是畫圖部分
for?p?in?centeroid:
????plt.scatter(p[0]?p[1]?marker=‘^‘)

plt.xlim(0?3)
plt.ylim(0?3)
plt.show()

#===============================================================================
#?第二步,遍歷所有點?P,將?P?放入最近的聚類中心的集合中
points_set?=?{key:?[]?for?key?in?range(K)}
#?print(“前“points_set)
for?p?in?p_list:
????#?print(“center:“centeroid)
????#?print(“循環(huán)出來的p:“p)
????#判斷這個點離哪一個中心點近,近則記錄下index。然后在對應index的集合中加入該點
????nearest_index?=?np.argmin(np.sum((centeroid?-?p)?**?2?axis=1)?**?0.5)?#np.argmin返回(距離)最小值的下標,參數axis=1:按行方向求和
????points_set[nearest_index].append(p)
????#?point_set?=?{0:[([x1y1])([x2y2])......]1:[]......}

#?以下是畫圖部分
for?k_index?p_set?in?points_set.items():
????p_xs?=?[p[0]?for?p?in?p_set]
????p_ys?=?[p[1]?for?p?in?p_set]
????plt.scatter(p_xs?p_ys?color=‘C{}‘.format(k_index))

for?ix?p?in?enumerate(centeroid):
????plt.scatter(p[0]?p[1]?color=‘C{}‘.format(ix)?marker=‘^‘?edgecolor=‘black‘?s=128)

plt.xlim(0?3)
plt.ylim(0?3)
plt.show()

#===============================================================================
#?第三步,遍歷每一個點集,計算新的聚類中心
for?k_index?p_set?in?points_set.items():
????#?print(k_indexp_set)
????p_xs?=?[p[0]?for?p?in?p_set]
????p_ys?=?[p[1]?for?p?in?p_set]
????#?print(p_xs)
????#?print(p_ys)
????centeroid[k_index?0]?=?sum(p_xs)?/?len(p_set)
????centeroid[k_index?1]?=?sum(p_ys)?/?len(p_set)

#?0?[array([1.6062368?0.7195094])
#?array([1.5209016??0.79879955])
#?array([1.58217716?0.6752188?])
#?array([1.47984399?0.77579545])
#?array([1.56584005?0.72633556])
#?array([1.36814733?0.85458767])
#?array([1.47112209?1.26962527])
#?array([1.39860841?0.88106941])
#?array([1.42538269?1.00176483])
#?array([1.8285598??0.63840008])
#?array([1.59278399?0.87566845])
#?array([1.75997827?1.58673337])
#?array([2.40024409?1.43842766])
#?array([1.63783343?1.04777846])
#?array([2.27202361

評論

共有 條評論