Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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

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

Sample Solution :

Python Code :

import numpy as np 
np.random.seed(20) 
cbrt = np.cbrt(7)
nd1 = 200 
print(cbrt * np.random.randn(10, 4) + nd1) 

Sample 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]]

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a 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: Write a 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.



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m