"Introduction to NumPy" serves as a gateway to understanding the fundamental aspects of numerical computing in Python. NumPy, short for Numerical Python, stands as a cornerstone library in the Python ecosystem, offering robust support for multidimensional arrays and a suite of mathematical functions.
NumPy, short for Numerical Python, is a fundamental library for scientific computing in Python. It provides support for multidimensional arrays and matrices, along with a wide range of mathematical functions to operate on these arrays efficiently. NumPy is the cornerstone of many scientific and numerical computing tasks in Python.
NumPy offers several advantages over regular Python lists:
You can install NumPy using pip, the Python package manager:
pip install numpy
After installing NumPy, you can import it into your Python scripts or interactive sessions using:
import numpy as np
Here, np
is a commonly used alias for NumPy, making it convenient to reference NumPy functions and objects.
NumPy arrays can be created in various ways:
arr = np.array([1, 2, 3, 4, 5])
print(arr)
[1 2 3 4 5]
np
.zeros()
, ones()
, and arange()
:
zeros_arr = np.zeros((2, 3))
print(zeros_arr)
[[0. 0. 0.]
[0. 0. 0.]]
np
.
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
arr = np.array(data)
print(arr)
[[1 2 3]
[4 5 6]
[7 8 9]]
np
.NumPy arrays have attributes like shape
and ndim
that provide information about the array’s dimensions.
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Shape:", arr.shape) # Shape of the array
print("Dimensions:", arr.ndim) # Number of dimensions
Shape: (2, 3)
Dimensions: 2
Here, arr
is a 2-dimensional array with a shape of (2, 3)
.
Arrays can be reshaped into different shapes using the reshape()
method.
arr = np.arange(1, 10)
reshaped_arr = arr.reshape(3, 3)
print(reshaped_arr)
[[1 2 3]
[4 5 6]
[7 8 9]]
Here, we reshape a 1-dimensional array into a 3×3 matrix.
The T
attribute can be used to transpose arrays.
arr = np.array([[1, 2, 3], [4, 5, 6]])
transposed_arr = arr.T
print(transposed_arr)
[[1 4]
[2 5]
[3 6]]
The transpose of a 2×3 array becomes a 3×2 array.
NumPy arrays support element-wise arithmetic operations.
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = arr1 + arr2
print(result)
[5 7 9]
Here, result
contains the element-wise sum of arr1
and arr2
.
NumPy provides universal functions (ufuncs) for element-wise operations like np.add()
, np.subtract()
, np.multiply()
, and np.divide()
.
arr = np.array([1, 2, 3])
result = np.square(arr)
print(result)
[1 4 9]
Here, np.square()
computes the square of each element in the array.
NumPy provides functions for linear algebra operations like matrix multiplication (np.dot()
) and matrix inversion (np.linalg.inv()
).
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
result = np.dot(matrix1, matrix2)
print(result)
[[19 22]
[43 50]]
The result is the matrix product of matrix1
and matrix2
.
You can access elements or slices of NumPy arrays using indexing and slicing similar to Python lists.
arr = np.array([1, 2, 3, 4, 5])
print(arr[0]) # Accessing the first element
print(arr[1:4]) # Slicing from index 1 to 3
1
[2 3 4]
Here, we access elements and slices of the array arr
.
You can use boolean arrays for indexing to filter elements based on conditions.
arr = np.array([1, 2, 3, 4, 5])
mask = arr > 3
print(arr[mask])
[4 5]
arr
that are greater than 3.Fancy indexing allows you to access multiple elements or rows using integer arrays.
arr = np.array([1, 2, 3, 4, 5])
indices = np.array([0, 2, 4])
print(arr[indices])
[1 3 5]
arr
.Broadcasting is a powerful mechanism in NumPy that allows for element-wise operations between arrays of different shapes, without the need for explicit looping.
When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions and works its way backward. Two dimensions are compatible when they are equal or one of them is 1.
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([10, 20, 30])
result = arr1 + arr2
print(result)
[[11 22 33]
[14 25 36]]
Here, arr2
is broadcasted to the shape of arr1
, and then element-wise addition is performed.
NumPy provides various trigonometric functions like np.sin()
, np.cos()
, and np.tan()
.
angle = np.pi / 6
sin_val = np.sin(angle)
print(sin_val)
0.5
np.sin()
computes the sine of the angle in radians.NumPy provides functions for exponential and logarithmic operations like np.exp()
and np.log()
.
x = 2
exp_val = np.exp(x)
log_val = np.log(x)
print("Exponential:", exp_val)
print("Logarithm:", log_val)
Exponential: 7.38905609893065
Logarithm: 0.6931471805599453
np.exp()
computes the exponential of x
, and np.log()
computes the natural logarithm of x
.
In this topic, we covered a wide range of topics, from the basics of array creation to advanced mathematical functions. NumPy stands as a powerful library for numerical computing in Python, offering efficient array operations, mathematical functions, and tools for data manipulation. Happy coding! ❤️