python - 3d numpy 数组的模式/中值/平均值

标签 python numpy scipy

我有一个 3d numpy 数组,我的目标是获取它的均值/众数/中值。

它的形状是 [500,300,3]

我想得到例如:

[430,232,22]作为模式

有没有办法做到这一点?标准的 np.mean(array) 给了我一个非常大的数组。

不知道这样说对不对?

weather_image.mean(axis=0).mean(axis=0)

它给了我一个长度为 3 的一维 np 数组

最佳答案

您想获得前两个轴的均值/中值/众数。这应该有效:

data = np.random.randint(1000, size=(500, 300, 3))

>>> np.mean(data, axis=(0, 1)) # in nunpy >= 1.7
array([ 499.06044   ,  499.01136   ,  498.60614667])
>>> np.mean(np.mean(data, axis=0), axis=0) # in numpy < 1.7
array([ 499.06044   ,  499.01136   ,  498.60614667])
>>> np.median(data.reshape(-1, 3), axis=0)
array([ 499.,  499.,  498.]) # mode
>>> np.argmax([np.bincount(x) for x in data.reshape(-1, 3).T], axis=1)
array([240, 519, 842], dtype=int64)

请注意,np.median 需要一个展平数组,因此需要 reshape 。 bincount 只处理一维输入,因此列表理解,加上一些用于解包的转置魔法。

关于python - 3d numpy 数组的模式/中值/平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21617194/

相关文章:

python - 如何测试我的分类器是否过度拟合?

python - 在 Flask 中使用 WTForms 验证 GET 参数

python - Numpy 从文本文件中读取复数

python - MFCC采样频率

Python:导入错误:没有名为_md5 的模块

python - 从python列表中删除元素

python - 计算数组中相同的元素并创建字典

python - `ndarray.flags[' OWNDATA' ]`, ` ndarray.base`、 `id(ndarray)` 和 `ndarray.__array_interface__[' data']` 有什么不同?

python - Scipy fmin_slsqp 错误 "failed in converting 8th argument ` g' of _slsqp.slsqp to C/Fortran array"

python - Scipy.sparse.csr_matrix : How to get top ten values and indices?