NumPy Tutorial
Introduc on to NumPy:
NumPy is a powerful Python library for numerical computa ons. It provides support for large, mul -
dimensional arrays and matrices, along with a collec on of mathema cal func ons to operate on
these arrays efficiently.
To use NumPy, you need to import the module:
import numpy as np
Crea ng NumPy Arrays:
1. Create a 1D Array:
Use the np.array() func on to create a 1D array from a Python list or tuple
arr = np.array([1, 2, 3, 4, 5])
2. Create a 2D Array:
Create a 2D array by passing a nested list to the np.array() func on.
arr = np.array([[1, 2, 3], [4, 5, 6]])
3. Create an Array with Zeros:
Use np.zeros() to create an array filled with zeros.
arr = np.zeros((3, 4)) #creates a 3x4 array of zeros
4. Create an Array with Ones:
Use np.ones() to create an array filled with ones.
arr = np.ones((2, 3)) #creates a 2x3 array of ones
5. Create an Array with a Range:
Use np.arange() to create an array with a specified range of values.
arr = np.arange(1, 10, 2) #creates an array [1, 3, 5, 7, 9]
Array Opera ons:
6. Array Shape:
Use the .shape a ribute to get the shape of an array.
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape) #prints (2, 3)
7. Array Reshaping:
Use .reshape() to reshape an array into a different shape.
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped_arr = arr.reshape((2, 3)) #reshapes the array to a 2x3 shape
8. Array Slicing:
Use slicing to extract a por on of an array.
arr = np.array([1, 2, 3, 4, 5])
sliced_arr = arr[2:4] #extracts elements at index 2 and 3
9. Array Concatena on:
Use np.concatenate() to concatenate mul ple arrays.
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
concatenated_arr = np.concatenate((arr1, arr2)) #[1, 2, 3, 4, 5, 6]
Mathema cal Func ons:
10. Array Sum:
Use np.sum() to calculate the sum of array elements.
arr = np.array([1, 2, 3, 4, 5])
total = np.sum(arr) #15
11. Array Mean:
Use np.mean() to calculate the mean of array elements.
arr = np.array([1, 2, 3, 4, 5])
avg = np.mean(arr) #3.0
12. Array Maximum and Minimum:
Use np.max() and np.min() to find the maximum and minimum values in an array.
arr = np.array([1, 2, 3, 4, 5])
max_val = np.max(arr) #5
min_val = np.min(arr) 1
Random Number Genera on:
13. Generate Random Numbers:
Use np.random.rand() or np.random.randint() to generate random numbers
random_num = np.random.rand() #generates a random number between 0 and 1
rand_int = np.random.randint(1, 10) #generates a random integer between 1 and 10
Exercise
Create a 2D array with shape (3, 4) and print its dimensions.
Create a 1D array with values from 1 to 10 and print its mean.
Create a 3x3 array filled with random numbers between 0 and 1.
Create two 1D arrays, [1, 2, 3] and [4, 5, 6], and concatenate them.
Calculate the sum and product of a given 1D array [1, 2, 3, 4, 5].
Create a 2D array with shape (3, 4) and calculate the sum of its columns.
Create a 2D array with shape (3, 4) and calculate the mean of its rows.
Create a 2D array and reshape it into a 1D array.
Create a 2D array and extract a por on of it using slicing.
Generate a random integer between 1 and 100.
Create a 2D array with shape (3, 4) and find the maximum value in each row.
Create a 1D array with values from 1 to 20 and reshape it into a 4x5 matrix.
Create a 1D array with values from 1 to 10 and reverse its order.
Create a 2D array with shape (3, 3) and calculate the transpose of it.
Create a 2D array with shape (3, 3) and calculate the determinant of it.