- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我基本上正在尝试进行这种蒙特卡洛分析,其中我将实验中的参与者随机重新分配到新组,然后重新分析给定随机新组的数据。这就是我想要做的事情:
参与者最初被分为八组,每组四名参与者。我想将每个参与者随机重新分配到一个新组,但我不希望任何参与者最终与来自同一原始组的另一个参与者一起加入新组。
这是我的进展:
import random
import pandas as pd
import itertools as it
data = list(it.product(range(8),range(4)))
test_df = pd.DataFrame(data=data,columns=['group','partid'])
test_df['new_group'] = None
for idx, row in test_df.iterrows():
start_group = row['group']
takens = test_df.query('group == @start_group')['new_group'].values
fulls = test_df.groupby('new_group').count().query('partid >= 4').index.values
possibles = [x for x in test_df['group'].unique() if (x not in takens)
and (x not in fulls)]
test_df.loc[idx,'new_group'] = random.choice(possibles)
这里的基本想法是,我将参与者随机重新分配到一个新组,其约束条件是(a)新组中没有原始组伙伴之一,并且(b)新组中没有已重新分配 4 个或更多参与者。
这种方法的问题在于,很多时候,当我们尝试重新分配最后一个组时,唯一剩余的组槽位位于同一组中。我也可以在失败时尝试重新随机化,直到成功为止,但这感觉很愚蠢。另外,我想进行 100 次随机重新分配,因此这种方法可能会变得非常慢......
所以必须有一种更聪明的方法来做到这一点。我还觉得应该有一种更简单的方法来解决这个问题,因为目标感觉很简单(但我意识到这可能会产生误导......)
最佳答案
经过深思熟虑,我发现了一个明显更好的解决方案,位于~ Big O of numGroups
。
import random
import numpy as np
import pandas as pd
import itertools as it
np.random.seed(0)
numGroups=4
numMembers=4
data = list(it.product(range(numGroups),range(numMembers)))
df = pd.DataFrame(data=data,columns=['group','partid'])
g = np.repeat(range(numGroups),numMembers).reshape((numGroups,numMembers))
In [95]: g
Out[95]:
array([[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]])
g = np.random.permutation(g)
In [102]: g
Out[102]:
array([[2, 2, 2, 2],
[3, 3, 3, 3],
[1, 1, 1, 1],
[0, 0, 0, 0]])
g = np.tile(g,(2,1))
In [104]: g
Out[104]:
array([[2, 2, 2, 2],
[3, 3, 3, 3],
[1, 1, 1, 1],
[0, 0, 0, 0],
[2, 2, 2, 2],
[3, 3, 3, 3],
[1, 1, 1, 1],
[0, 0, 0, 0]])
注意对角线。
array([[2, -, -, -],
[3, 3, -, -],
[1, 1, 1, -],
[0, 0, 0, 0],
[-, 2, 2, 2],
[-, -, 3, 3],
[-, -, -, 1],
[-, -, -, -]])
从上到下画对角线。
newGroups = []
for i in range(numGroups):
newGroups.append(np.diagonal(g[i:i+numMembers]))
In [106]: newGroups
Out[106]:
[array([2, 3, 1, 0]),
array([3, 1, 0, 2]),
array([1, 0, 2, 3]),
array([0, 2, 3, 1])]
newGroups = np.ravel(newGroups)
df["newGroups"] = newGroups
In [110]: df
Out[110]:
group partid newGroups
0 0 0 2
1 0 1 3
2 0 2 1
3 0 3 0
4 1 0 3
5 1 1 1
6 1 2 0
7 1 3 2
8 2 0 1
9 2 1 0
10 2 2 2
11 2 3 3
12 3 0 0
13 3 1 2
14 3 2 3
15 3 3 1
结果比我想象的要困难得多......
我有一种蛮力方法,基本上可以猜测组的不同排列,直到最终得到一个每个人最终都在不同组中的排列。与您所展示的方法相比,这种方法的好处是它不会遭受“最后用完组”的问题。
它可能会变慢 - 但对于 8 个组和每个组 4 个成员来说,速度很快。
import random
import numpy as np
import pandas as pd
import itertools as it
random.seed(0)
numGroups=4
numMembers=4
data = list(it.product(range(numGroups),range(numMembers)))
df = pd.DataFrame(data=data,columns=['group','partid'])
g = np.repeat(range(numGroups),numMembers).reshape((numGroups,numMembers))
In [4]: g
Out[4]:
array([[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]])
def reArrange(g):
g = np.transpose(g)
g = [np.random.permutation(x) for x in g]
return np.transpose(g)
# check to see if any members in each old group have duplicate new groups
# if so repeat
while np.any(np.apply_along_axis(lambda x: len(np.unique(x))<numMembers,1,g)):
g = reArrange(g)
df["newGroup"] = g.ravel()
In [7]: df
Out[7]:
group partid newGroup
0 0 0 2
1 0 1 3
2 0 2 1
3 0 3 0
4 1 0 0
5 1 1 1
6 1 2 2
7 1 3 3
8 2 0 1
9 2 1 0
10 2 2 3
11 2 3 2
12 3 0 3
13 3 1 2
14 3 2 0
15 3 3 1
关于python - 将参与者随机重新分配到组中,以便最初来自同一组的参与者最终不会出现在同一组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42602014/
我需要并行处理多个数据值(“SIMD”)。我可以使用 java.util.concurrent API (Executors.newFixedThreadPool()) 使用 Future 实例并行处
说未引用的参与者仍然订阅事件流是否正确?至少,这是我从 Akka 实验中得到的…… 我正在尝试在 EventBus 场景中为参与者实现弱引用。在这些情况下,事件监听器/参与者通常来来去去。与应该一直在
场景:我有这个代码: class MyActor extends Actor { def act() { react { case Message() => printl
我正在尝试对一条新闻帖子进行建模,其中包含有关发布该新闻的用户的信息。我认为最好的方法是发送用户摘要信息以及创建新闻帖子的消息,但我有点困惑如果底层用户信息发生变化如何更新该摘要信息。现在我有以下 N
开始学习Actor模型(使用Akka.net),打算在现在的项目中使用。我当前的任务是使用 IFilter 从不同的文件中提取文本。 IFilter 是一组带有 native 代码的 COM 服务器。
我在我的 Linux 服务器上安装了 openfire(版本:Openfire 3.9.3)。它在 2 天前工作正常,具有所有功能。我专注于存档消息插件。什么是问题: 1.当我开始与用户聊天时,它显示
感谢阅读这个问题。我有一个包含 5 个参与者的数据集,其中 10 个参与者有 8 个变量。我需要将数据集的每个参与者/行导出到一个单独的 pdf 文件中,并根据参与者名称为导出的 pdf 文件命名。附
在 Twilio REST API 中,是否可以检索已完成 session 的参与者列表?我试图在 session 结束时计算每个参与者的通话时长。我在 session 创建时传递了一个“ Actio
当使用 LDAP 存储实现 RBAC 模型时(我使用 Apache Directory 1.0.2 作为测试平台),一些参与者显然可以映射到特定的对象类: 资源 - 我没有看到此资源的清晰映射。 ap
我需要将数千个不同格式的 ms office 文档转换为一种通用格式。为了加快速度,我会使用 akka.net 将其并行化。 WordSaveAsActor 应该: 在单线程单元中运行 持有 Word
现在我们的 ASF 集群正在运行: Web API 项目 - 无状态且面向公众 Actor 项目 - 主要是 volatile 的,将数据保存在内存中,由某些 API 使用 我们正在试用 Applic
我在 R 中有一个 data.frame,其中一列指示实验中的参与者 subject,另一列指示 condition、trial_type 和最后一列,我的数字因变量 rt. 这是我使用 dput()
APARAPI project self 描述为: Aparapi allows Java developers to take advantage of the compute power of G
我正在使用 Vidyo.io 服务将视频 session 集成到我的 Android 应用程序中。我已经能够使用 android-vidyo-sdk 附带的 VidyoConnector 示例应用程序
只是一个小介绍: 有任务(任务表) 有用户(用户表) 用户可以参与不同的任务(task_has_user表) 参与者分为作者、实现者、检查者等。 我想获取以逗号分隔的参与者的所有任务列表,如下所示(数
我正在尝试实现 JUnit 测试来测试参与者。 我有这个 ActorTest : import org.junit.Test; import play.libs.Akka; import playte
我是一名优秀的程序员,十分优秀!