Use numpy.randn(), numpu.random.normal and numpu.random.standard_normal() functions to create a one-dimensional Gaussian distribution

Looking at the numpydocumentation, we can see that numpy.randn(), numpu.random.normaland numpu.random.standard_normal()functions can be used to get the return value of a specified number of Gaussian distributions. I had a whim the other day, is there a way to use them to generate a one-dimensional Gaussian distribution? The answer is yes.

class GaussianDistribution:
    @staticmethod
    def selfMadeMethod():
        num = 1001
        array = np.random.randn(num)
        max_ = np.max(abs(array))
        x = np.linspace(-max_, max_, num)
        result = np.ones(num - 1)
        for i in range(num):
            for j in range(num - 1):
                if x[j] < array[i] < x[j + 1]:
                    result[j] += 1
        real_x = np.linspace(-max_ + max_ * 2 / (num - 1), max_ - max_ * 2 / (num - 1), num - 1)
        plt.figure(1)
        plt.title('1D Gaussian Distribution')
        plt.plot(real_x, result)

    @staticmethod
    def numpyRandomRandn():
        num = 1001
        array = np.random.randn(num)
        plt.figure(2)
        plt.title('1D Gaussian Distribution')
        plt.hist(array, 50, density=True)

    @staticmethod
    def numpyRandomNormal():
        mu, sigma = 0, 0.1  # mean and standard deviation
        s = np.random.normal(mu, sigma, 1000)
        plt.figure(3)
        plt.title('1D Gaussian Distribution')
        plt.hist(s, 50, density=True)

    @staticmethod
    def numpyRandomStandardNormal():
        s = np.random.standard_normal(1000)
        plt.figure(4)
        plt.title('1D Gaussian Distribution')
        plt.hist(s, 50, density=True)
        plt.show()


if __name__ == '__main__':
    gaussian = GaussianDistribution()
    gaussian.selfMadeMethod()
    gaussian.numpyRandomRandn()
    gaussian.numpyRandomNormal()
    gaussian.numpyRandomStandardNormal()

selfMadeMethod()The methods in the function are equivalent to the other three methods to some extent, and the results of the four methods are shown in the figure below.

selfMadeMethod()Method result:
gaussian distribution
we can see that the image here is probably a Gaussian distribution type, of course, there is a certain difference from the ideal smooth curve. After all, we randomly generate Gaussian distribution values. It is speculated that when there are enough points, the curve Will gradually become smoother. However, it is enough to explain the problem. How to optimize is not discussed here.
numpyRandomRandn()Method result:
gaussian distribution
numpyRandomNormal()Method result:
gaussian distribution

numpyRandomStandardNormal()Method result:
gaussian distribution
We can see that the results are all approximate to Gaussian distribution, and histthe total integral of the area in the three images is 1. However, there are still some differences. But it is possible to use three random functions to generate Gaussian distributed results.

The code word is not easy, if you find it useful, please raise your hand to give a like and let me recommend it for more people to see~

Guess you like

Origin blog.csdn.net/u011699626/article/details/112062173