Python中np.random.normal的使用

np.random.normal(n,σ,size)主要参数分别是平均数,均方差,数组长度

功能是生成长度为size的以n为中心,σ为均方差的正太分布的一组数据

用代码理解一下

import numpy as np

y = np.random.normal(10, 1, 10000)


# 正太分布的特点
count = 0
for i in y:
    if abs(i - 10) <= 1:  #1σ
        count += 1
print(count / len(y))

count = 0
for i in y:
    if abs(i - 10) <= 2:  #2σ
        count += 1
print(count / len(y))

count = 0
for i in y:
    if abs(i - 10) <= 3:   #3σ
        count += 1
print(count / len(y))

y是以10位中心(平均数),均方差为1,有10000个数据的array数组

我们知道正太分布中,平均数加正负1,2,3倍标准偏差的概率,分别68.3%,95.5%,99.7%

输出:

E:\python\Nuclear_Signal\venv\Scripts\python.exe E:\python\Nuclear_Signal\实验8.py 
0.6804
0.9545
0.9978

可以看到是符合规律的。

全部代码:(个人学习记录用)

import numpy as np

y = np.random.normal(10, 1, 10000)

#
# equal = 0
# for i in y:
#     equal += i
# equal = equal / len(y)
# print(equal)
#
# variance = 0
# for i in y:
#     variance += pow((i - equal), 2)
# variance = variance / (len(y)-1)
# print(pow(variance, 0.5))

# 正太分布的特点
count = 0
for i in y:
    if abs(i - 10) <= 1:
        count += 1
print(count / len(y))

count = 0
for i in y:
    if abs(i - 10) <= 2:
        count += 1
print(count / len(y))

count = 0
for i in y:
    if abs(i - 10) <= 3:
        count += 1
print(count / len(y))

你可能感兴趣的:(python,机器学习,开发语言)