- 帖子
- 3
- 精华
- 0
- 积分
- 18
- 阅读权限
- 10
- 注册时间
- 2019-12-5
- 最后登录
- 2022-5-24
|
我有一些数据想要实现kmeans算法,照着网上的原理自己写了下但是不知道为什么中心点位置无法更新,import json
import numpy as np
def euclDistance(vector1, vector2):
return np.sqrt(np.sum(np.power(vector2 - vector1, 2)))
#C:\\Users\\babyblue\\Desktop\\xsilent_test
#C:\\deeplearning\\CSL\\test
def load_data():
with open('C:\\deeplearning\\CSL\\test\\P02_01_06_0_color.json', 'r') as load_f:
load_dict = json.load(load_f)
data = load_dict['data']
zball = []
for i in data:
skeleton = i['skeleton']
pose = skeleton['pose']
x = pose[::2]
y = pose[1::2]
x_unnorm = []
y_unnorm = []
for p, q in zip(x, y):
x_un = p *1280
y_un = q *720
x_unnorm.append(x_un) #未归一化的坐标
y_unnorm.append(y_un)
zb = []
for j in range(52):
zuobiao = [x_unnorm[j], y_unnorm[j]]
zb.append(zuobiao)
zball.append(zb) #一帧52个关节点的xy坐标
return zball
def weight():
zball_3 = load_data()
zball_array = np.array(zball_3)
zball_trans = zball_array.transpose(1, 0, 2)#视频中所有帧的x,y
zball_list = zball_trans.tolist()
skeleton_var = []
for c in zball_list:
#sum_x ,sum_y = 0,0
x_f, y_f = [], []
for d in c:
x_f.append(d[0])
y_f.append(d[1])
x_var = np.var(x_f)
y_var = np.var(y_f)
var = x_var + y_var
skeleton_var.append(var)
var_sum = 0
for d in skeleton_var:
var_sum += d #所有帧的方差之和
weight_percent = []
for e in skeleton_var:
weight = np.sqrt(e/(var_sum))
weight_percent.append(weight) #每个节点的贡献度权值
return weight_percent
def center_init():
zball_ = load_data()
weight_percent1 = weight()
disall = []
for n in range(len(zball_)-1):
distance = []
for m in range(52):
vecA, vecB = np.array(zball_[n][m]), np.array(zball_[n+1][m])
dis = euclDistance(vecA, vecB)
distance.append(dis)
disall.append(distance) #所有相邻帧的关节点坐标表欧氏距离
dist_fr = []
for dist in disall:
dist_sum = 0
for keypoint, wei in zip(dist, weight_percent1):
dist_sum += (keypoint * wei)
dist_fr.append(dist_sum) #所有相邻帧的权重距离之和
dist_reverse = sorted(dist_fr, reverse =True)#降序排列D
fr_order = sorted(range(len(dist_fr)), key=lambda k: dist_fr[k], reverse =True)#降序排列的距离的索引值
k_value = sorted(fr_order[:4])# k-1
center = [np.random.randint(0, k_value[0])+1]
for i in range(len(k_value)-1):
center += [np.random.randint(k_value[i]+1, k_value[i+1]+1)]
center_ = center + [np.random.randint(k_value[-1]+1, len(zball_))]#从k类中抽取一帧作为中心
center_list = []
for cen in center_:
center_list.append(zball_[cen]) #初始中心点
return center_list
#######前面主要是获取原始数据和初始化中心点,都没有什么问题,我获得的数据就是一个视频的所有帧,每个帧有52个点,shape是(136,52,2)的
#######我利用自己定义的求距离方法,定义了几个函数,下面就出现问题了,循环五次,但是除了和原始中心点center_list不一样,后面全一样,不知道问题到底出在哪里??
def distance():
zball_1 = load_data()
weight_percent2 = weight()
diss = np.zeros((len(zball_1), len(centers)))
for o in range(len(zball_1)):
for p in range(len(centers)):
for q in range(52):
veA, veB = np.array(zball_1[o][q]), np.array(centers[p][q])
diss[o,p] =np.sum(weight_percent2[q] * euclDistance(veA, veB))
return diss
def near_center():
dist = distance()
near_cen = np.argmin(dist, 1)
return near_cen
def kmeans():
zball_2 = load_data() #原始数据
zball_array = np.array(zball_2)
cent_list = []
count = 0
centers = center_init() #初始化的中心点
for _ in range(5):
near_cen = near_center() # 每一帧的归属类别
count += 1
for ci in range(5):
centers[ci] = zball_array[np.where(near_cen==ci)].mean(0) #更新中心点shape是(5,52,2)
cent_list.append(centers)
print(count)
return centers, near_cen
centers, near_cen = kmeans()
|
|