NumPy is a powerful library in Python that provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently. It is the foundation for many other scientific computing libraries like SciPy, Pandas, and more.
To use NumPy, you need to install it. You can install NumPy using pip:
pip install numpy
Once installed, you can import NumPy into your Python script or Jupyter Notebook. It is customary to import it using the alias np
:
import numpy as np
You can create a NumPy array from a Python list using the np.array()
function.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Create 2D or multi-dimensional arrays by passing nested lists.
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix)
Create arrays filled with zeros or ones using np.zeros()
and np.ones()
.
zeros = np.zeros((3, 3))
ones = np.ones((2, 4))
Use np.arange()
to create arrays with a specified range and step size, or np.linspace()
to create an array of evenly spaced values.
range_array = np.arange(0, 10, 2)
linspace_array = np.linspace(0, 1, 5)
Explore important attributes of arrays:
The shape of an array tells you its dimensions.
print(matrix.shape)
The total number of elements in the array.
print(matrix.size)
Check the data type of the array elements.
print(arr.dtype)
NumPy arrays can be indexed and sliced similarly to Python lists, but with more power, especially for multi-dimensional arrays.
arr = np.array([10, 20, 30, 40, 50])
print(arr[2]) # Outputs 30
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix[1, 2]) # Outputs 6
print(arr[1:4]) # Outputs [20 30 40]
print(matrix[:, 1]) # Outputs second column [2 5 8]
Select elements from arrays using lists or arrays of indices.
arr = np.array([10, 20, 30, 40, 50])
indices = [0, 2, 4]
print(arr[indices]) # Outputs [10 30 50]
NumPy supports element-wise operations as well as matrix operations.
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Element-wise addition
print(arr1 + arr2)
# Element-wise multiplication
print(arr1 * arr2)
NumPy provides many mathematical functions that operate element-wise.
arr = np.array([1, 2, 3, 4])
# Square root
print(np.sqrt(arr))
# Exponential
print(np.exp(arr))
There is a wide range of ufuncs available in NumPy for various mathematical operations: np.sin()
, np.cos()
, np.log()
, np.sum()
, np.mean()
, and more.
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
result = np.dot(matrix1, matrix2)
print(result)
print(matrix1.T)
Broadcasting allows NumPy to perform element-wise operations on arrays of different shapes.
arr = np.array([1, 2, 3])
matrix = np.array([[10, 20, 30], [40, 50, 60]])
# Broadcasting arr to match the shape of matrix
result = matrix + arr
print(result)
NumPy provides a variety of aggregation functions:
arr = np.array([1, 2, 3, 4, 5])
# Sum
print(np.sum(arr))
# Mean
print(np.mean(arr))
# Maximum
print(np.max(arr))
# Minimum
print(np.min(arr))
For multi-dimensional arrays, you can specify the axis:
matrix = np.array([[1, 2, 3], [4, 5, 6]])
# Sum along the columns
print(np.sum(matrix, axis=0))
# Sum along the rows
print(np.sum(matrix, axis=1))
np.diff
: Compute Differences Between ElementsThe np.diff()
function calculates the difference between consecutive elements in an array.
arr = np.array([10, 20, 30, 40, 50])
diff = np.diff(arr)
print(diff) # Outputs [10 10 10 10]
For 2D arrays, you can specify the axis along which to calculate the differences.
matrix = np.array([[1, 2, 4],
[7, 11, 13]])
diff_axis0 = np.diff(matrix, axis=0) # Difference along rows
diff_axis1 = np.diff(matrix, axis=1) # Difference along columns
print(diff_axis0) # Outputs [[6 9 9]]
print(diff_axis1) # Outputs [[ 1 2] [ 4 2]]
np.cumsum
: Compute Cumulative SumThe np.cumsum()
function returns the cumulative sum of elements along a given axis.
arr = np.array([1, 2, 3, 4, 5])
cumsum = np.cumsum(arr)
print(cumsum) # Outputs [ 1 3 6 10 15]
For 2D arrays, you can also specify the axis.
matrix = np.array([[1, 2, 3], [4, 5, 6]])
cumsum_axis0 = np.cumsum(matrix, axis=0) # Cumulative sum along columns
cumsum_axis1 = np.cumsum(matrix, axis=1) # Cumulative sum along rows
print(cumsum_axis0) # Outputs [[ 1 2 3] [ 5 7 9]]
print(cumsum_axis1) # Outputs [[ 1 3 6] [ 4 9 15]]
Change the shape of an array without changing its data.
arr = np.arange(1, 7)
reshaped = arr.reshape((2, 3))
print(reshaped)
Convert a multi-dimensional array to a 1D array.
flattened = reshaped.flatten()
print(flattened)
NumPy’s random
module provides functions for generating random numbers.
# Random integers
rand_ints = np.random.randint(0, 10, size=(3, 3))
# Random floats between 0 and 1
rand_floats = np.random.rand(3, 3)
# Normal distribution
rand_normal = np.random.randn(3, 3)
You can save arrays to disk in binary format using np.save()
or in text format using np.savetxt()
.
np.save('array.npy', arr)
np.savetxt('array.txt', arr)
Load arrays from files.
loaded_arr = np.load('array.npy')
loaded_txt_arr = np.loadtxt('array.txt')
. Compute the cumulative sum of a 2D matrix along both axes.
NumPy is a vast library with capabilities far beyond what is covered in this guide. This introduction should help you get started with basic array operations, mathematical functions, and array manipulation. As you progress, you’ll find that NumPy is indispensable for scientific computing and data analysis in Python. Happy coding!