我在写Wilcoxon秩和测试的扩展,它要求我先写这个测试的基本函数,这也意味着我不能在这个练习中使用SciPy。在
我有基本的基本代码,但我有困难平均排名平局。这是我的代码:
#read in data
m1 = [0,0,0,0,0,2,3,3,3,4,4,5,6,10,10,10,11,12,15,15,15,20,22,25,25,27,30]
w1 = [0,0,0,0,0,0,1,3,3,3,3,7,8,8,19,20,27,30]
#convert to tuples, incl where they came from
m1t = []
for m in m1:
m1t.append((m, "m1"))
w1t = []
for w in w1:
w1t.append((w, "w1"))
all1t = m1t + w1t #combine
all1ts = sorted(all1t, key=lambda tup: tup[0]) #sort
all1tsr = [row+(i,) for i,row in enumerate(all1ts,0)] #rank
#revert to back to original grouping
m1r = [i for i in all1tsr if i[1]=="m1"]
w1r = [i for i in all1tsr if i[1]=="w1"]
这是电流输出:
^{pr2}$每个元组的元素1是对它们进行排序的值,元素2只是一个标识符,而元素3是按元素1排序时的排名。有10个观察值以“0”作为元素1,现在它们都被分配了升序,但我想以某种方式平均这些级别(将所有这些级别都赋值为5级)。在
换句话说,我想要这个:
[(0, 'm1', 5),
(0, 'm1', 5),
(0, 'm1', 5),
(0, 'm1', 5),
(0, 'm1', 5),
(0, 'w1', 5),
(0, 'w1', 5),
(0, 'w1', 5),
(0, 'w1', 5),
(0, 'w1', 5),
(0, 'w1', 5),
(1, 'w1', 11),
(2, 'm1', 12),
(3, 'm1', 13.5),
(3, 'm1', 13.5)]
欢迎所有反馈,谢谢
对于初学者,我将以一种更短的方式得到
all1ts
:然后我将使用^{} ,它基本上是为这样做而设计的。在
^{pr2}$在你的测试数据上运行得到的结果是:
我想这正是你想要的。在
相关问题 更多 >
编程相关推荐