我有以下代码
points=candies
K=5
centers = []
for i in range(K):
centers.append(random.choice(points))
centers
这基本上是一个数组列表
[array([0.6 , 0.92, 0.29]),
array([0.99, 0.23, 0.45]),
array([0.65, 0.6 , 0.03]),
array([0.21, 0.22, 0.55]),
array([0.62, 0.84, 0.83])]
我想要的是单个数组,如
array[[0.6 , 0.92, 0.29],
[0.99, 0.23, 0.45],
[0.65, 0.6 , 0.03],
[0.21, 0.22, 0.55],
[0.62, 0.84, 0.83]]
我必须更改什么?
将数组列表转换为二维数组:
np.array(centers)
或从一个空数组开始并填充它:
centers = np.empty((K,3))
for i in range(K):
centers[i] = random.choice(points)