我试图创建1到1000之间的10,000个随机数的列表。但是我希望80-85%的数字是相同的类别(我的意思是大约100个数字其中应该出现在随机数列表中的80%的时间),其余的出现在大约15-20%的时间。任何想法,如果这可以在Python/NumPy/SciPy中完成。谢谢。Python中的加权随机数值列表
1
A
回答
1
这里有一个方法 -
a = np.arange(1,1001) # Input array to extract numbers from
# Select 100 random unique numbers from input array and get also store leftovers
p1 = np.random.choice(a,size=100,replace=0)
p2 = np.setdiff1d(a,p1)
# Get random indices for indexing into p1 and p2
p1_idx = np.random.randint(0,p1.size,(8000))
p2_idx = np.random.randint(0,p2.size,(2000))
# Index and concatenate and randomize their positions
out = np.random.permutation(np.hstack((p1[p1_idx], p2[p2_idx])))
让运行后的验证 -
In [78]: np.in1d(out, p1).sum()
Out[78]: 8000
In [79]: np.in1d(out, p2).sum()
Out[79]: 2000
2
这可以很容易地完成使用1致电random.randint()
选择一个列表和另一个电话random.choice()
在正确的列表上。我假设列表frequent
包含100个要选择的元素80
百分比和rare
包含900
个元素要选择的百分比20
百分比。
import random
a = random.randint(1,5)
if a == 1:
# Case for rare numbers
choice = random.choice(rare)
else:
# case for frequent numbers
choice = random.choice(frequent)
相关问题
- 1. 随机选择Python列表中的列表,取决于权重
- 2. 如何在Python中添加随机权重列表?
- 3. C中的随机加权#
- 4. PHP加权随机数
- 5. 列表中的python随机序列
- 6. R中的加权随机数生成
- 7. 随机加权阵列的权重小于1和小数
- 8. Python列表随机选择
- 9. Python中的随机列表选项
- 10. 从排序列表中加权随机选择
- 11. Python |如何添加元素添加到列表中随机
- 12. Python |避免从列表中随机选择以前的值
- 13. T-SQL中的随机加权选择
- 14. 加权随机在Golang
- 15. 随机加权子图
- 16. 随机加权选择
- 17. 非随机加权分配
- 18. 加权随机森林
- 19. Python - 如何添加一个随机数列表?
- 20. 不同权值的随机值
- 21. Python:列表和随机。如何从0到[列表中的字符串数量]之间选择随机数字?
- 22. 加权随机数生成C#
- 23. 熊猫,Python。替换列中的随机数值子集
- 24. Python随机排序的列表理解
- 25. Python中的随机函数
- 26. Python随机数
- 27. 添加列随机值Sequelize PostgreSQL的
- 28. 随机整数列表
- 29. Java加权随机机会集中在一个数字
- 30. 的Python:随机序列
你能更具体?这100个号码是否连续放置? – martianwars
不,他们可以分散,但应该在1和1000之间。 –
为什么你需要一个特殊功能呢?只需使用'random.randint()'2次。第一次选择100与其余的和下次选择其中 – martianwars