python生成正态分布,按顺序生成正态分布python,numpy

I am able to generate random samples of normal distribution in numpy like this.

>>> mu, sigma = 0, 0.1 # mean and standard deviation

>>> s = np.random.normal(mu, sigma, 1000)

But they are in random order, obviously. How can I generate numbers in order, that is, values should rise and fall like in a normal distribution.

In other words, I want to create a curve (gaussian) with mu and sigma and n number of points which I can input.

How to do this?

解决方案

To (1) generate a random sample of x-coordinates of size n (from the normal distribution) (2) evaluate the normal distribution at the x-values (3) sort the x-values by the magnitude of the normal distribution at their positions, this will do the trick:

import numpy as np

mu,sigma,n = 0.,1.,1000

def normal(x,mu,sigma):

return ( 2.*np.pi*sigma**2. )**-.5 * np.exp( -.5 * (x-mu)**2. / sigma**2. )

x = np.random.normal(mu,sigma,n) #generate random list of points from normal distribution

y = normal(x,mu,sigma) #evaluate the probability density at each point

x,y = x[np.argsort(y)],np.sort(y) #sort according to the probability density

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值