Skip to main 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.

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

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)

Creating filled arrays

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.

Mathematical functions

Sinus, cosinus, tangens

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.

Sums

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

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