1

我根据“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.
4

1 回答 1

0

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

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
于 2020-01-24T16:24:08.727 回答