简体   繁体   English

2D 数组的 median_absolute_deviation 抛出 AxisError: axis 1 is out of bounds for array of dimension 1

[英]median_absolute_deviation of 2D array throws AxisError: axis 1 is out of bounds for array of dimension 1

The chunk of code is:代码块是:

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 is (992,2048) and temps.ndim is 2 temps.shape是 (992,2048) 并且temps.ndim是 2

but line 3 using median_absolute_deviation throws AxisError : axis 1 is out of bounds for array of dimension 1. If I comment out the line, med and mean run fine with no axis error.但是使用median_absolute_deviation的第 3 行抛出AxisError :轴 1 超出了维度 1 数组的范围。如果我注释掉该行,则medmean运行良好,没有轴错误。 Why is this happening and how do I get it to compute the median absolute deviation along the axis of length 2048?为什么会发生这种情况,我如何让它计算沿长度 2048 轴的中值绝对偏差?

I do not have your exact array, but I have tried replicating your code to produce your error:我没有您的确切数组,但我尝试复制您的代码以产生您的错误:

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)

With the argument nan_policy='omit' in median_absolute_deviation() you are effectively asking the function to remove all the np.nan values from the array temps .使用median_absolute_deviation()中的参数nan_policy='omit' ,您实际上是在要求 function 从数组temps中删除所有np.nan值。

As I have shown above, if there is an entire column of these values, they are removed by calling nan_policy='omit' and so the array temps is reduced to an array of dimension 1 and the errors you see arise.正如我在上面所展示的,如果有一整列的这些值,它们会通过调用nan_policy='omit'被删除,因此数组temps被缩减为一个维度为 1 的数组,并且您看到的错误就会出现。

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

相关问题 遇到 AxisError:轴 1 超出维度 0 数组的范围 - Encountering AxisError: axis 1 is out of bounds for array of dimension 0 AxisError:轴 1 超出维度 1 数组的范围 - AxisError: axis 1 is out of bounds for array of dimension 1 numpy.AxisError:轴 1 超出维度 1 数组的范围 - numpy 数组 - numpy.AxisError: axis 1 is out of bounds for array of dimension 1 - numpy array 如何解决 AxisError:轴 1 超出维度 0 数组的范围 - How to solve AxisError: axis 1 is out of bounds for array of dimension 0 numpy.AxisError:来源:轴 2 超出了维度 2 数组的范围 - numpy.AxisError: source: axis 2 is out of bounds for array of dimension 2 np.linalg.norm AxisError:轴 1 超出维度 1 数组的范围 - np.linalg.norm AxisError: axis 1 is out of bounds for array of dimension 1 AxisError:计算类的准确性时,轴 1 超出维度 1 数组的范围 - AxisError: axis 1 is out of bounds for array of dimension 1 when calculating accuracy of classes AxisError:计算 roc_auc_score 时,轴 1 超出维度 1 数组的范围 - AxisError: axis 1 is out of bounds for array of dimension 1 when calculating roc_auc_score 分类器:轴 1 超出维度 1 数组的范围 - classifier : axis 1 is out of bounds for array of dimension 1 索引超出2D数组的范围? - Index out of bounds for a 2D Array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM