kmean python_Kmeans分类python实现

import numpy as np

import matplotlib.pyplot as plt

class KmeansClassifier:

def __init__(self, k, distance_method = "o", random_select = True,

plot = False):

self.k = k

self.distance_method = distance_method

self.random_select = random_select

self.plot = plot

if(distance_method == "o"):

self._dist = self._euler_dist

else:

self._dist = self._manhattan_distance

def _euler_dist(self, x1, x):

return np.sqrt(np.multiply(x-x1, x-x1).sum())

def _manhattan_distance(self, x1, x):

return np.abs(x-x1).sum()

def _get_nearest(self, x, center_list, dist):

dists = []

for center in center_list:

dists.append(dist(x, center))

return dists.index(min(dists))

def _fit(self, x, y, dist, x_center_index_list, center_list):

xy_map = np.hstack((x, x_center_index_list, x_center_index_list))

for row in xy_map:

row[-1] = self._get_nearest(row[:-2], center_list, dist)

flag = np.all(xy_map[:, -1] == xy_map[:, -2])

return flag, xy_map[:, -1].reshape(-1, 1), center_list

def _random_center_list(self, x, k):

center_list = np.zeros((x.shape[1], k))

for col in range(x.shape[1]):

col_max = np.max(x[:, col])

col_min = np.min(x[:, col])

center_list[col, :] = col_min + (col_max - col_min) * np.random.rand(1, k)

return center_list.T

def _updata_center_list(self, x, x_center_index_list, center_list):

new_center_list = []

for index in range(len(center_list)):

part_x = x[np.where(

x_center_index_list[:, -1] == index)]

if(0 != part_x.size):

new_center_list.append(np.mean(part_x, axis = 0))

else:

new_center_list.append(np.zeros(part_x.shape[1]))

return new_center_list

def _plot(self, x, x_center_index_list, center_list):

'''

数据绘制,只能绘制二维

'''

center_array = np.array(center_list)

for index in range(len(center_list)):

part_x = x[np.where(

x_center_index_list[:, -1] == index)]

plt.scatter(part_x[:, 0], part_x[:, 1])

plt.scatter(center_array[:, 0], center_array[:, 1], marker = "+")

plt.show()

def fit(self, x, y, center_list = None):

if not center_list:

center_list = self._random_center_list(x, self.k)

x_center_index_list = np.zeros(x.shape[0]).reshape(-1, 1)

flag = False

while(True):

flag, x_center_index_list, center_list = self._fit(x,

y, self._dist, x_center_index_list, center_list)

if(flag):

break

center_list = self._updata_center_list(x, x_center_index_list,

center_list)

if(self.plot):

self._plot(x, x_center_index_list, center_list)

return self

x = np.random.randint(1, 100, (50, 2))

y = np.random.randint(1,4,(10, 1))

kmeans_clf = KmeansClassifier(k = 4, plot=True)

kmeans_clf.fit(x, y)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值