python - Generating approximately unique set of numbers from numpy array
问题描述
I want to generate unique smaller arrays from numpy array.I used numpy.random.choice which does'nt seem to produce unique array. Here is my code :
sample=np.array([5,6,1,8,9,2,10,4,3,17,11,19,7,15])
for i in range(30):
sample=np.random.choice(sample,3,replace=True)
print(sample)
Here is the output
[8 5 4]
[4 8 4]
[4 8 4]
[4 4 4]
[4 4 4]
[4 4 4]
[4 4 4]
[4 4 4]
[4 4 4]
[4 4 4]
.
.
.
.
Goes on like that
I want it to be at least approximately unique or uniformly distributed. But here [4,4,4] dominates. What I am doing wrong?
解决方案
You reset sample
in the middle of your loop, after which there is not much randomness to be had.
Use something like
foo=np.random.choice(sample,3,replace=True)
推荐阅读
- c# - 如何在 C# 中分配/编辑单独类中的变量?
- jquery - 从 data-actions 属性中获取值
- android - Kotlin:在 App 类中定义协程范围
- c# - MemoryMappedFile:为什么 CreateNew 在 Dispose 之后抛出?
- powershell - 解析导入 csv 并迭代管道值
- kubernetes-helm - 在 Azure Kubernetes 服务上安装 ISTIO 时将注解(创建私有负载均衡器)传递给 helm
- elasticsearch - 分析日志文件并显示结果的简单方法
- sqlite - 如何在一个查询中执行多个查询?
- docker - Docker for windows“服务器行为不端”从不同域的机器中提取图像时出错
- typescript - In Typescript is there a way to constrain an object to be a sub of another object without typing all fields as undefined?