Creating Random Arrays with Numpy#
Introduction#
This notebook was created by Jupyter AI with the following prompt:
/generate Create a Jupyter notebook that shows how to create a random array using numpy.
This Jupyter notebook demonstrates how to create a random array using the numpy library. The necessary libraries are imported and a random array is generated using the specified dimensions. The random seed is set to ensure reproducibility, and the generated random array is displayed.
Generating a random array#
[1]:
import numpy as np
[2]:
# Set the seed for reproducibility
np.random.seed(0)
[3]:
# Generate a random array of shape (3, 4) with values between 0 and 1
random_array = np.random.rand(3, 4)
[4]:
# Display the random array
random_array
[4]:
array([[0.5488135 , 0.71518937, 0.60276338, 0.54488318],
[0.4236548 , 0.64589411, 0.43758721, 0.891773 ],
[0.96366276, 0.38344152, 0.79172504, 0.52889492]])
Specifying the array dimensions#
[5]:
import numpy as np
[6]:
# Specify the dimensions of the random array
rows = 5
columns = 3
[7]:
# Create a random array with the specified dimensions
random_array = np.random.rand(rows, columns)
[8]:
# Return the random array
random_array
[8]:
array([[0.56804456, 0.92559664, 0.07103606],
[0.0871293 , 0.0202184 , 0.83261985],
[0.77815675, 0.87001215, 0.97861834],
[0.79915856, 0.46147936, 0.78052918],
[0.11827443, 0.63992102, 0.14335329]])
Setting the random seed#
[9]:
import numpy as np
np.random.seed(42)
Creating the random array#
[10]:
import numpy as np
[11]:
# Set the dimensions of the random array
rows = 5
cols = 3
[12]:
# Create the random array
random_array = np.random.rand(rows, cols)
[13]:
# Return the random array
random_array
[13]:
array([[0.37454012, 0.95071431, 0.73199394],
[0.59865848, 0.15601864, 0.15599452],
[0.05808361, 0.86617615, 0.60111501],
[0.70807258, 0.02058449, 0.96990985],
[0.83244264, 0.21233911, 0.18182497]])
Displaying the random array#
[14]:
import numpy as np
[15]:
random_array = np.random.random((3, 4))
[16]:
random_array
[16]:
array([[0.18340451, 0.30424224, 0.52475643, 0.43194502],
[0.29122914, 0.61185289, 0.13949386, 0.29214465],
[0.36636184, 0.45606998, 0.78517596, 0.19967378]])