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 the foundational package for numerical computing in Python. One of its primary classes is the ndarray, or simply, array. This tutorial will guide you through various ways to create NumPy arrays.
First, ensure you have NumPy installed:
pip install numpy
Then, import the library:
import numpy as np
You can create a NumPy array directly from a Python list or tuple.
arr = np.array([1, 2, 3, 4, 5]) print(arr) # Output: [1 2 3 4 5]
For multi-dimensional arrays:
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(matrix)
zeros
:To create an array filled with zeros:
zeros_arr = np.zeros(5) print(zeros_arr) # Output: [0. 0. 0. 0. 0.]
ones
:For an array filled with ones:
ones_arr = np.ones(5) print(ones_arr) # Output: [1. 1. 1. 1. 1.]
arange
:To create an array with a sequence of numbers:
sequence_arr = np.arange(10) print(sequence_arr) # Output: [0 1 2 3 4 5 6 7 8 9]
linspace
:For evenly spaced numbers over a specified range:
linspace_arr = np.linspace(0, 1, 5) print(linspace_arr) # Output: [0. 0.25 0.5 0.75 1. ]
empty
:To create an uninitialized array (its content is not predictable):
empty_arr = np.empty(5) print(empty_arr)
eye
:For a 2-D array with ones on the diagonal and zeros elsewhere:
identity_matrix = np.eye(3) print(identity_matrix)
full
:To create an array filled with any given value:
full_arr = np.full(5, 7) print(full_arr) # Output: [7 7 7 7 7]
To create an array of random values:
random_arr = np.random.rand(5) print(random_arr)
NumPy offers a variety of methods to create arrays, from simple sequences to random initializations. Knowing which method to use can help in efficiently setting up your data structures for numerical and matrix operations.
Description: NumPy provides various functions for creating arrays, allowing you to initialize arrays with specified values or shapes.
Code:
import numpy as np # Creating a 1D array array_1d = np.array([1, 2, 3]) # Creating a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) print("1D Array:") print(array_1d) print("2D Array:") print(array_2d)
Description: Initialization of arrays involves creating arrays with specified values or patterns.
Code:
import numpy as np # Initializing a 1D array with zeros zeros_array = np.zeros(5) # Initializing a 2D array with ones ones_array = np.ones((3, 4)) print("Zeros Array:") print(zeros_array) print("Ones Array:") print(ones_array)
Description: NumPy provides various array creation functions for different use cases, including np.zeros
, np.ones
, and np.empty
.
Code:
import numpy as np # Using array creation functions zeros_array = np.zeros(3) ones_array = np.ones((2, 4)) empty_array = np.empty(6) print("Zeros Array:") print(zeros_array) print("Ones Array:") print(ones_array) print("Empty Array:") print(empty_array)
Description: You can create a NumPy array by converting a Python list.
Code:
import numpy as np # Creating an array from a Python list python_list = [1, 2, 3, 4, 5] numpy_array = np.array(python_list) print("Python List:") print(python_list) print("NumPy Array:") print(numpy_array)
Description: Initialize arrays with specific values or patterns using NumPy's functions.
Code:
import numpy as np # Initialize an array with a specific value value_array = np.full((2, 3), 7) # Initialize an array with a range of values range_array = np.arange(1, 10, 2) print("Array with Specific Value:") print(value_array) print("Array with Range of Values:") print(range_array)
Description: NumPy provides np.zeros
and np.ones
functions to create arrays filled with zeros or ones.
Code:
import numpy as np # Create arrays filled with zeros and ones zeros_array = np.zeros((2, 3)) ones_array = np.ones(4) print("Zeros Array:") print(zeros_array) print("Ones Array:") print(ones_array)
Description: NumPy's np.random
module allows the generation of arrays with random values.
Code:
import numpy as np # Generate random arrays random_array = np.random.random((2, 3)) randint_array = np.random.randint(1, 10, size=(3, 3)) print("Random Array:") print(random_array) print("Random Integer Array:") print(randint_array)
Description: NumPy enables the creation of specific types of arrays, such as identity matrices and diagonal matrices.
Code:
import numpy as np # Create an identity matrix identity_matrix = np.eye(3) # Create a diagonal matrix diagonal_matrix = np.diag([1, 2, 3]) print("Identity Matrix:") print(identity_matrix) print("Diagonal Matrix:") print(diagonal_matrix)