DATA SCIENCE: Numpy
what does this do: element = my_2d_array[1, 2]
# Access a single element at row 1, column 2 (indexing starts from 0)
What can we use in the reshape() function to automatically allow NumPy to infer the size of the corresponding dimension based on the total number of elements in the array
- 1 example : np.reshape(array, (4,-1))
wah dis do? import numpy as np # Create two NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Add the two arrays together using the + operator result = array1 + array2
adds the 2 arrays
create a 3D array
arr_3d = np.random.rand(2,3,4)
what does this do? zeros_Arr = np.zeros((3,4))
creates a 3x4 array of zeros
What's a universal function?
functions that operate element-wise on arrays, providing high-performance mathematical operation
What's broadcasting
Broadcasting in NumPy is a mechanism that allows arrays of different shapes to be combined in arithmetic operations. When performing operations on arrays with different shapes, NumPy automatically "broadcasts" the arrays to make their shapes compatible.
What are key features in NumPy?
Efficient array operations Mathematical Functions Random Number Generation
What is NumPy used for?
High-level mathematical functions and operations on arrays
What is NumPy an abbreviation of?
Numerical Python
what's the difference between np.random.rand(shape) and np.random.randn(shape)?
Use np.random.rand(shape) to generate random numbers from a uniform distribution over the interval [0, 1). Use np.random.randn(shape) to generate random numbers from a standard normal distribution (mean=0, standard deviation=1).
wah dis do: subset = my_array[2:4] # Extract elements from index 2 to index 3 (inclusive) print(subset) # Output: [3 4] subset = my_array[:3] # Extract elements from the beginning up to index 2 (inclusive) print(subset) # Output: [1 2 3] subset = my_array[3:] # Extract elements from index 3 to the end of the array print(subset) # Output: [4 5] subset = my_array[::2] # Extract every second element print(subset) # Output: [1 3 5] subset = my_array[::-1] # Reverse the array print(subset) # Output: [5 4 3 2 1]
different ways to slice the array
what do np.arange() and np.linespace() do?
generate arrays with evenly spaced values
create a 2D shape array
my_2d_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
How do we make arrays from python lists?
np.array("sumthing")
how do we multiply 2 matrices?
np.dot(matrix1, matrix2)
Describe the uses of the following: np.random.randint(low, high, size) np.random.choice(Array, size, replace) np.random.random.seed(seed)
np.random.randint(low, high, size): Generates random integers in the specified range [low, high) with the given size np.random.choice(array, size, replace): Randomly samples elements from the given array np.random.seed(seed): Sets the seed for reproducibility, ensuring the same sequence of random numbers is generated
how do we flatten an array into a 1D array
np.ravel(arr2)
how can we reshape an array?
np.reshape(array,(3,4))
how can we create arrays of zeros and ones?
np.zeros() np.ones()1
NumPy provides functions for aggregating data, such as
sum, mean, minimum, maximum