简体   繁体   English

查找矩阵中每一列的中位数

[英]Finding median of each column in a matrix

Is there a way to find the median of each column in a ndarray.有没有办法找到ndarray中每一列的中位数。 I tried the dumbest method using dual loop over each row to get the element column-wise and perform statstics.median on that and store it in a list.我尝试了最愚蠢的方法,在每一行上使用双循环来逐列获取元素并对其执行 statstics.median 并将其存储在列表中。

But, as the dimensionality of the matrix grows, the time complexity will shoot up as well.但是,随着矩阵维数的增加,时间复杂度也会上升。 Does Python have a better way to solve this? Python 有没有更好的方法来解决这个问题?

arr = np.array([[1, 2, 3],[2,3,4],[3,4,5]])
print(arr)
array([[1, 2, 3],
       [2, 3, 4],
       [3, 4, 5]])

Expected output:预期 output:

2,3,4

Use numpy.median() across columns.跨列使用 numpy.median()。

arr = np.array([[1, 2, 3],[2,3,4],[3,4,5]])
print(arr)
array([[1, 2, 3],
       [2, 3, 4],
       [3, 4, 5]])

np.median(arr,axis=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM