Python numpy.random.normal [英] Python numpy.random.normal
问题描述
我生成了均值为0且方差为1(np.random.normal)的随机20个数字.我计算了两次方差ddof = 1和0.
I generated random 20 numbers with mean 0 and variance 1 (np.random.normal). I calculated the variance twice ddof = 1 and 0.
我的问题是我想将(平均值0和方差1)添加到(np.random.normal),但是在该网站上没有提及方差
My question is i am trying to add (mean 0 and variance 1) to (np.random.normal), However on there website is no mention for the variance https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.normal.html
loc : float Mean ("centre") of the distribution.
scale : float Standard deviation (spread or "width") of the distribution.
size : int or tuple of ints, optional
所以我可以这样做
mu, sigma = 0, math.sqrt(1)
x = np.random.normal(mu, sigma, 20)
因为我必须分别执行90次和20个数字的估算,然后重新计算一次
Because i have to perform the estimation in 90 times and 20 numbers each time and recount again
a = np.random.rand(90, x)
这是完整的代码
import math
import numpy as np
import pandas as pd
mu, sigma = 0, math.sqrt(1)
x = np.random.normal(mu, sigma, 20)
#caluclateing the unbiased_estimator and the biased_estimator
unbiased_estimator = np.var(x, ddof=1)
biased_estimator = np.var(x, ddof=0)
print ("Unbiased_estimator : ",unbiased_estimator)
print ("Biased_estimator : ", biased_estimator)
a = np.random.rand(90, x)
#caluclateing the unbiased_estimator and the biased_estimator
unbiased_estimator_for_each_20 = np.var(a, ddof=1, axis=1)
biased_estimator_for_each_20 = np.var(a, ddof=0, axis=1)
print (unbiased_estimator_for_each_20 )
print(" ")
print (biased_estimator_for_each_20 )
推荐答案
定义:variance = (standard deviation)^2
,然后是standard deviation = sqrt(variance)
,因此:
the definition: variance = (standard deviation)^2
, then standard deviation = sqrt(variance)
, in consequence:
import numpy as np
mean = 0,
variance = 1,
np.random.normal(loc = mean, scale= np.sqrt(variance), 20)
#caluclateing the unbiased_estimator and the biased_estimator
unbiased_estimator = np.var(x, ddof=1)
biased_estimator = np.var(x, ddof=0)
print ("Unbiased_estimator : ",unbiased_estimator)
print ("Biased_estimator : ", biased_estimator)
输出:
Unbiased_estimator : 1.08318083742
Biased_estimator : 1.02902179555
这篇关于Python numpy.random.normal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!