【问题标题】:Python numpy.random.normalPython numpy.random.normal
【发布时间】:2016-12-06 17:11:23
【问题描述】:

我生成了 20 个随机数,均值为 0,方差为 1 (np.random.normal)。我计算了两次 ddof = 1 和 0 的方差。

我的问题是我正在尝试将(均值 0 和方差 1)添加到(np.random.normal),但是在网站上没有提及方差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 个数字的估计并重新计算

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 )

【问题讨论】:

  • “所以我可以这样做吗” - 不。这段代码有很多问题。
  • 什么意思?
  • 虚假的尾随逗号,试图分配给(std)**2,将(std)**2 传递给一个应该是标准偏差而不是方差的参数,没有将返回值分配给任何东西,变量名称不一致等.
  • user2357112 你怎么看这部分“a = np.random.rand(90, x)” 我知道它的磨损,因为它给出了错误。我在这里错过了什么?
  • 嗯,一方面,这不像how numpy.random.rand takes arguments

标签: python numpy


【解决方案1】:

定义:variance = (standard deviation)^2,然后是standard deviation = sqrt(variance),结果:

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

【讨论】:

    猜你喜欢
    • 2013-04-25
    • 1970-01-01
    • 2021-02-04
    • 2021-10-05
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    相关资源
    最近更新 更多