Hướng dẫn normal distribution curve in python - đường cong phân phối chuẩn trong python

Bạn có thể nhận được CDF dễ dàng. Vì vậy, PDF thông qua CDF

    import numpy as np
    import matplotlib.pyplot as plt
    import scipy.interpolate
    import scipy.stats

    def setGridLine(ax):
        #http://jonathansoma.com/lede/data-studio/matplotlib/adding-grid-lines-to-a-matplotlib-chart/
        ax.set_axisbelow(True)
        ax.minorticks_on()
        ax.grid(which='major', linestyle='-', linewidth=0.5, color='grey')
        ax.grid(which='minor', linestyle=':', linewidth=0.5, color='#a6a6a6')
        ax.tick_params(which='both', # Options for both major and minor ticks
                        top=False, # turn off top ticks
                        left=False, # turn off left ticks
                        right=False,  # turn off right ticks
                        bottom=False) # turn off bottom ticks

    data1 = np.random.normal(0,1,1000000)
    x=np.sort(data1)
    y=np.arange(x.shape[0])/(x.shape[0]+1)

    f2 = scipy.interpolate.interp1d(x, y,kind='linear')
    x2 = np.linspace(x[0],x[-1],1001)
    y2 = f2(x2)

    y2b = np.diff(y2)/np.diff(x2)
    x2b=(x2[1:]+x2[:-1])/2.

    f3 = scipy.interpolate.interp1d(x, y,kind='cubic')
    x3 = np.linspace(x[0],x[-1],1001)
    y3 = f3(x3)

    y3b = np.diff(y3)/np.diff(x3)
    x3b=(x3[1:]+x3[:-1])/2.

    bins=np.arange(-4,4,0.1)
    bins_centers=0.5*(bins[1:]+bins[:-1])
    cdf = scipy.stats.norm.cdf(bins_centers)
    pdf = scipy.stats.norm.pdf(bins_centers)

    plt.rcParams["font.size"] = 18
    fig, ax = plt.subplots(3,1,figsize=(10,16))
    ax[0].set_title("cdf")
    ax[0].plot(x,y,label="data")
    ax[0].plot(x2,y2,label="linear")
    ax[0].plot(x3,y3,label="cubic")
    ax[0].plot(bins_centers,cdf,label="ans")

    ax[1].set_title("pdf:linear")
    ax[1].plot(x2b,y2b,label="linear")
    ax[1].plot(bins_centers,pdf,label="ans")

    ax[2].set_title("pdf:cubic")
    ax[2].plot(x3b,y3b,label="cubic")
    ax[2].plot(bins_centers,pdf,label="ans")

    for idx in range(3):
        ax[idx].legend()
        setGridLine(ax[idx])

    plt.show()
    plt.clf()
    plt.close()


Để vẽ một phân phối bình thường trong Python, bạn có thể sử dụng cú pháp sau:

#x-axis ranges from -3 and 3 with .001 steps
x = np.arange(-3, 3, 0.001)

#plot normal distribution with mean 0 and standard deviation 1
plt.plot(x, norm.pdf(x, 0, 1))

Mảng X xác định phạm vi cho trục x và plt.plot () tạo ra đường cong cho phân phối bình thường với giá trị trung bình và độ lệch chuẩn được chỉ định.x array defines the range for the x-axis and the plt.plot() produces the curve for the normal distribution with the specified mean and standard deviation.

Các ví dụ sau đây cho thấy cách sử dụng các chức năng này trong thực tế.

Ví dụ 1: Vẽ một bản phân phối bình thường duy nhất

Mã sau đây cho thấy cách vẽ một đường cong phân phối bình thường duy nhất với giá trị trung bình là 0 và độ lệch chuẩn là 1:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

#x-axis ranges from -3 and 3 with .001 steps
x = np.arange(-3, 3, 0.001)

#plot normal distribution with mean 0 and standard deviation 1
plt.plot(x, norm.pdf(x, 0, 1))

Hướng dẫn normal distribution curve in python - đường cong phân phối chuẩn trong python

