Skip to content

Numpy cheat sheet

Numpy is a Python library for working with arrays and matrices. Selling point of the library is its high performance, as Numpy is based on C. Let’s go over the most important features of Numpy.

But first, we have to install Numpy.

Terminal window
pip install numpy

Then, you can import it and use it in your Python project:

import numpy as np

Of course you can import Numpy “as” everything you want, but the standard is to import it as np. Therefore, calling Numpy functions works with np.function.

Arrays can be created and manipulated like the default lists in Python.

arr = np.array([1, 2, 3])
arr[0] # 1

Also, lists can be transformed to Numpy arrays:

numbers = [1, 2, 3]
arr = np.asarray(numbers)

Numpy holds different functions for creating arrays of a given shape, filled with the same value.

np.ones(5)
# array([1., 1., 1., 1., 1.])

np.ones creates an array filled with ones, of size 5. The shape is by default one-dimensional. For non-one-dimensional arrays, you need to pass one more pair of brackets like this:

np.ones((3, 3))

Which creates a 3 x 3 array, filled with ones.

instead of .ones , one can also use .zeros for filling the array with zeros.

Array iteration in Numpy allows you to perform operations on array elements. You can iterate over elements, rows, and columns, and use efficient iteration tools like nditer.

You can iterate over each element in a Numpy array using a simple for loop.

import numpy as np
arr = np.array([1, 2, 3])
for x in arr:
print(x)

For 2D arrays, nested loops are used to iterate through rows and columns.

arr = np.array([[1, 2], [3, 4]])
for row in arr:
for x in row:
print(x)

nditer provides a flexible way to iterate over an array.

arr = np.array([[1, 2], [3, 4]])
for x in np.nditer(arr):
print(x)

Numpy provides a range of functions for linear algebra operations.

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
result = np.dot(a, b)

You can find the determinant and inverse of a matrix.

matrix = np.array([[1, 2], [3, 4]])
det = np.linalg.det(matrix)
inv = np.linalg.inv(matrix)

Numpy allows you to compute eigenvalues and eigenvectors of a matrix.

matrix = np.array([[1, 2], [3, 4]])
eigenvalues, eigenvectors = np.linalg.eig(matrix)

Numpy offers various statistical functions.

arr = np.array([1, 2, 3, 4, 5])
std_dev = np.std(arr)
variance = np.var(arr)
median = np.median(arr)
arr = np.array([3, 1, 5, 2])
sorted_arr = np.sort(arr)

Numpy supports various file formats for saving and loading data.

arr = np.array([1, 2, 3, 4, 5])
np.save('my_array', arr)
loaded_arr = np.load('my_array.npy')

Numpy supports file formats like .npy and .npz for efficient storage and retrieval of array data.

Numpy’s random module can be used for generating random numbers and arrays.

random_arr = np.random.rand(4)
np.random.seed(0)

This cheat sheet provides a quick overview of some of the most essential aspects of Numpy for programmers. As you delve deeper into Numpy, you’ll discover a plethora of functions and features that make Python an excellent choice for scientific computing.

All of these functions work element-wise, we pass an array of values and receive the function results.

np.sin([1, 2, 3])
# array([0.84147098, 0.90929743, 0.14112001])

For the cosinus and tangens, use np.cos, np.tan, np.arcsin and so on.

With np.sum one can sum up the values of an array-like structure.

numbers = [1, 2, 3]
np.sum(numbers)
# 6