From Novice to Ninja: NumPy Arrays in Python — Day 02 (Introduction to Random Numbers)

Data Science Delight
5 min readJul 6, 2023

--

In the previous session, we saw what are NumPy arrays, the difference between lists and NumPy arrays, and the basics of NumPy functions. You can visit the site from the link below:

In Python, the NumPy library provides a powerful toolset for generating random numbers effectively. With the help of NumPy, we can easily create arrays of random numbers with different distributions, enabling us to perform different statistical computations.

In this blog post, we will be discussing how to generate random numbers using various NumPy functions along with examples, that we will be using in most of the data science applications, including statistical analysis and machine learning.

Photo by NEOM on Unsplash

Let’s dive into the code and get a clear understanding.

Generating single random numbers:

To generate random numbers, first, we need to import the NumPy library & its random module.

import numpy as np

→ Generating random integer:

Random module’s randint() method is used to return a random integer between low(inclusive) and high(exclusive) i.e.; within a specified range.

To generate a random integer between 0 & 100, we can use the following code:

x = np.random.randint(100)
x

→ Generating random float:

Random module’s rand() method is used to return a float value from a discrete uniform distributionbetween 0 & 1.

Generate a random float between 0 & 1:

y = np.random.rand()
y

→ Generating random numbers from a standard normal distribution:

For this, we use randn() method to generate a random number from a standard normal distribution(mean — 0, standard_deviation — 1)

z = np.random.randn()
z

Creating random arrays:

Beyond generating single random numbers, NumPy allows us to create an entire array filled with random numbers. Let’s examine such functions:

→ Generating an array of random numbers within a given range:

In such a case, we will be using np.random.randint() function.

It takes in argument: (low, high, no. of values)

For 1-D Array:

arr_1d = np.random.randint(0, 4, 10)
arr_1d

It will return a 1-D array containing 10 random integers between 0 & 4 where 4 is exclusive so you might see that 0, 1, 2, or 3 are being repeated more than once because here we want 10 random integers.

For 2-D Array:

arr_2d = np.random.randint(2, 15, size = (2, 3))
arr_2d

It will generate a 2d array containing random integers between 2 and 15.

→ Generating an array of random numbers from a uniform distribution over (0 & 1):

In such a case, we will be using np.random.rand() function.

It takes in argument: (shape of an array)

For 1-D Array (containing 5 random numbers):

arr_1d = np.random.rand(5)
arr_1d

It will generate an array of random numbers from a uniform distribution.

For a 2-D Array containing 20 random numbers:

arr_2d = np.random.rand(4, 5)
arr_2d

It will generate a 2d array of random numbers from a uniform distribution between 0 and 1.

NOTE:

Here, we needed 20 random numbers so we took (4, 5) or you can also take (5, 4) up to you.

Now, the question is;

How to know the shape of an array based on the numbers?

Suppose I want to create a 2d array containing 25 random values, the shape will be: (5, 5) coz (5 * 5 = 25)

Suppose I want to create a 2d array containing 15 random values, the shape will be: (3, 5) or (5, 3) coz (3 * 5) = 15

→ Generating an array of random numbers from a standard normal distribution:

In such a case, we will be using np.random.randn() function. It will return random numbers centered around 0.

It takes in argument: (shape of an array)

For 1-D Array containing 2 random numbers:

arr_1d = np.random.randn(2)
arr_1d

For a 2-D Array containing 12 random numbers:

arr_2d = np.random.randn(4, 3)
arr_2d

NOTE:

It returns random numbers, with a combination of both positive & negative numbers.

np.random.normal():

It is used to generate random numbers from a normal or Gaussian distribution, characterized by its mean and standard deviation.

It takes in argument: (mean, std_dev, size of an array)

Let’s create:

1-D Array of size 12 with numbers drawn from a normal distribution with a mean of 4 and a standard deviation of 2

random_num = np.random.normal(4, 2, 12)
random_num

A 2-D Array of size (4, 3) with numbers drawn from a normal distribution with a mean of 6 and a standard deviation of 3

random_num = np.random.normal(6, 3, size = (4, 3))
random_num

Now, the question arises:

What is the difference between np. random.randn() and np.random.normal():

The main difference actually lies in how they generate random numbers and what parameters you specify.

np.random.normal(mean, std_dev, size):

  • This function generates random numbers from a normal or Gaussian distribution with a specified mean and standard deviation.
  • You need to provide ‘mean’, ‘standard_deviation’, and ‘size’ as arguments.
  • The generated numbers will be drawn from a normal distribution with a given mean and standard deviation.

np.random.randn(size):

  • This function generates random numbers from a standard normal deviation which has a mean of 0 and a standard deviation of 1.
  • You only need to provide ‘size’ as an argument.
  • The generated numbers will be drawn from a standard normal deviation.

Exercise Questions:

Now, your task is to try out the below questions on your own based on the NumPy functions that we have discussed in this session, and if you have any doubts feel free to ask me.

For each question, write the code using the appropriate NumPy function to generate the desired array:

  1. Create a 2d array containing 36 random numbers between 0 & 1.
  2. Generate 10 random integers between 1 to 20.
  3. How can you create a 2D array of 9 random floating-point numbers between 0 and 1 using NumPy?
  4. Can you generate a random array of size 10 with numbers drawn from a normal distribution with a mean of 5 and a standard deviation of 2?

That’s it for Day — 02:

That’s it for today’s session. In the next session, we will be discussing about methods & attributes used in creating a NumPy array in detail along with the code.

Please Subscribe/Follow, Like, Share, and Clap as it would encourage me to write more such blog posts, especially on data analytics and data science field.

Stay Tuned!!

Thank you!

--

--

Data Science Delight

Content Creator | Sharing insights & tips on data science | Instagram: @datasciencedelight | YouTube: https://www.youtube.com/channel/UCpz2054mp5xfcBKUIctnhlw