python,加权linspace [英] python, weighted linspace

查看:38
本文介绍了python,加权linspace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我最好的方法是生成一个包含 0 到 100 的(numpy)数组,该数组由一个(例如)均值为 50 和方差为 5 的正态分布函数加权.所以有更多50s 和更少(几乎没有)零和数百.我认为这个问题不应该太难解决,但我不知何故被卡住了......

can anyone show me what the best way is to generate a (numpy) array containig values from 0 to 100, that is weighted by a (for example) normal distribution function with mean 50 and variance 5. So that there are more 50s and less (nearly no) zeros and hundreds. I think the problem should not be too hard to solve, but I'm stucked somehow...

我想过使用 np.linspace 的一些东西,但似乎没有权重选项.

I thought about something with np.linspace but it seems, that there is no weight option.

所以要明确一点:我不想要一个从 0 到 100 的简单正态分布,而是想要一个从 0 到 100 的数组,中间值的密度更高.

So just to be clear: I don't wan't a simple normal distribution from 0 to 100, but something like an array from 0 to 100 with higher density of values in the middle.

谢谢

推荐答案

重要的是要了解,您的问题不是完全可解决的,因为通常有限离散样本不能完全重现您的分布.

It is important to understand, that your problem is not exactly solvable, since generally a finite discrete sample cannot exactly reproduce your distribution.

当您询问问题的琐碎版本时,例如 [0,1] 中具有相等分布的 3 个值的集合时,您可以轻松看到这一点.这里的结果 [0,0,1] 和 [0,1,1] 都是合理的.

You can easily see this, when asking trivial versions of your question like a set of 3 values in [0,1] with an equal distribution. Here the results [0,0,1] and [0,1,1] would both be reasonable.

但是,您可以粗略地解决这个问题.如果您要求一个包含 [0,1,...,N] 中的 count 个元素的数组,其中给定的概率是 p=[p0,p1,...,pN] 并归一化 (p0+...+pN==1) 然后元素 k<的计数 c_k/code> 在你的结果数组中理论上是

However, you can solve the problem roughly. If you ask for an array with count elements out of [0,1,...,N] where the given probabilities are p=[p0,p1,...,pN] and normalized (p0+...+pN==1) then the count c_k of the element k in your resulting array is theoretically

c[k] = p[k]*count

但这些计数现在是浮点数.您必须决定一种方法来舍入"它们,同时保持它们的总和.这是由于您的问题定义不明确而导致的选择自由.

but these counts now are floats. You have to decide for a way to "round" them while keeping their total sum. This is the freedom of choice arising from the under-definedness of your question.

这篇关于python,加权linspace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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