![](/img/trans.png)
[英]Encountering AxisError: axis 1 is out of bounds for array of dimension 0
[英]median_absolute_deviation of 2D array throws AxisError: axis 1 is out of bounds for array of dimension 1
代码块是:
import scipy as sp
import numpy as np
print(temps.shape)
print(temps.ndim)
mad = sp.stats.median_absolute_deviation(temps, axis=1, nan_policy='omit')
med = np.median(temps, axis=1)
mean = np.mean(temps,axis=1)
temps.shape
是 (992,2048) 并且temps.ndim
是 2
但是使用median_absolute_deviation
的第 3 行抛出AxisError
:轴 1 超出了维度 1 数组的范围。如果我注释掉该行,则med
和mean
运行良好,没有轴错误。 为什么会发生这种情况,我如何让它计算沿长度 2048 轴的中值绝对偏差?
我没有您的确切数组,但我尝试复制您的代码以产生您的错误:
from scipy.stats import median_absolute_deviation
import numpy as np
# mimicking your data with random numbers
temps = np.random.normal(0.0, 1.0, size=(992,2048))
# setting values of axis 1 to NaN
temps[:,1] = np.nan
print(temps.shape)
print(temps.ndim)
mad = median_absolute_deviation(temps, axis=1, nan_policy='omit')
med = np.median(temps, axis=1)
mean = np.mean(temps,axis=1)
使用median_absolute_deviation()
中的参数nan_policy='omit'
,您实际上是在要求 function 从数组temps
中删除所有np.nan
值。
正如我在上面所展示的,如果有一整列的这些值,它们会通过调用nan_policy='omit'
被删除,因此数组temps
被缩减为一个维度为 1 的数组,并且您看到的错误就会出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.