NumPy: Create a two-dimensional array with shape (8,5) of random numbers, Select random numbers from a normal distribution (200,7)
NumPy: Basic Exercise-48 with Solution
Random Normal Distribution 2D Array
Write a NumPy program to create a two-dimensional array with shape (8,5) of random numbers. Select random numbers from a normal distribution (200,7).
This problem involves writing a NumPy program to generate a two-dimensional array with a shape of (8,5) filled with random numbers selected from a normal distribution with a mean of 200 and a standard deviation of 7. By generating random values from a normal distribution, the program constructs a matrix suitable for various statistical analyses and modeling tasks.
Sample Solution :
Python Code :
# Importing the NumPy library with an alias 'np'
import numpy as np
# Setting the seed for NumPy's random number generator to 20
np.random.seed(20)
# Calculating the cube root of 7
cbrt = np.cbrt(7)
# Defining a variable 'nd1' with a value of 200
nd1 = 200
# Generating a 10x4 array of random numbers from a normal distribution with mean nd1 and standard deviation cbrt
print(cbrt * np.random.randn(10, 4) + nd1)
Output:
[[201.6908267 200.37467631 200.68394275 195.51750123] [197.92478992 201.07066048 201.79714021 198.1282331 ] [200.96238963 200.77744291 200.61875865 199.05613894] [198.48492638 198.38860811 197.55239946 200.47003621] [199.91545839 202.99877319 202.01069857 200.77735483] [199.67739161 193.89831807 202.14273593 202.54951299] [199.53450969 199.7512602 199.79145727 202.97687757] [200.24634413 196.04606934 198.30611253 197.88701546] [201.78450912 203.94032834 198.21152803 196.91446071] [201.0082481 197.03285104 200.63052763 197.82590294]]
Explanation:
In the above code -
np.random.seed(20) sets the random seed value to 20. Setting the seed ensures that the same set of random numbers is generated every time the code is executed, which can be useful for reproducibility.
cbrt = np.cbrt(7): This line calculates the cube root of 7 using NumPy's np.cbrt() function and assigns the result to the variable cbrt.
nd1 = 200 assigns the value 200 to the variable nd1.
print(cbrt * np.random.randn(10, 4) + nd1): This statement does the following:
- np.random.randn(10, 4) generates a 10x4 array with random numbers from a standard normal distribution (mean=0, standard deviation=1).
- cbrt * np.random.randn(10, 4) scales the random numbers in the array by the cube root of 7, which was calculated earlier.
- cbrt * np.random.randn(10, 4) + nd1 shifts the scaled random numbers by adding 200 to each element in the array.
Python-Numpy Code Editor:
Previous: NumPy program to create a one dimensional array of forty pseudo-randomly generated values. Select random numbers from a uniform distribution between 0 and 1.
Next: NumPy program to generate a uniform, non-uniform random sample from a given 1-D array with and without replacement.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/numpy/basic/numpy-basic-exercise-48.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics