Hướng dẫn how do you create a normal distribution curve in python? - làm cách nào để bạn tạo đường cong phân phối chuẩn trong python?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Prerequisites:

    • Đọc
    • Bàn luận
    • Matplotlib
    • Numpy

    Scipy is a probability function used in statistics that tells about how the data values are distributed. It is the most important probability distribution function used in statistics because of its advantages in real case scenarios. For example, the height of the population, shoe size, IQ level, rolling a die, and many more. 

    Số liệu thống kê

    Hướng dẫn how do you create a normal distribution curve in python? - làm cách nào để bạn tạo đường cong phân phối chuẩn trong python?

    Phân phối bình thường là một hàm xác suất được sử dụng trong các số liệu thống kê cho biết cách phân phối giá trị dữ liệu. Đây là chức năng phân phối xác suất quan trọng nhất được sử dụng trong số liệu thống kê vì lợi thế của nó trong trường hợp thực tế. Ví dụ, chiều cao của dân số, kích thước giày, mức IQ, lăn một cái chết, và nhiều hơn nữa. & NBSP;

    Hàm mật độ xác suất của phân phối bình thường hoặc Gaussian được đưa ra bởi:

    Hàm mật độ xác suất

    • Ở đâu, x là biến, MU là giá trị trung bình và độ lệch chuẩn Sigmais python’s data visualization library which is widely used for the purpose of data visualization.
    • Các mô -đun cần thiết is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python.
    • Matplotlib là thư viện trực quan hóa dữ liệu Python, được sử dụng rộng rãi cho mục đích trực quan hóa dữ liệu.is a python library that is useful in solving many mathematical equations and algorithms.
    • Numpyis một gói xử lý mảng đa năng. Nó cung cấp một đối tượng mảng đa chiều hiệu suất cao và các công cụ để làm việc với các mảng này. Đây là gói cơ bản để điện toán khoa học với Python.module provides functions for calculating mathematical statistics of numeric data.

    Scipy là một thư viện Python hữu ích trong việc giải nhiều phương trình toán học và thuật toán.

    • Mô -đun thống kê cung cấp các chức năng để tính toán số liệu thống kê toán học của dữ liệu số.

    Syntax:

    mean(data)
    • Các chức năng được sử dụng

    Syntax:

    stdev(data)
    • Để tính toán trung bình của dữ liệu

    Syntax:

    Để tính toán độ lệch chuẩn của dữ liệu

    Để tính toán mật độ xác suất bình thường của định mức dữ liệu.pdf được sử dụng, nó đề cập đến hàm mật độ xác suất bình thường là mô -đun trong thư viện SCIPY sử dụng hàm mật độ xác suất trên để tính toán giá trị.

    Norm.pdf (dữ liệu, LỘC, tỷ lệ)

    • Ở đây, tham số LỘC còn được gọi là giá trị trung bình và tham số tỷ lệ còn được gọi là độ lệch chuẩn.
    • Cách tiếp cận
    • Nhập mô -đun
    • Tạo dữ liệu
    • Tính toán trung bình và độ lệch
    • Tính mật độ xác suất bình thường

    Sơ đồ sử dụng các giá trị được tính toán ở trên

    Python3

    Hiển thị cốt truyện

    Dưới đây là việc thực hiện.

    import numpy as np

    import matplotlib.pyplot as plt

    from scipy.stats import

    stdev(data)
    0

    import

    stdev(data)
    2

    stdev(data)
    3
    stdev(data)
    4
    stdev(data)
    5
    stdev(data)
    6
    stdev(data)
    7
    stdev(data)
    8
    stdev(data)
    7
    stdev(data)
    8
        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()
    
    1
        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()
    
    2

        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()
    
    9

    import0

    Output:

        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()
    
    3
    stdev(data)
    4
        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()
    
    5

    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()
    

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

    Sử dụng phương thức Random.nmoral () để có phân phối dữ liệu bình thường.Nó có ba tham số: loc - (trung bình) trong đó đỉnh của chuông tồn tại. normal() method to get a Normal Data Distribution. It has three parameters: loc - (Mean) where the peak of the bell exists.

    Làm thế nào để bạn tạo một phân phối bình thường bằng cách sử dụng một đường cong bình thường?

    Phác thảo một hình ảnh của một phân phối bình thường.Bắt đầu bằng cách vẽ một đường ngang (trục).Tiếp theo, vẽ một đường cong bình thường (hình chuông) tập trung vào trục ngang.Sau đó vẽ một đường thẳng đứng từ trục ngang qua tâm của đường cong, cắt nó làm đôi.

    Làm thế nào để bạn vẽ một phân phối bình thường trong một cuốn sổ Jupyter?

    PDF trả về giá trị PDF, chúng ta có thể sử dụng hàm này để vẽ hàm phân phối bình thường.Chúng tôi vẽ đồ thị PDF của phân phối bình thường bằng cách sử dụng SCIPY, Numpy và Matplotlib.Chúng tôi sử dụng miền −4