【问题标题】:Scipy Stats Wilcoxon X and Y must be one dimensional errorScipy Stats Wilcoxon X 和 Y 必须是一维误差
【发布时间】:2020-05-10 07:37:53
【问题描述】:

我根据 'yr' 值中的值将 'cnt' 变量分成两组,以便我可以将两者与 Wilcoxon 秩和检验进行比较。我不断收到错误“样本 x 和 y 必须是一维的”。谁能帮我弄清楚如何解决这个问题?我定义了两个数组 cnt_yr0 和 cnt_yr1,它们是 1xn 数组。

#Here is some of my code:
from scipy import stats as sc

cnt_yr0 = np.transpose(np.array(data.loc[data['yr']==0,['cnt']]))
cnt_yr1 = np.transpose(np.array(data.loc[data['yr']==1,['cnt']]))
print(cnt_yr0)
print(cnt_yr1)


#binary predictors with continuous response
print(sc.wilcoxon(cnt_yr0,cnt_yr1))

[[16 40 32 ... 52 38 31]]
[[48 93 75 ... 90 61 49]]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-145-16e4c5da6eb0> in <module>
      9 
     10 #binary predictors with continuous response
---> 11 print(sc.wilcoxon(cnt_yr0,cnt_yr1))
     12 print(sc.wilcoxon(np.array(data['holiday']),np.array(data['cnt'])))
     13 print(sc.wilcoxon(np.array(data['workingday']),np.array(data['cnt'])))

~\Anaconda3\lib\site-packages\scipy\stats\morestats.py in wilcoxon(x, y, zero_method, correction, alternative)
   2844         x, y = map(asarray, (x, y))
   2845         if x.ndim > 1 or y.ndim > 1:
-> 2846             raise ValueError('Samples x and y must be one-dimensional.')
   2847         if len(x) != len(y):
   2848             raise ValueError('The samples x and y must have the same length.')

ValueError: Samples x and y must be one-dimensional.

【问题讨论】:

  • [[ 表示这是一个形状为 (1,n) 的二维数组。您需要使用索引或reshape() 转换为一维
  • 我应该把它改造成什么?

标签: python python-3.x pandas numpy scipy


【解决方案1】:

给定的数组是二维的,函数只接受一维数组

np.array([[16, 40, 32, 52, 38, 31]]).shape

(1,6)

但您可以使用索引或 array.flatten() 删除第二个维度

np.array([[16, 40, 32, 52, 38, 31]])[0,:].shape

(6,)

np.array([[16, 40, 32, 52, 38, 31]]).flatten().shape

(6,)

也可以用ndim验证得到维数:

np.array([[16, 40, 32, 52, 38, 31]]).flatten().ndim

1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 2017-10-23
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多