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 scientific computing and deals primarily with numerical arrays and matrices. In this tutorial, we will cover the basics of creating and manipulating NumPy arrays.
Before diving into the tutorial, ensure you have NumPy installed:
pip install numpy
Then, you'll need to import NumPy:
import numpy as np
NumPy's main object is the homogeneous multidimensional array. It��s a table of elements, all of the same type.
arr = np.array([1, 2, 3, 4, 5]) print(arr) # Output: [1 2 3 4 5]
matrix = np.array([[1, 2], [3, 4], [5, 6]]) print(matrix)
print(arr.shape) # Output: (5,) print(matrix.shape) # Output: (3, 2) print(arr.ndim) # Output: 1 print(matrix.ndim) # Output: 2
NumPy offers functions to create specific types of arrays:
zeros = np.zeros(5) # Array of zeros ones = np.ones((3,3)) # 3x3 array of ones identity = np.eye(3) # 3x3 identity matrix range_arr = np.arange(10) # Array of numbers from 0 to 9
NumPy arrays allow for element-wise operations:
a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) print(a + b) # Output: [5 7 9] print(a * b) # Output: [ 4 10 18]
For matrix operations:
print(np.dot(a, b)) # Dot product
NumPy offers powerful indexing capabilities:
c = np.array([0, 1, 2, 3, 4, 5]) print(c[2:5]) # Output: [2 3 4] print(c[:4]) # Output: [0 1 2 3]
Multidimensional slicing:
d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(d[1, 2]) # Output: 6 (second row, third column) print(d[1:]) # Output: [[4 5 6] [7 8 9]] (all rows from the second onward)
You can change the shape of an array without altering its data:
e = np.arange(6) print(e.reshape(2, 3)) # Reshape to 2 rows and 3 columns
It refers to how NumPy handles element-wise operations with arrays of different shapes. For example, you can add a scalar to a matrix, and NumPy will add it to each element in the matrix.
f = np.array([[1, 2], [3, 4]]) print(f + 1) # Add 1 to every element
NumPy arrays form the core of scientific computing in Python. With their powerful capabilities and efficient implementations, they provide a flexible and fast array processing feature set. This tutorial touched on the basics; there's a lot more depth to explore as you become more accustomed to NumPy!
Description: NumPy provides a powerful array object that facilitates efficient mathematical operations on large datasets.
Code:
import numpy as np # Create a NumPy array array = np.array([1, 2, 3, 4, 5]) # Perform array operations squared_array = array ** 2 sum_array = np.sum(array) print("Original Array:") print(array) print("Squared Array:") print(squared_array) print("Sum of Array:") print(sum_array)
Description: NumPy supports a variety of array operations, including mathematical operations, statistical functions, and linear algebra operations.
Code:
import numpy as np # Create two arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Perform array operations sum_arrays = array1 + array2 dot_product = np.dot(array1, array2) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Sum of Arrays:") print(sum_arrays) print("Dot Product:") print(dot_product)
Description: Basic concepts of NumPy arrays, including array creation, attributes, and basic operations.
Code:
import numpy as np # Create a NumPy array array = np.array([1, 2, 3, 4, 5]) # Array attributes array_shape = array.shape array_dtype = array.dtype print("Array:") print(array) print("Array Shape:") print(array_shape) print("Array Data Type:") print(array_dtype)
Description: NumPy provides functions for creating and manipulating arrays, such as np.arange
and np.reshape
.
Code:
import numpy as np # Create an array using np.arange array1 = np.arange(1, 10, 2) # Reshape the array reshaped_array = array1.reshape(2, 2) print("Original Array:") print(array1) print("Reshaped Array:") print(reshaped_array)
Description: Indexing and slicing allow you to access and manipulate specific elements or subarrays within a NumPy array.
Code:
import numpy as np # Create a NumPy array array = np.array([1, 2, 3, 4, 5]) # Indexing and slicing first_element = array[0] subarray = array[2:4] print("Original Array:") print(array) print("First Element:") print(first_element) print("Subarray:") print(subarray)
Description: Comparing NumPy arrays with Python lists, highlighting the advantages of NumPy for numerical operations.
Code:
import numpy as np # Using NumPy arrays numpy_array = np.array([1, 2, 3]) # Using Python lists python_list = [1, 2, 3] # Array vs List operations numpy_squared = numpy_array ** 2 list_squared = [x ** 2 for x in python_list] print("NumPy Array Squared:") print(numpy_squared) print("List Squared:") print(list_squared)
Description: NumPy provides a variety of functions and methods for array manipulation, including mathematical and statistical functions.
Code:
import numpy as np # Create a NumPy array array = np.array([1, 2, 3, 4, 5]) # Array functions and methods array_sum = np.sum(array) array_mean = np.mean(array) array_max = np.max(array) print("Array:") print(array) print("Sum of Array:") print(array_sum) print("Mean of Array:") print(array_mean) print("Max of Array:") print(array_max)
Description: Reshaping and broadcasting operations in NumPy allow you to manipulate the shape and size of arrays efficiently.
Code:
import numpy as np # Create a NumPy array array = np.array([1, 2, 3, 4, 5, 6]) # Reshape the array reshaped_array = array.reshape(2, 3) # Broadcasting operation broadcasted_array = reshaped_array * 2 print("Original Array:") print(array) print("Reshaped Array:") print(reshaped_array) print("Broadcasted Array:") print(broadcasted_array)