Quick Contact

    Python Tutorial
    Python Panda Tutorial
    Python Selenium
    Python Flask Tutorial
    Python Django
    Numpy
    Tensorflow
    Interview Questions & Answers

    NumPy Normal Distribution is one of the various functions supported by the python numpy library that allows us to create a normal distribution or Gaussian distribution, which is can be used to fit the probability distribution of various elements and events that occur naturally or created by us. Furthermore, with the help of the feature random supported by the numpy library, we can create or generate a random normal distribution, and using various visualization packages in python, we can also plot and visualize the distribution.

    Syntax:

    The basic syntax of the NumPyNewaxis function is:

    numpy.random.normal(loc=, scale= size=)

    • numpy.random.normal:

      It is the function that is used to generate the normal distribution of our desired shape and size.

    • loc:

      Indicates the mean or average of the distribution; it can be a float or an integer.

    • scale:

      A non-negative integer or float that indicates the standard deviation, which is the width of the overall distribution.

    • size:

      It can be a tuple with a float or integer, and it represents the distribution’s output size or shape, and when loc and scales are n scalar values, the size will a single value is returned when size is given as none as input.

    Examples of NumPy Normal Distribution

    Given below are the examples of NumPy Normal Distribution:

    Example #1

    Let us see a basic example for understanding how the numpy normal distribution function is used to generate a normal distribution.

    Code:

    importnumpy as np
    mean = 2
    sigma = 0.4
    out = np.random.normal(mean, sigma, 500)
    

    Example #2

    In this example, we will see how to change the one-dimensional array to a two-dimensional array using the new axis object.

    Code:

    importnumpy as np
    out1 = np.random.normal(2, 4.5, size=(4, 8))
    out1
    

    Example #3

    In this example, we have created two normal distribution arrays, ‘a’ and ‘b’, using different techniques.

    Code:

    import numpy as np
    a = np.random.normal(size=(3, 4))
    b = np.random.normal(loc=2, scale=3, size=(2, 3))
    print(a)
    print(b)
    

    Example #4

    In this example, we will see how we can visualize the normal distribution using both the matplotlib library and seaborn library.

    Code:

    importnumpy as np
    import seaborn as sns
    import matplotlib.pyplot as plt
    mean = 3
    sigm = 5
    out = np.random.normal(mean, sigma, 1000)
    sns.distplot(out,hist=False)
    plt.hist(out, 25, density=True)
    

    Example #5

    In this example, we have created normal distribution and a random distribution and compared both the distribution using histogram from the matplotlib library.

    Code:

    import numpy as np
    N, mean, sigm = 1000, 50, 7
    a = mean + sigm*np.random.randn(N)
    b = mean + sigm*(np.random.rand(N)-0.7)
    fig, axes = plt.subplots(ncols=2)
    axes[0].set_title('Normal Distribution')
    x, bin_1, patch1 = axes[0].hist(a, 20, facecolor='R', alpha=0.7)
    axes[1].set_title('Random Distribution')
    x2, bins_2, patch2 = axes[1].hist(b, 20, facecolor='K', alpha=0.7)
    plt.show()
    


     

    Apply now for Advanced Python Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.