Numpy Cheat Sheet

Okan Yeşiloğlu
5 min readMay 18, 2022

“NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more.”

In this tutorial, I am going to use jupyter notebook. Feel free to use which tool you are developing python.

Firstly let’s import NumPy to our project.

import numpy as np

Note that: When you create an array with NumPy. The array type becomes numpy.ndraray.

Let’s create a simple array.

np.array() function takes an array. In this example, we define one dimension array. it’s called a vector.

If you want to get array size:

array.shape
#run this you will see (5,)

Now, what if we want reshape our array.

To achieve this I will define a vector 1 x 9. Then reshape it to 3 x 3 matrix.

arrayName.reshape(row, column)

What about learning array dimensions size array.ndim

Maybe you are curious about data types in the array. arrayName.dtype.name

You can get array size arrayName.size

What if you need an array with a known size but you don't want to enter any value. Numpy has a built-in function for this. It gives you the functionality to create an array of empty, zeros, and ones.

np.zeros((row_size,column_size)) returns zeros NumPy array.

np.ones((row_size, column_size)) returns one's NumPy array.

np.ones((row_size, column_size)) returns empty some random numbers in memory.

What about generating random values with a start, end, and amount of increased value. (Note that end value not included)

np.arange(start value, end value, increase value)

Maybe you need random values in two ranges. To achieve that np.linspace(start_value, end_value, how_many_numbers_you_want)

In this section, we learned how to define arrays in different types.

Numpy can do matrix math :) Don’t forget this matrix size has to be the same.

Let’s create two matrices of 3x5.

Adding two Matrices

Subtracting Matrices

Division of Matrices

Numpy can the trigonometric calculating

Numpy can calculate exponential numbers

Numpy can get transpose the matrix.

`arrayName.T

“The transpose of a matrix is simply a flipped version of the original matrix. We can transpose a matrix by switching its rows with its columns. We denote the transpose of matrix A by A^T”

Numpy can multiply the matrix.

If you don’t know how to multiply matrices check this out.

Numpy can sum all numbers inside the matrix. a.sum()

You can get the max and min values in NumPy.

Numpy can summation in rows and columns.

Numpy can calculate square and square root

This is the last part of my article. In this section, we are going to slice the array.

What about you need the first column in the matrix.

Let’s figure out the matrix like this array[row, column] “:” this refers to getting all values. In our example, it refers to rows. You can take all rows and ranges of columns.

You can resize the matrix in different ways too. array.reval() function convert to vector.

You can resize the matrix using array.reshape() or array.resize() . The difference between these methods array.resize() affect actual matrix but array.reshape() doesn’t change the actual matrix.

Numpy can stack matrices vertically and horizontally.

This is my second English article. I will become better write I just need some time.

Thank you for reading.

jupyter notebook link:

--

--