Reputation: 33
I would like to implement phython code which does the following:
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])]
My current solution is the following:
i = 0
return [clusters[i].mean(axis=1) for i in range(np.amax(i)+1)]
However, I only get the following output:
[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]]),
])
So, I only get the mean of the first row computed, but unfortunately not for the seconder row. Do you have any idea which improves my code?
Thanks!
Upvotes: 0
Views: 1479
Reputation: 2823
The list comprehension should be
[q.mean(axis=0) for q in clusters]
It is a bit confusing that the output is called a matrix. It is a list of rank 1 arrays.
Upvotes: 3