【发布时间】:2021-07-24 15:28:33
【问题描述】:
我想实现执行以下操作的 phython 代码:
For a collections of clusters calculate the mean for each cluster
Args:
clusters (List[np.ndarray]): A list of 2d arrays
Returns:
List[np.ndarray]: A matrix where each row represents a mean of a cluster
Example:
>>> tiny_clusters = [
np.array([[0.2, 0.3], [0.1, 0.2]]),
np.array([[0.8, 0.9], [0.7, 0.5], [0.6, 0.7]]),
]
>>> calc_means(tiny_clusters)
[array([0.15, 0.25]), array([0.7,0.7])]
我目前的解决方案如下:
i = 0
return [clusters[i].mean(axis=1) for i in range(np.amax(i)+1)]
但是,我只得到以下输出:
[array([0.25, 0.15])]
(Input: [
np.array([[0.2, 0.3], [0.1, 0.2]]),
np.array([[0.8, 0.9], [0.7, 0.5], [0.6, 0.7]]),
])
所以,我只计算了第一行的平均值,但不幸的是第二行没有。你有什么想法可以改进我的代码吗?
谢谢!
【问题讨论】:
-
不要把所有东西都命名为
i。