程序不能运行是缺少什么吗?
-
import numpy as np
x = np.random.randn(20)
y = np.random.randn(20)
k = 3
#1.选取聚类中心
center0=np.array([x[0],y[0]])
center1=np.array([x[1],y[1]])#2.开始迭代
iter = 100
while iter > 0:
print('111111')
res = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
for i in range(20):
# 2.1计算每个对象与聚类中心的距离
dis0 = np.sqrt((x[i] - center0[0]) ** 2 + (y[i] - center0[1]) ** 2)
dis1 = np.sqrt((x[i] - center1[0]) ** 2 + (y[i] - center1[1]) ** 2)#2.1归类 if(dis0 > dis1): res[i]=1 else: res[i]=0 #2.3新的聚类中心 res0 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] res01 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] res1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] res11 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] n0 = 0 n1 = 0 for i in range(20): if res[i] == 0: res0[n0] = x[i] res01[n0] = y[i] n0 += 1 else: res1[n1] = x[i] res11[n1] = y[i] n1 += 1 center0_new = np.array([np.array(res0).mean(), np.array(res01).mean()]) center1_new = np.array([np.array(res1).mean(), np.array(res11).mean()]) #3.判定聚类中心位置是否发生变换 if all((center0 == center0_new) & (center1 == center1_new)): # 如果没发生变换则退出循环,表示已得到最终的聚类中心 break center1=center1_new center0=center0_new
#4.输出结果
for i in range(20):
if res[i] == 0:
print(x[i],y[i],'0')
else:
print(x[i],y[i],'1')
-
OpenMV不能用numpy,你的代码哪来的?