创建Kruskal-Wallis H-test python的向量 [英] create vectors for Kruskal-Wallis H-test python
问题描述
我有如下数据集
df = pd.DataFrame({'numbers':range(9), 'group':['a', 'b', 'c']*3})
group numbers
0 a 0
1 b 1
2 c 2
3 a 3
4 b 4
5 c 5
6 a 6
7 b 7
8 c 8
我想创建向量
a = [0, 3, 6]
b = [1, 4, 7]
c = [2, 5, 8]
对于Kruskal-Wallis H-test python
for Kruskal-Wallis H-test python
stats.kruskal(a, b, c)
或可能类似于R(数字〜组)
or maybe analogue as in R (numbers ~ group)
推荐答案
我不熟悉Kruskal-Wallis测试的任何特殊要求,但您可以通过将这些数组放入字典中来访问这些分组的数组方式:
I'm not familiar with any special requirements of the Kruskal-Wallis test, but you can access these grouped arrays via by putting them into a dictionary this way:
groupednumbers = {}
for grp in df['group'].unique():
groupednumbers[grp] = df['numbers'][df['group']==grp].values
print(groupednumbers)
*** {'c': array([2, 5, 8]), 'b': array([1, 4, 7]), 'a': array([0, 3, 6])}
也就是说,您可以通过明确地调用 groupingnumbers ['a']
等等,或通过列表获取您的向量:
That is, you'd get your vectors by either explicitly calling groupednumbers['a']
etc., or via a list:
args = groupednumbers.values()
...或如果您需要他们按顺序:
... or if you need them in an order:
args = [groupednumbers[grp] for grp in sorted(df['group'].unique())]
然后调用
stats.kruskal(*args)
或者如果您需要实际列表,您可以执行列表(df ['numbers'] [...]。
。)
Or if you need actual lists, you can do list(df['numbers'][...].values
.)
这篇关于创建Kruskal-Wallis H-test python的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!