代码块是:
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:axis 1超出了维1数组的范围。如果我注释掉这一行,med和mean运行良好,没有轴错误。为什么会发生这种情况,如何让它计算长度2048轴上的中位绝对偏差?
发布于 2020-07-09 21:34:44
我没有确切的数组,但我尝试复制您的代码以生成错误:
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()中的参数median_absolute_deviation(),您实际上是在请求函数从数组temps中删除所有的np.nan值。
如上面所示,如果这些值有一整列,则通过调用nan_policy='omit'删除它们,因此数组temps被简化为维1的数组,并且会出现错误。
https://stackoverflow.com/questions/62823284
复制相似问题