亲宝软件园·资讯

展开

Python概率曲线

hhh江月 人气:0

曲线一

解释

这里是使用matplotlib来绘制正态分布的曲线。

代码实现

import numpy as np
import matplotlib.pyplot as plt


def test1(n, m=500):
    out = []
    result = np.random.normal(1, 5, n * m)
    print(result)
    for i in range(m):
        average0 = 0
        for j in range(n):
            average0 += result[n * i  + j] 
            if j == n - 1:
                out.append(average0 / n)
                average0 = 0
    print(out)
    
    plt.hist(out,bins=25) 
    plt.title("test (1)")
    plt.xlabel("x")
    plt.ylabel("rate")    
    plt.show()

 
test1(5)

曲线二

解释

这里使用了matplotlib.pyplot来实现指数分布的绘制,具体的代码实现参见下面所示:

代码实现

import numpy as np
import matplotlib.pyplot as plt

def test2(n, m=500):
    out0 = []
    result0 = np.random.exponential(scale=1, size=n * m)
    # print(result0)
    for i in range(m):
        average000 = 0
        for j in range(n):
            average000 += result0[n * i  + j] 
            if j == n - 1:
                out0.append(average000 / n)
                average000 = 0
    # print(out0)
    
    plt.hist(out0,bins=25)     
    plt.show()
test2(5)

曲线三

代码实现

import numpy as np
import matplotlib.pyplot as plt
def test3(n1, m111=500):
    out11 = []
    # np.random.standard_t
    result11 = np.random.standard_t(1, size=n1 * m111)
    #  print(result)
    for i in range(m111):
        average0 = 0
        for j in range(n):
            average0 += result11[n1 * i  + j] 
            if j == n - 1:
                out11.append(average0 / n1)
                average0 = 0
    #  print(out11)
    
    plt.hist(out11,bins=20) 
    plt.title("test (3)")   
    plt.show() 
test3(30)

加载全部内容

相关教程
猜你喜欢
用户评论