赞
踩
K-Means算法import numpy as np
import pandas as pd
from sklearn.cluster import KMeans
data = pd.read_csv('multi_vol.csv')
data1 = data.T #使待分类样本格式正确
estimator = KMeans(n_clusters=4) #构造聚类器
estimator.fit(data1)
label_pred = estimator.labels_ #最终聚类类别
centroids = estimator.cluster_centers_ #最终聚类中心
inertia = estimator.inertia_
学习向量量化LVQ#迭代轮数
for i in range(loops):
#随机产生样本
index = np.random.randint(0,30)
min_dist = sum((q[0] - x[index])**2)
q_index = 0
#搜索里样本最近原型向量
for j in range(1,len(q)):
dist = sum((q[j] - x[index])**2)
if dist < min_dist:
min_dist = dist
q_index = j
if q_label[q_index] == y[index]:
#样本标记和原型向量标记相同,该原型向量向样本方向移动
#eta为学习率
q[q_index] += eta*(x[index]-q[q_index])
else:
#样本标记和原型向
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。