如何将参数动态传递给 anova 测试?

Moh*_* ah 2 python feature-extraction scipy anova pandas

我有一个 df,从这个 df 我想传递 Anova 测试的参数。但问题是 df 值是动态的。如何将参数传递给scipy.stats.f_oneway这个。

例如:

    num    cat
0   164  type1
1   172  type1
2   168  type1
3   177  type1
4   156  type1
5   195  type1
6   178  type2
7   191  type2
8   197  type2
9   182  type2
10  185  type2
11  177  type2
12  175  type3
13  193  type3
14  178  type3
15  171  type3
16  163  type3
17  176  type3
18  155  type4
19  166  type4
20  149  type4
21  164  type4
22  170  type4
23  168  type4
Run Code Online (Sandbox Code Playgroud)

我必须传递如下值,

t1 = [164, 172, 168, 177, 156, 195]
t2 = [178, 191, 197, 182, 185, 177]
t3 = [175, 193, 178, 171, 163, 176]
t4 = [155, 166, 149, 164, 170, 168]

F, p = stats.f_oneway(t1,t2,t3,t4)
Run Code Online (Sandbox Code Playgroud)

在上述方法中,我必须将每种类型的值存储到单独的变量中。但我想避免这种情况。因为我的价值观是动态的。例如,上面的示例 df 只有 4 种类型,这里这 4 种是动态的,它在运行时可以是任何东西。

到目前为止,我可以使用下面的方法将值放入列表中。

result = df.groupby(1)[0].apply(list).values.tolist()
Run Code Online (Sandbox Code Playgroud)

但我不知道如何将这个值传递给scipy.stats.f_oneway.

请给出解决这个问题的好方法。

jez*_*ael 5

使用*拆包列表

result = df.groupby('cat')['num'].apply(list)
print (result)
cat
type1    [164, 172, 168, 177, 156, 195]
type2    [178, 191, 197, 182, 185, 177]
type3    [175, 193, 178, 171, 163, 176]
type4    [155, 166, 149, 164, 170, 168]
Name: num, dtype: object

F, p = scipy.stats.f_oneway(*result)
print (F)
5.406342913776015
print (p)
0.0068759477547351
Run Code Online (Sandbox Code Playgroud)