Numpy is a powerful library for numerical computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently.
Installation
To install Numpy, you can use pip:
pip install numpy
Basic Usage
Here's a simple example of how to create a Numpy array and perform some operations on it:
import numpy as np
# Create a 1D array
arr = np.array([1, 2, 3, 4, 5])
# Print the array
print(arr)
# Create a 2D array
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
# Print the array
print(arr2)
# Perform some operations
sum_arr = np.sum(arr)
print("Sum of array:", sum_arr)
mean_arr = np.mean(arr2)
print("Mean of array:", mean_arr)
Advanced Features
Numpy offers a wide range of advanced features, including:
- Broadcasting: Allows you to perform operations on arrays of different shapes.
- Slicing: Extracting parts of arrays.
- Indexing: Accessing elements of arrays.
- Ufuncs: Universal functions that operate element-wise on arrays.
For more information, you can visit the Numpy documentation.
Example: Image Processing
Numpy is commonly used in image processing. Here's a simple example of loading an image using Numpy:
import numpy as np
from PIL import Image
# Load an image
img = Image.open('example.jpg')
# Convert the image to a Numpy array
img_array = np.array(img)
# Print the shape of the array
print("Image shape:", img_array.shape)
For more information on image processing with Numpy, you can check out our image processing tutorial.
Numpy Logo