Date: January 16, 2024

Topic: Using Numpy

Recall

pandas dataframes can be treated as ndarrays

Notes

Relationship to pandas

Untitled

Finding properties of an ndarray


Slicing is done with colon, starting with the outer array to the inner

Slicing with numpy

To get a specific set of rows and columns:

subset_array = ndarray[start_row:end_row+1, start_col:end_col+1] # Add 1 as upper is exclusive

# Example
ndarray[0:3, 1:3] # Get the first 3 rows, and 2nd and 3rd column

Create arrays with:

Creating ndarrays

With pre-defined values

Empty array (np.empty)

Zeros array (np.zeros)

Specifying datatype

np.ones((5,4), dtype=int) # Use dtype to specify

Create random arrays with:

Creating random ndarrays

Seed value

To ensure that arrays are generated consistently, define a seed with np.random.seed(<ANY NUMBER>)

Random numbers with sample from [0.0, 1.0)

# Generate an array full of random numbers, uniformly samples from [0.0, 1.0)
np.random.random((5, 4)) # pass in a size tuple

# Generate an array full of random numbers, uniformly samples from [0.0, 1.0)
np.random.rand(5, 4) # function arguments (not a tuple)

Sample from Gaussian distribution

# Sample numbers from a Gaussian (normal) distribution
np.random.normal(size=(2, 3)) # "standard normal" (mean = 0, s.d. = 1)

# Sample numbers from a Gaussian (normal) distribution
np.random.normal(50, 10, size=(2, 3)) # change mean to 50 and s.d. to 10

Random integers

# Random integers
np.random.randint(1) # a single integer in [0, 10)
np.random.randint(0, 10) # same as above, specifying [low, high) explicit
np.random.randint(0, 10, size=5) # 5 random integers as a 1D array
np.random.randint(0, 10, size=(2, 3)) # 2x3 array of random integers





<aside> 📌 SUMMARY: Numpy arrays are similar to dataframes, and can perform a series of operations stated above

</aside>