Bạn cũng có thể sửa đổi màu sắc và chiều rộng của dòng trong biểu đồ:

plt.plot(x, norm.pdf(x, 0, 1), color='red', linewidth=3)

Hướng dẫn normal distribution curve in python - đường cong phân phối chuẩn trong python

Ví dụ 2: Vẽ nhiều bản phân phối bình thường

Mã sau đây cho thấy cách vẽ nhiều đường cong phân phối bình thường với các phương tiện và độ lệch chuẩn khác nhau:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

#x-axis ranges from -5 and 5 with .001 steps
x = np.arange(-5, 5, 0.001)

#define multiple normal distributions
plt.plot(x, norm.pdf(x, 0, 1), label='μ: 0, σ: 1')
plt.plot(x, norm.pdf(x, 0, 1.5), label='μ:0, σ: 1.5')
plt.plot(x, norm.pdf(x, 0, 2), label='μ:0, σ: 2')

#add legend to plot
plt.legend()

Hướng dẫn normal distribution curve in python - đường cong phân phối chuẩn trong python

Hãy thoải mái sửa đổi màu sắc của các dòng và thêm nhãn tiêu đề và trục để làm cho biểu đồ hoàn thành:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

#x-axis ranges from -5 and 5 with .001 steps
x = np.arange(-5, 5, 0.001)

#define multiple normal distributions
plt.plot(x, norm.pdf(x, 0, 1), label='μ: 0, σ: 1', color='gold')
plt.plot(x, norm.pdf(x, 0, 1.5), label='μ:0, σ: 1.5', color='red')
plt.plot(x, norm.pdf(x, 0, 2), label='μ:0, σ: 2', color='pink')

#add legend to plot
plt.legend(title='Parameters')

#add axes labels and a title
plt.ylabel('Density')
plt.xlabel('x')
plt.title('Normal Distributions', fontsize=14)

Hướng dẫn normal distribution curve in python - đường cong phân phối chuẩn trong python

Tham khảo tài liệu matplotlib để biết giải thích chuyên sâu về hàm plt.plot ().plt.plot() function.

Làm thế nào để bạn tạo một đường cong phân phối bình thường trong Python?

Approach..
Nhập mô -đun ..
Tạo dữ liệu ..
Tính trung bình và độ lệch ..
Tính mật độ xác suất bình thường ..
Lô đất sử dụng các giá trị được tính toán ở trên ..
Hiển thị âm mưu ..

Làm thế nào để Python tính toán phân phối bình thường?

Chúng tôi khởi tạo đối tượng của định mức lớp với độ lệch trung bình và độ lệch chuẩn, sau đó sử dụng phương thức .cdf () truyền một giá trị lên đến đó chúng ta cần tìm giá trị xác suất tích lũy.Hàm phân phối tích lũy (CDF) tính toán xác suất tích lũy cho giá trị X nhất định.initialize the object of class norm with mean and standard deviation, then using . cdf( ) method passing a value up to which we need to find the cumulative probability value. The cumulative distribution function (CDF) calculates the cumulative probability for a given x-value.

Làm cách nào để tạo một đường cong chuông trong Python?

Cách tiếp cận: Chúng tôi sẽ lập danh sách các điểm trên trục x và vượt qua các điểm này bên trong hàm PDF tùy chỉnh của chúng tôi để tạo chức năng phân phối xác suất để tạo giá trị y tương ứng với từng điểm trong x.Bây giờ chúng tôi vẽ đường cong bằng các phương thức Plot () và Scatter () có sẵn trong thư viện matplotlib.plot the curve using plot() and scatter() methods that are available in the matplotlib library.

Làm thế nào để bạn vẽ một đường cong Gaussian trong Python?

Làm thế nào để vẽ một hàm phân phối Gaussian một chiều trong Python..
x_values = np.Arange (-5, 5, 0.1).
y_values = scipy.số liệu thống kê.định mức (trung bình, Standard_Deviation).
plt.Lô đất (x_values, y_values. PDF (x_values)).