Introduction to NumPy
NumPy is a powerful open-source library in Python that provides support for large, multi-dimensional arrays and matrices. It also includes a large collection of high-level mathematical functions to operate on these arrays. NumPy is widely used in the field of scientific computing, machine learning, and data analysis.
One of the key features of NumPy is its ability to perform element-wise operations on arrays. This means that you can apply a function or operation to each individual element of an array, without having to loop through the array manually.
To get started with NumPy, you first need to install it. You can install NumPy using pip, the Python package manager, by running the following command in your terminal:
pip install numpy
Once you have installed NumPy, you can import it into your Python script using the following line:
import numpy as np
Now, you can start creating and manipulating NumPy arrays. Here's an example of how to create a simple 1D array:
## Create a 1D array
arr = np.array([1, 2, 3, 4, 5])
print(arr)
This will output:
[1 2 3 4 5]
You can also create multi-dimensional arrays, such as 2D arrays (matrices):
## Create a 2D array
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix)
This will output:
[[1 2 3]
[4 5 6]
[7 8 9]]
In the next section, we'll dive deeper into element-wise operations with NumPy arrays.