手寫演算法系列(Python實現)

2021-10-24 06:49:19 字數 3023 閱讀 4109

鍛鍊自己實現演算法的能力,

k-means演算法

'''

k-means演算法是一種常用的無監督聚類演算法,可以視作同時優化質心和每個樣本的標籤,使得損失函式最小,演算法執行過程提現了em的思想

'''import numpy as np

import random

class

kmeans()

:def

__init__

(self, k=2)

: self.k = k

def__dist

(self, a, b)

:return np.sqrt(

sum(np.power(

(a-b),2

)))#輸入ndarray型別x,輸出每個樣本所對應的簇

deffit

(self, x)

:#1.初始化質心

clusters =

[[x[random.randint(0,

len(x)-1

)],1

]for i in

range

(self.k)

] labels =[0

for i in

range

(len

(x))

]#2.每次更新節點所屬簇

changed =

true

while changed:

changed =

false

old_labels = labels.copy(

)for i in

range

(len

(x))

: c0 = clusters[0]

[0]/ clusters[0]

[1] cur_dist = self.__dist(c0, x[i]

)for j in

range(1

,len

(clusters)):

cj = clusters[j][0

]/ clusters[j][1

]if cur_dist > self.__dist(cj, x[i]):

labels[i]

= j cur_dist = self.__dist(cj, x[i]

)if old_labels != labels:changed =

true

#3.更新質點

for k in

range

(len

(clusters)):

clusters[k]

=[np.zeros(

(x.shape[1]

)),0

]for i in

range

(len

(x))

: clusters[labels[i]][

0]+= x[i]

clusters[labels[i]][

1]+=1

return labels

if __name__ ==

'__main__'

: dataset =[[

0.697

,0.460],

[0.774

,0.376],

[0.634

,0.264],

[0.608

,0.318],

[0.556

,0.215],

[0.403

,0.237],

[0.481

,0.149],

[0.437

,0.211],

[0.666

,0.091],

[0.243

,0.267],

[0.245

,0.057],

[0.343

,0.099],

[0.639

,0.161],

[0.657

,0.198],

[0.360

,0.370],

[0.593

,0.042],

[0.719

,0.103],

[0.359

,0.188],

[0.339

,0.241],

[0.282

,0.257],

[0.748

,0.232],

[0.714

,0.346],

[0.483

,0.312],

[0.478

,0.437],

[0.525

,0.369],

[0.751

,0.489],

[0.532

,0.472],

[0.473

,0.376],

[0.725

,0.445],

[0.446

,0.459]]

k =3 x = np.array(dataset)

result = kmeans(k=k)

.fit(x)

import matplotlib.pyplot as plt

colors =

['b'

,'g'

,'r'

]for i in

range

(k):

ys =

for j in

range

(len

(result)):

if result[j]

plt.scatter(x[ys,0]

, x[ys,1]

, c=colors[i]

, alpha=

0.5)

plt.show(

)print

(result)

pass

Python演算法系列 雜湊演算法

四 總結 雜湊演算法又稱雜湊函式演算法,是一種查詢演算法。就是把一些複雜的資料通過某種對映關係。對映成更容易查詢的方式,但這種對映關係可能會發生多個關鍵字對映到同一位址的現象,我們稱之為衝突。在這種情況下,我們需要對關鍵字進行二次或更多次處理。出這種情況外,雜湊演算法可以實現在常數時間內儲存和查詢這...

演算法系列 全排列演算法 Java實現

昨晚心血來潮,想起全排列演算法,但是忘記怎麼實現,所以就自己再次試圖編寫 實現。晚上想不出來,想不到,一覺醒來後,一下子就寫好了!public class quanpailie array a array a array b array b array a array b array a array...

演算法系列 bitmap演算法詳解和實現

我們可以將bitmap看成是一種資料結構,所謂的bit map就是用乙個 或幾個 bit位來標記某個元素對應的state value 最基本的情況,使用1bit標示乙個關鍵字的狀態 可標示兩種狀態 但根據需要也可以使用2bit 標示4種狀態 3bit 標示8種狀態 當乙個狀態標示需要的位數達到32b...