4 Ways to Use Numpy Random Normal Function in Python

In the Numpy module, we have discussed many functions used to operate on the multidimensional array. In this tutorial, we will discuss the concept of the numpy Random normal() function, which is used to get the random samples from a normal distribution. This is the built-in function in the numpy package of python. The NumPy random normal() function generate random samples from a normal distribution/Gaussian distribution.

What is Numpy Random normal() function?

The Numpy random normal() function generates an array of specified shapes and fills it with random values, which is actually a part of Normal(Gaussian)Distribution. The other name of this distribution is a bell curve because of its shape.

Syntax of Numpy Random normal()

numPy.random.normal(loc = 0.0, scale = 1.0, size = None)

Parameters of Numpy Random normal()

  • loc: It is an optional parameter. Input is like an array-like or a float value. It specifies the mean of the distribution. By default, it is 0.0.
  • scale: It is an optional parameter. Input is like an array-like or a float value. It specifies the standard deviation or the flatness of the distribution graph should like. By default, it is 1.0. It must not be a negative value.
  • size: It is an optional parameter. Input is like an integer or a tuple of integer. It specifies the shape of the resultant array. If the size is None. By default, it is 1.

Return value of Numpy Random normal()

This function’s return value is the array of defined shapes filled with random values of normal distribution/gaussian distribution.

Examples of numpy random normal() function

Here, we will be discussing how we can write the random normal() function from the numpy package of python.

1. Taking size as a parameter

In this example, we will be importing the numpy library. Then, we will apply the random.normal() function with size = 5 and tuple of 2 and 6 as the parameter. So the output will come as the array of size = 5, and for tuple 2, rows and columns will create a multidimensional array as the output. Let us look at the example for understanding the concept in detail.

# importing numpy library as np
import numpy as np

output= np.random.normal(size = 5)

print(output)
out= np.random.normal(size = (2,6))

print(out)

Output:

[-0.95488981  2.2038197  -0.50444554  0.51845053 -1.15899431]

[[-0.62126192 -0.05753977 -0.26676354 -2.04002572  1.26073641 -0.03351041]
 [ 1.54348742  2.46762337 -0.53278498  0.35762827 -0.05746769 -0.63672764]]

Explanation:

  • Firstly, we will be importing the numpy library with an alias name as np.
  • Then, we will apply the function with size = 5 as the parameter.
  • Size means the number of values in the array.
  • After that, we will print the output.
  • Then, we will take size as the tuple values and print the output.
  • Hence, you can see the output of both methods.

2. Taking loc as a parameter

In this example, we will import the numpy library. Then, we will apply the random.normal() function with loc = 5 as the parameter and see the output as the mean of the array’s random values. Let us look at the example for understanding the concept in detail.

# importing numpy library as np
import numpy as np

output= np.random.normal(loc = 5)

print(output)

Output:

5.55426892339958

Explanation:

  • Firstly, we will be importing the numpy library with an alias name as np.
  • Then, we will be applying the function with loc = 5 as the parameter.
  • Loc means the mean value of the random values chose from the function.
  • At last, we will print the output.
  • Hence, you can see the mean as the output.

3. Taking scale as a parameter

In this example, we will import the numpy module. Then, we will apply random.normal() function with scale = 5.0 as the parameter and see the output as the input’s standard deviation. Let us look at the example for understanding the concept in detail.

# importing numpy library as np
import numpy as np

output= np.random.normal(scale = 5.0)

print(output)

Output:

-6.245600820720526

Explanation:

  • Firstly, we will be importing the numpy library with an alias name as np.
  • Then, we will be applying the function with scale = 5.0 as the parameter.
  • scale means the standard deviation of the values chosen from the function.
  • At last, we will print the output.
  • Hence, you can see the standard deviation as the output.

4. Taking all three as a parameter

In this example, we will import the numpy library. Then, we will apply random.normal() function with scale = 5.0, loc = 2, and size = 6 as the parameter. after that we will see the output with respect to the parameters. Let us look at the example for understanding the concept in detail.

# importing numpy library as np
import numpy as np

output= np.random.normal(scale = 5.0, loc = 2, size =6)

print(output)

Output:

[-4.4756869   5.84053972  9.57577152 -3.39868126  2.60193779 -2.81686457]

Explanation:

  • Firstly, we will be importing the numpy library with an alias name as np.
  • Then, we will be applying the function with scale = 5.0, loc = 2, and size = 6 as the parameter.
  • scale means the standard deviation of the values chosen from the function.
  • Loc means the mean value of the random values chose from the function.
  • Size means the number of values in the array.
  • At last, we will print the output.
  • Hence, you can see the output with all the parameters.

Graphical representation of numpy random normal() function

In this example, we will import the numpy library and pyplot library. Then, we will apply the random.normal() function with all the parameters and print the output. After that, we will make a histogram with the help pyplot library and print the graph. Let us look at the example for understanding the concept in detail.

# import numpy library
import numpy as np
# import pyplot library
from matplotlib import pyplot as plt

output = np.random.normal( 0.0 , 1.0, size = 50 )

print(output)
count, bins, ignored = plt.hist( output, 100)
plt.show()

Output:

[-2.0599869   1.86530215 -0.83282691 -0.9609226  -0.17388443  0.77542348
 -0.69147226  0.26678705 -0.43186711  2.45332791  0.41294372  1.06856734
  0.26322133 -1.57945561  1.15077477  1.74056801 -0.48934632  0.04633452
 -0.48010091 -1.59184196  0.08802584  0.16564185  0.53415194 -0.5628642
 -0.90310561 -0.4983175  -0.12221693 -1.08879417  1.31233936 -0.4831863
 -0.16477098  0.36494931 -0.18429183 -0.47282319  0.65385078 -0.51189286
 -0.61625412 -1.1119411   0.06379226  0.17779413  1.49812809 -0.21141802
  0.79097138  1.41886803  0.86628245 -0.17059296  0.59086623 -0.70027204
  1.80495114 -0.70090817]
Graphical representation of numpy random normal() function

Explanation:

  • Firstly, we will be importing the numpy library with an alias name as np.
  • Then, we will import pyplot as plt from the matplotlib library.
  • Then, we will apply the function with all the parameters and print the output.
  • Array values are so many as the size is 50. so there will be 50 values as the random samples.
  • After that, we will print the histogram with the help of the pyplot module.
  • At last, we can see the graph drawn.
  • Hence, you can see both the output.

Conclusion

In this tutorial, we have learned about the concept of the numpy random normal() function. We have discussed the definition, syntax, parameter, and return value of the function. We have also explained all the ways by which we can use the numpy random normal() with the help of examples explained in detail. You can use any of the functions according to your choice and your requirement in the program.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments