Q:

Python | Generate random number using numpy library

belongs to collection: Python basic programs

0

Goal: To speculate and generate random numbers using numpy library

Random Number Generation: Random number generation in very important in the field of machine learning. It is used to initialize weights in almost every machine learning algorithm.

So few functions used for machine learning algorithms from numpy library:

  1. numpy.random.rand()
    It take shape of the array as its argument and then generate random numbers and fill whole the array with the random numbers that lies in between 0 and 1. The distribution of random numbers follows uniform distribution.
  2. numpy.random.randint()
    It takes two arguments(low and high). It generates random integer between low and high in which low is inclusive and high is exclusive. It follows discrete uniform distribution.
  3. numpy.random.randn()
    It takes shape of the array as its argument and generate random numbers in the form of gaussian distribution with mean as 0 and variance as 1. It follows standard normal distribution.
  4. numpy.random.random()
    It takes size as its argument and generate random number random number lying between 0 and 1. It follows continuous random distribution.
  5. numpy.random.multivariate()
    It primarily takes three arguments(mean of individual feature in form of matrix,co -variance matrix and last argument is number of datapoints). For generating data for more than one feature, mean and variance matrix must be of higher dimension. It follows multivariate normal distribution.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python implementation:

import numpy as np

print("###########random.rand()############")
A = np.random.rand(2,5)
print(A)
print("###########random.randint()############")
B = np.random.randint(2,17)
print(B)
print("###########random.randn()############")
C = np.random.randn(2,5)
print(C)
print("###########random.random()############")
D = np.random.random((2,5))
print(D)
print("###########random.multivariate_normal()############")
E = np.random.multivariate_normal([1.0,5.0], [[1.0,2.0],[2.0,1.0]],5)
print(E)

Output

###########random.rand()############
[[0.87736653 0.75351615 0.06455974 0.36414861 0.04139118]
 [0.41138255 0.10342316 0.05800631 0.12752116 0.33958441]]
###########random.randint()############
12
###########random.randn()############
[[ 0.01895673  0.50055148  0.12352832 -0.35232071  0.03695278]
 [ 2.02632408  0.94237563  0.60807025 -0.37935715  1.45447358]]
###########random.random()############
[[0.57192619 0.85141271 0.49857667 0.62128599 0.39234191]
 [0.72266235 0.05779006 0.99732815 0.27651905 0.14774923]]
###########random.multivariate_normal()############
/home/main.py:16: RuntimeWarning: covariance is not positive-semidefinite.
  E = np.random.multivariate_normal([1.0,5.0], [[1.0,2.0],[2.0,1.0]],5)
[[ 2.27370346  4.71914942]
 [-0.222617    4.50092221]
 [-0.38584754  4.88753041]
 [ 2.2530275   5.5017934 ]
 [-0.13875541  3.25742664]]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Program to calculate n-th term of a Fibon... >>
<< Python | Count total number of bits in a number...