k-means算法是无监督学习中的经典算法,它的主要目的就是聚类。原理很简单,在次不多做赘述 可以参考https://www.cnblogs.com/pinard/p/6164214.html。下面主要是算法基本原理实现的代码,刚刚学习的朋友可以参考一下。编写语言:python。
import numpy as np
import matplotlib.pyplot as plt
#初始化数据集
def init_dateSet():
date_set = []
date1 = np.random.randint(1, 50, [100, 2])
#print(date1)
date2 = np.random.randint(45, 95, [100, 2])
#print(date2)
for i in range(len(date1)):
date_set.append(date1[i])
for j in range(len(date2)):
date_set.append(date2[j])
date_set = np.array(date_set)
return date_set
#可视化数据
def plot_date(date_set):
plt.plot(date_set[:, 0], date_set[:, 1], ‘ko’)
plt.show()
#初始化K(簇)函数 这里默认初始化俩个簇
def k_nums(date_set):
k_number = []
k1 = np.random.randint(1, 200)
print(k1)
k_number.append(date_set[k1])
k2 = np.random.randint(1, 200)
print(k2)
k_number.append(date_set[k2])
k_number = np.array(k_number)
return k_number
#求欧式距离函数
def distance_vector(v1, v2):
v1 = np.array(v1).reshape(1, -1)
v2 = np.array(v2).reshape(1, -1)
dis = pow(np.linalg.norm(v1-v2), 2)
return dis
#求向量的均值
def avg_vector(k_list):
avg_k_list = []
k_list = np.array(k_list)
#print(‘k_lsit=’, k_list)
for i in range(len(k_list[0])):
avg = sum(k_list[:, i])/len(k_list)
avg_k_list.append(avg)
#print(‘avg=’, avg_k_list)
return avg_k_list
#k-means函数
def k_means(date_set, k_number, iter):
k_list = []
len_date_set = len(date_set)
len_k_number = len(k_number)
#初始化簇数组
for i in range(len_k_number):
k_list.append([])
for i in range(iter):
for j in range(len_date_set):
dist = []
for k in range(len_k_number):
dist.append(distance_vector(date_set[j], k_number[k])) #计算每个数据点到各个k(簇)的欧式距离
#取dist中的最小值的索引和值
min_dist = min(dist)
#print(‘min_dist=’, min_dist)
min_dist_index = dist.index(min_dist)
#print(‘min_dist_index=’, min_dist_index)
#将其加入某个簇
k_list[min_dist_index].append(date_set[j])
#print(‘k_list=’, k_list)
#求所有簇中的均值 然后将簇的位置移动到均值处
avg_k_list = []
for m in range(len(k_list)):
avg_k_list.append(avg_vector(k_list[m]))
#print(avg_k_list)
for k_num in range(len_k_number):
k_number[k_num] = avg_k_list[k_num]
print(‘k_number=’, k_number)
plt.plot(k_number[:, 0], k_number[:, 1], ‘ro’)
plot_date(date_set)
return k_number
def main():
iter = 10
date_set = init_dateSet()
k_number = k_nums(date_set)
if name == ‘main’:
main()