我试图计算每个嵌套列表的平均值:
import numpy as np
centroids = [[[3, 2]], [[2, 3], [3, 4], [2, 3]], [[1, 2], [3, 4]]]
所以我希望这些列的平均值如下:[3, 2]
、[[2, 3], [3, 4], [2, 3]] = [2.3, 3.3]
和[[1, 2], [3, 4]] = [2, 3]
我试着去做:
for c in centroids:
c = np.mean(np.array(c), axis=0)
但它不起作用,质心列表中没有任何变化。怎么做?
2条答案
按热度按时间eqoofvh91#
使用简单的列表理解来收集方法:
n3h0vuf22#
一个vanilla python实现:
它输出预期结果: