Day 3. Generating random arrays

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/6

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

7 Terms

1
New cards
import numpy as np

np.random.seed(0)
arr = np.random.rand(3, 4)

print(arr)

np.random.seed ensures that the random numbers in the array remain the same for each execution. A 2-D array of random numbers, 3 rows, and 4 columns is created with np.random.rand. The numbers in the array are drawn from a uniform distribution ranging from 0-1.

2
New cards
# from book

import numpy as np

rng = np.random.default_rng(seed = 0)
arr = rng.random(size = (3, 4))

print(arr)

Numpy’s random.default_rng creates a random number generator with a seed to 0. Seed ensures that the random numbers in the array remain the same for each execution. A 2-D array with 3 rows and 4 columns is made by using random on the rng variable which contains the random number generator. The numbers in the array are drawn from a uniform distribution ranging from 0-1.

3
New cards
import numpy as np 

np.random.seed(0)
arr = np.random.randint(0, 11, size = (3, 4))

print(arr)

Numpy’s random.seed ensures that the numbers in the array remain the same for each execution. A 2-D array with 3 rows, and 4 columns is filled with random integers from 0-10 with Numpy’s random.randint.

4
New cards
import numpy as np

rng = np.random.default_rng()
arr = rng.integers(11, size = (3, 4))

print(arr)

A random number generator is assigned to a variable called rng with Numpy’s random.default_rng. A 2-D array with 3 rows, and 4 columns is filled with random integers from 0-10 by using integers on the random number generator variable.

5
New cards
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)
arr = np.random.standard_normal(1000)
arr_mean = arr.mean()
arr_var = arr.var()
standard_arr = (arr - arr_mean) / np.sqrt(arr_var)

plt.hist(standard_arr, range = (standard_arr.min(), standard_arr.max()), color = 'blue')
plt.title('Standard Normal Distribution')


print(f'mean: {standard_arr.mean().astype(int)} variance: {standard_arr.var().astype(int)}')

Numpy and matplotlib.pyplot are imported. Numpy’s random.seed ensures that the values in the array remain the same after each execution. A 1-D array with 1000 numbers from a standard normal distribution is created with Numpy’s random.standard_normal. The mean and variance of the array are calculated and assigned to variables. The array is standardized by subtracting it from its mean and dividing that by the standard deviation. The standard deviation is calculated by finding the square root of the variance with NumPy’s sqrt function. Standardizing sets the mean to 0 and variance to 1. A histogram is created from the standardized array. The range of the histogram is set to the lowest and highest values in the standardized array. Plot title assigns a title to the histogram. The print statement includes the mean and variance of the standardized array as integers.

6
New cards
import numpy as np 
import matplotlib.pyplot as plt
import math

rng = np.random.default_rng(0)
arr = rng.standard_normal(1000)
arr_mean = arr.mean()
arr_var = arr.var()
standard_arr = (arr - arr_mean) / math.sqrt(arr_var)

plt.hist(standard_arr, range = (standard_arr.min(), standard_arr.max()), color = 'blue')
plt.title('Standard Normal Distribution')

print(f'mean: {standard_arr.mean().astype(int)} variance: {standard_arr.var().astype(int)}')

Numpy, matplotlib.pyplot, and math are imported. A random number generator set to 0 is created with Numpy’s random.default_rng. A 1-D array of 1000 numbers from a standard normal distribution is created with standard_normal on the random number generator variable. The mean and variance of the array are calculated and assigned to variables. The array is standardized by subtracting it from its mean and dividing that by the standard deviation. The standard deviation is calculated by finding the square root of the variance with math’s sqrt function. Standardizing sets the mean to 0 and the variance to 1. A histogram is created from the standardized array. The range of the histogram is set to the lowest and highest values in the standardized array. Plot title assigns a title to the histogram. The print statement includes the mean and variance of the standardized array as integers.

7
New cards
import numpy as np

list = ['orange', 'apple', 'pear']
arr = np.random.choice(list, size = (3, 4))

print(arr)

a list with 3 strings is created. A 2-D array with 3 rows and 4 columns is created using np.random.choice. This function selects elements from the list and fills the array with the chosen elements.