Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
NumPy is an essential library in Python for numerical computing. Its main object is the homogeneous multidimensional array, which is useful for performing vectorized operations without the need for explicit loops.
Here's a tutorial on various operations you can perform on NumPy arrays:
Initialization
Before you can work with NumPy, you need to install and import it:
import numpy as np
Basic Operations
Arithmetic Operations: These are element-wise.
a = np.array([10, 20, 30, 40]) b = np.array([1, 2, 3, 4]) print(a + b) # [11 22 33 44] print(a - b) # [9 18 27 36] print(a * b) # [10 40 90 160] print(a / b) # [10. 10. 10. 10.]
Universal Functions (ufunc): NumPy provides familiar mathematical functions such as sin, cos, exp, etc. These operate element-wise.
c = np.array([0, np.pi/2, np.pi]) print(np.sin(c)) # [0. 1. 0.]
Matrix Operations
Element-wise Multiplication:
A = np.array([[1, 2], [3, 4]]) B = np.array([[2, 0], [0, 2]]) print(A * B) # Element-wise product
Matrix Product:
print(A @ B) # or use np.dot(A, B)
Matrix Transposition:
print(A.T)
Aggregate Functions: Operations for computing aggregates.
d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(d.sum()) # 45 print(d.min()) # 1 print(d.max()) # 9 print(d.sum(axis=0)) # sum of each column: [12 15 18] print(d.cumsum(axis=1)) # cumulative sum along each row: [[ 1 3 6] # [ 4 9 15] # [ 7 15 24]]
Reshaping, Splitting, and Joining
Reshape:
e = np.arange(12).reshape(3, 4) print(e)
Split:
f1, f2, f3 = np.split(e, 3) # Split the array into 3 equal-sized subarrays print(f1)
Concatenate/Join:
g = np.concatenate((f1, f2, f3)) print(g)
Slicing and Indexing
h = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) print(h[0, 1]) # 2 print(h[:, 1]) # [ 2 6 10] print(h[1:3, :]) # [[ 5 6 7 8] # [ 9 10 11 12]]
Conditional Operations
i = np.array([[1, 2], [3, 4], [5, 6]]) bool_idx = (i > 2) print(bool_idx) # Returns an array of the same shape with True/False values print(i[bool_idx]) # Returns a 1D array with the True values: [3 4 5 6]
Broadcasting: Broadcasting allows NumPy to work with arrays of different shapes.
j = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) k = np.array([1, 0, 1]) print(j + k) # Adds k to each row of j
This is just a basic overview of the capabilities of NumPy. The library has a rich set of features and functions which you can explore further in the official documentation or various online resources.
Performing operations on arrays involves applying various mathematical and statistical operations using NumPy.
import numpy as np # Create two arrays for demonstration array1 = np.array([1, 2, 3, 4]) array2 = np.array([5, 6, 7, 8]) # Perform addition of arrays result_addition = array1 + array2 # Perform multiplication of arrays result_multiplication = array1 * array2 print("Array 1:", array1) print("Array 2:", array2) print("Addition Result:", result_addition) print("Multiplication Result:", result_multiplication)
Common array operations include addition, subtraction, multiplication, and division using NumPy in Python.
# Assuming 'array1' and 'array2' are already defined # Perform common array operations result_addition = array1 + array2 result_subtraction = array1 - array2 result_multiplication = array1 * array2 result_division = array1 / array2 print("Array 1:", array1) print("Array 2:", array2) print("Addition Result:", result_addition) print("Subtraction Result:", result_subtraction) print("Multiplication Result:", result_multiplication) print("Division Result:", result_division)
An introduction to array operations using NumPy, showcasing basic mathematical operations.
# Assuming 'array1' and 'array2' are already defined # Perform basic array operations result_addition = array1 + array2 result_multiplication = array1 * array2 print("Array 1:", array1) print("Array 2:", array2) print("Addition Result:", result_addition) print("Multiplication Result:", result_multiplication)
Explore mathematical operations on NumPy arrays, such as addition and multiplication.
# Assuming 'array1' and 'array2' are already defined # Perform mathematical operations on arrays result_addition = np.add(array1, array2) result_multiplication = np.multiply(array1, array2) print("Array 1:", array1) print("Array 2:", array2) print("Addition Result:", result_addition) print("Multiplication Result:", result_multiplication)
Understand and perform element-wise operations on NumPy arrays, where each element is individually processed.
# Assuming 'array1' and 'array2' are already defined # Perform element-wise operations result_square = np.square(array1) result_sqrt = np.sqrt(array2) print("Array 1:", array1) print("Array 2:", array2) print("Square of Array 1:", result_square) print("Square Root of Array 2:", result_sqrt)
Sample code demonstrating various array operations using NumPy in Python.
# Assuming 'array1' and 'array2' are already defined # Perform sample array operations result_sum = np.sum(array1) result_mean = np.mean(array2) print("Array 1:", array1) print("Array 2:", array2) print("Sum of Array 1:", result_sum) print("Mean of Array 2:", result_mean)
Explore aggregate and statistical operations on NumPy arrays, such as sum and mean.
# Assuming 'array1' and 'array2' are already defined # Perform aggregate and statistical operations result_sum = np.sum(array1) result_mean = np.mean(array2) print("Array 1:", array1) print("Array 2:", array2) print("Sum of Array 1:", result_sum) print("Mean of Array 2:", result_mean)
Efficiently perform operations on large NumPy arrays by utilizing the vectorized nature of NumPy.
import numpy as np # Create large arrays for demonstration large_array1 = np.random.rand(10**6) large_array2 = np.random.rand(10**6) # Perform efficient array operations result_addition = large_array1 + large_array2 result_mean = np.mean(large_array1) print("Large Array 1 (Partial):", large_array1[:5]) print("Large Array 2 (Partial):", large_array2[:5]) print("Efficient Addition Result (Partial):", result_addition[:5]) print("Efficient Mean of Large Array 1:", result_mean)