🔢 Numpy Basics¶
NumPy (Numerical Python) is a powerful library for numerical computations in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of functions to operate on these arrays efficiently. This tutorial will introduce you to the basics of NumPy and guide you through the process of creating arrays.
Once installed, you can import the NumPy library into your Python script using the following statement:
import numpy as np
The np
alias is commonly used for NumPy to save typing.
1. Creating Arrays¶
NumPy arrays are the fundamental data structure in NumPy. They are homogeneous, meaning all elements in an array must have the same data type.
1.1 From a Python List¶
One of the most common ways to create a NumPy array is by converting a Python list. Here's an example:
import numpy as np
my_list = [1, 2, 3, 4, 5]
my_array = np.array(my_list)
my_array, type(my_list), type(my_array)
(array([1, 2, 3, 4, 5]), list, numpy.ndarray)
1.2. Using Built-in Functions¶
NumPy provides several built-in functions to create arrays with specific properties. Some commonly used functions include:
np.zeros(shape)
: Creates an array filled with zeros.np.ones(shape)
: Creates an array filled with ones.np.full(shape, value)
: Creates an array filled with a specified value.np.arange(start, stop, step)
: Creates an array with a range of values.np.linspace(start, stop, num)
: Creates an array with evenly spaced values.
Here are some examples:
import numpy as np
zeros_array = np.zeros((2, 3)) # 2x3 array of zeros
ones_array = np.ones((3, 3)) # 3x3 array of ones
full_array = np.full((2, 2), 7) # 2x2 array filled with 7
range_array = np.arange(0, 10, 2) # array with values from 0 to 8 with a step of 2
linspace_array = np.linspace(0, 1, 5) # array with 5 evenly spaced values between 0 and 1
print("Array of zeros:\n", zeros_array)
print("Array of ones:\n", ones_array)
print("Array of sevens:\n", full_array)
print("Array of range:\n", range_array)
print("Array of linspace:\n", linspace_array)
Array of zeros: [[0. 0. 0.] [0. 0. 0.]] Array of ones: [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]] Array of sevens: [[7 7] [7 7]] Array of range: [0 2 4 6 8] Array of linspace: [0. 0.25 0.5 0.75 1. ]
1.3. Generating Random Arrays:¶
NumPy provides functions to generate arrays with random values. Here's an example:
import numpy as np
random_array = np.random.rand(2, 3) # 2x3 array with random values between 0 and 1
random_array
array([[0.12877505, 0.37812433, 0.94988499], [0.35069467, 0.73255702, 0.42488187]])
1.4. Creating Special Arrays:¶
NumPy also offers functions to create special types of arrays, such as identity matrices and diagonal matrices. Here's an example:
import numpy as np
identity_matrix = np.eye(3) # 3x3 identity matrix
diagonal_matrix = np.diag([1, 2, 3]) # diagonal matrix with [1, 2, 3] as diagonal elements
print("Identity matrix:\n", identity_matrix)
print("Diagonal matrix:\n", diagonal_matrix)
Identity matrix: [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] Diagonal matrix: [[1 0 0] [0 2 0] [0 0 3]]
You have learned the basics of NumPy and how to create arrays using various methods. NumPy provides a wide range of array manipulation and mathematical operations, making it an essential library for scientific computing in Python. Experiment with different array creation techniques and explore the NumPy documentation to further enhance your knowledge.
Remember to check the official NumPy documentation for more details: https://numpy.org/doc/