生活随笔
收集整理的這篇文章主要介紹了
LVQ模型Python实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- https://github.com/Sean16SYSU/MachineLearningImplement
簡述
LVQ模型是聚類的經典模型,跟Kmeans有點像。但是作為一個聚類,這個模型是一個有監督的模型。
算法流程
輸入的數據集X, y,還有學習率(在0,1之間)η\etaη初始,選k個點,作為原型向量然后開始循環 在樣本集中隨機選個點。找到在原型向量中離它最近的點然后來比較這兩個點之間的y是否一樣。一樣就正向更新節點,不一樣就反向更新節點pi=pi(+/?)η?(xj?pi)p_i = p_i (+ / -) \eta * (x_j- p_i)pi?=pi?(+/?)η?(xj??pi?) 中間選是加或者減。 迭代一定的次數,或者變化足夠小之后,就退出迭代
實驗部分
from sklearn
import datasets
iris
= datasets
.load_iris
()
iris
.data
.shape
, iris
.target
.shape
((150, 4), (150,))
import numpy
as np
def random_split(X
, y
, train_rate
=0.3):y_
= np
.array
(list(zip(y
, range(len(y
)))))np
.random
.shuffle
(y_
)train_n
= int(len(y
) * train_rate
)y_train
= y_
[:train_n
]y_test
= y_
[train_n
:]index_train
= y_train
[:, 1]index_test
= y_test
[:, 1]return X
[index_train
], y_train
[:, 0], X
[index_test
], y_test
[:, 0]
def LVQ(X
, y
, k
, MAX_TIME
=100, ita
=0.2):init_index
= np
.random
.choice
(len(y
), k
)px
= X
[init_index
]py
= y
[init_index
]for _
in range(MAX_TIME
):j
= np
.random
.choice
(len(y
), 1)xj
, yj
= X
[j
], y
[j
]i
= np
.argmin
([np
.linalg
.norm
(xj
- pi
) for pi
in px
])pyi
= py
[i
]if pyi
== yj
:px
[i
] = px
[i
] + ita
* (xj
- px
[i
])else:px
[i
] = px
[i
] - ita
* (xj
- px
[i
])return px
def PVQ_clustering(X
, px
):return np
.array
(list(map(lambda x
: np
.argmin
([np
.linalg
.norm
(x
- pi
) for pi
in px
]), X
)))
train_x
, train_y
, test_x
, test_y
= random_split
(iris
.data
, iris
.target
)
px
= LVQ
(train_x
, train_y
, 3)
y
= PVQ_clustering
(test_x
, px
)
import matplotlib
.pyplot
as plt
from sklearn
.decomposition
import PCAX_reduced
= PCA
(n_components
=2).fit_transform
(test_x
)
plt
.scatter
(X_reduced
[:, 0], X_reduced
[:, 1], c
=y
, cmap
=plt
.cm
.Set1
)
def evaluate(y
, t
):a
, b
, c
, d
= [0 for i
in range(4)]for i
in range(len(y
)):for j
in range(i
+1, len(y
)):if y
[i
] == y
[j
] and y
[j
] == t
[j
]:a
+= 1elif y
[i
] == y
[j
] and y
[j
] != t
[j
]:b
+= 1elif y
[i
] != y
[j
] and y
[j
] == t
[j
]:c
+= 1elif y
[i
] != y
[j
] and y
[j
] != t
[j
]:d
+= 1return a
, b
, c
, d
a
, b
, c
, d
= evaluate
(y
, test_y
)
def external_index(a
, b
, c
, d
, m
):JC
= a
/ (a
+ b
+ c
)FMI
= np
.sqrt
(a
**2 / ((a
+ b
) * (a
+ c
)))RI
= 2 * ( a
+ d
) / ( m
* (m
+ 1) )return JC
, FMI
, RI
external_index
(a
, b
, c
, d
, len(y
))
External indexValue
| JC | 0.1485971596813301 |
| FMI | 0.26021571720401077 |
| RI | 0.5394429469901169 |
總結
以上是生活随笔為你收集整理的LVQ模型Python实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。