Python——正態分佈



正態分佈是一種透過排序資料中每個值機率分佈,展示資料形式的方法。大多數值位於平均值附近,形成了對稱圖案。

我們在numpy函式庫中使用不同的函式,用數學方法計算正態分佈的值。我們在上面繪製了機率分佈曲線來建立直方圖。

import matplotlib.pyplot as plt
import numpy as np

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

# Create the bins and histogram
count, bins, ignored = plt.hist(s, 20, normed=True)

# Plot the distribution curve
plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *
    np.exp( - (bins - mu)**2 / (2 * sigma**2) ),       linewidth=3, color='y')
plt.show()

它的輸出如下−

normdist.png
廣告
© . All rights reserved.