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
Let's dive into a basic tutorial on NumPy arrays!
NumPy, which stands for Numerical Python, is a core library for scientific computing in Python. It provides a high-performance, multi-dimensional array object called ndarray (N-dimensional array) and tools for working with these arrays.
Before using NumPy, you need to install it:
pip install numpy
Once installed, you can import it:
import numpy as np
a) From Python Lists
arr = np.array([1, 2, 3, 4, 5]) print(arr)
b) Zeros, Ones, and Custom Values
zeros = np.zeros(5) print(zeros) ones = np.ones(5) print(ones) sevens = np.full(5, 7) print(sevens)
c) Range of Values
range_arr = np.arange(0, 10, 2) # Start, Stop, Step print(range_arr)
d) 2D Arrays (Matrices)
matrix = np.array([[1, 2], [3, 4], [5, 6]]) print(matrix)
arr = np.array([[1, 2, 3], [4, 5, 6]]) print("Shape:", arr.shape) # Shape of array (rows, columns) print("Dimension:", arr.ndim) # Number of dimensions print("Size:", arr.size) # Total number of elements print("Data Type:", arr.dtype) # Data type of elements
a) Indexing
arr = np.array([1, 2, 3, 4, 5]) print(arr[0]) # First element print(arr[-1]) # Last element
b) Slicing
print(arr[1:4]) # Second to fourth element
c) Modifying Values
arr[2] = 33 print(arr)
d) 2D Array Access
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(matrix[1, 2]) # Accessing second row, third column (value 6)
Arrays support all basic arithmetic operations:
a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) print(a + b) print(a - b) print(a * b) print(a / b)
NumPy arrays are more efficient than standard Python lists for numerical operations. Their vast number of built-in functionalities and operations make them a powerful tool for data manipulation, scientific computing, and more.
Remember that this tutorial just scratched the surface. NumPy offers much more, from advanced indexing and slicing to mathematical functions, statistical operations, and broadcasting capabilities.
Creating arrays with NumPy using various methods.
import numpy as np # Creating a one-dimensional array array_1d = np.array([1, 2, 3]) # Creating a two-dimensional array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Creating an array with zeros zeros_array = np.zeros((3, 4)) # Creating an array with ones ones_array = np.ones((2, 3)) print("One-dimensional array:", array_1d) print("Two-dimensional array:\n", array_2d) print("Zeros array:\n", zeros_array) print("Ones array:\n", ones_array)
Indexing and slicing operations on NumPy arrays.
import numpy as np # Creating a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Indexing element = array_2d[1, 2] # Slicing slice_row = array_2d[0, 1:] slice_column = array_2d[:, 2] print("Array:\n", array_2d) print("Indexed Element:", element) print("Sliced Row:", slice_row) print("Sliced Column:", slice_column)
Basic array operations in NumPy for beginners.
import numpy as np # Creating arrays array_a = np.array([1, 2, 3]) array_b = np.array([4, 5, 6]) # Addition result_addition = array_a + array_b # Multiplication result_multiplication = array_a * array_b print("Array A:", array_a) print("Array B:", array_b) print("Addition Result:", result_addition) print("Multiplication Result:", result_multiplication)
Working with data types in NumPy arrays.
import numpy as np # Creating arrays with specified data types array_int = np.array([1, 2, 3], dtype=int) array_float = np.array([1.1, 2.2, 3.3], dtype=float) # Changing data type array_int_to_float = array_int.astype(float) print("Array with int data type:", array_int) print("Array with float data type:", array_float) print("Array with changed data type:", array_int_to_float)
Reshaping NumPy arrays using numpy.reshape()
.
import numpy as np # Creating a 1D array array_1d = np.array([1, 2, 3, 4, 5, 6]) # Reshaping to a 2D array reshaped_array_2d = np.reshape(array_1d, (2, 3)) print("Original 1D array:", array_1d) print("Reshaped 2D array:\n", reshaped_array_2d)
Accessing elements in NumPy arrays using different methods.
import numpy as np # Creating a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Accessing elements using indices element = array_2d[1, 2] # Accessing elements using boolean indexing filtered_elements = array_2d[array_2d > 4] print("Array:\n", array_2d) print("Accessed Element:", element) print("Filtered Elements:", filtered_elements)
Exploring some useful NumPy array functions and methods.
import numpy as np # Creating an array array = np.array([1, 2, 3, 4, 5]) # NumPy array functions mean_value = np.mean(array) max_value = np.max(array) # NumPy array methods sum_value = array.sum() sorted_array = np.sort(array) print("Array:", array) print("Mean Value:", mean_value) print("Max Value:", max_value) print("Sum Value (using method):", sum_value) print("Sorted Array (using method):", sorted_array)