给定每个变量的概率,选择列表变量 [英] Choose list variable given probability of each variable

查看:43
本文介绍了给定每个变量的概率,选择列表变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试编写一个在中间使用 softmax 激活函数的程序.

I've been trying to code a program that uses the softmax activation function in the middle.

现在,我有一个这样的概率列表:

Right now, I have a list of probabilities like this:

P[0.10,0.25,0.60,0.05]

P 中所有变量的总和始终为 1.

The sum of all the variables in P is always 1.

我想要一种方法来选择列表的索引给定概率.或者,换句话说,一个返回

I wanted a way to pick the index of the list given the probability attached to it. Or, in other words, a function that returned

0 - 10% of the time
1 - 25% of the time
2 - 60% of the time
3 - 5% of the time

我完全不知道从哪里开始.任何帮助,将不胜感激.:)

I've absolutely no idea where to start on this. Any help would be appreciated. :)

推荐答案

您可以使用 numpy 轻松实现这一目标.它有一个 choice 函数,它接受概率参数.

You can easily achieve this with numpy. It has a choice function which accepts the parameter of probabilities.

np.random.choice(
  ['pooh', 'rabbit', 'piglet', 'Christopher'], 
  5,
  p=[0.5, 0.1, 0.1, 0.3]
)

这篇关于给定每个变量的概率,选择列表变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