NumPy Cheatsheet

Gamze Yılan
Nerd For Tech
Published in
6 min readNov 26, 2022

--

So you need a quick look on NumPy that’ll get you working hands-on? This article is just what you need!

Getting Started

NumPy is a scientific computing package in Python. It comes with pre-defined functions for linear algebra, fourier transform and matrices. NumPy is generally used for Data Science applications.

In order to get started, first ensure you have Python and NumPy in your system. How you get it depends on your system and the IDE of your preference, but I recommend Anaconda. With the basic Anaconda installation you can simply go to terminal and run “conda install numpy” & voila!

Now, let’s create a example.py file to start working with.

Note: If we were to create a project instead of a single example file to work with, we would have to simply create a directory and a __init__.py file inside it.

You should add the following line to the top of the file in order to be able to work with NumPy within the file.

import numpy as np

Note: We generally import it as np so that we don’t have to type numpy everytime we call a function from the NumPy. Keep in mind that altough can be changed, np is the general convention.

After that, you can run the code within the example file by simply going to the terminal and running the command:

python example.py

List vs Array

We’re going to use Arrays a lot with our NumPy operations, so it’s better to clarify one thing: lists and arrays are different in Python.

With Arrays you can declare multi-dimentional structures like matrices, and you can perform numerical operations within but that’s not the case with the simple old Lists. Hence, NumPy heavily relies on Arrays.

Lists can be assigned to a value without having to be declared, but Arrays can’t. So in order to define an array you must do as:

exampleArray = np.arrray([10, 15, 20]) // single dimensional array

Or you can define a multi-dimentional array as:

exampleArray2 = np.array([10, 15, 20], [50, 100, 150], [1, 2, 3])

You can call items within the array as:

exampleArray2 [1] // returns [50, 100, 150]

exampleArray2 [1] [0]// returns 50

Methods

Below, you’re going to find the most basic functions that are commonly used when working with NumPy. Although there are many, many more the following will be sufficient to get you started.

Array Creation Methods

  • np.arange(0, 10): Creates a single-dimensional array with numbers 0 to 9 as items.
  • np.arange(0, 10, 3): Creates a single-dimensional array with numbers between 0 to 10 jumping to every third. Returns an array with 0,3,6,9 as items.
  • np.zeros(3): Creates a 3 item array with the value 0 for each item.
  • np.ones(3): Creates a 3 item array with the value 1 for each item.
  • np.linspace(0,20,5): Takes 5 numbers between 0 and 20 with equal steps in between. Returns an array of items 0, 5, 10, 15, 20. Keep in mind that if we said select 6 items, the items woudl be floats instead of intefers to ensure equal space in between.
  • np.eye(3): Creates an identity matrix with 3 columns and 3 rows. The diagonal values return all 1 and all other values return 0.

Random-Related Methods

  • np.random.randn(5): Creates a single-dimensional array with 5 random numbers as items. The numbers can be of type integer and/or float, negative and/or positive.
  • np.random.randn(5,5): Creates a matrix with 5 rows and 5 columns with 25 random values as items.
  • np.random.randint(1, 10, 2): Returns two random numbers that are greater than 1 and less than 10.

Array-Related Methods

  • myArray.reshape(5,5): Takes the single-dimensioned array named myArray and returns it as a matrix with 5 rows and 5 columns. Keep in mind that if there are not sufficient number of items within the array, in the example case 5 x 5 = 25 items, this will throw an error and not work. This method will not change the array itself but simply return a changed version of the array.
  • myArray.max(): Returns the biggest number within the items of a number array.
  • myArray.min(): Returns the smallest number within the items of a number array.
  • myArray.argmax(): Returns the index of the biggest number within the items of a number array.
  • myArray.argmin(): Returns the index of the smallest number within the items of a number array.
  • myArray.shape: Returns the shape of an array. If the array is a single dimensioned one with 10 items, it’ll return (10, ). If the array is a matrix that consists of 5 columns and 6 rows, it’ll return (5,6). Keep in mind that this is not a function but instead an attribute so there’re no parantheses when calling shape.

Array Indexes

  • myArray[5]: Returns the item from the array with the index number of 5
  • myArray[3:5]: Returns the items between the index numbers 3 and 5, including the item with the index 3 but not including 5.
  • myArray[3:5] = -3: Changes the value of the items between the indexes 3 and five to -3.
  • myArray[:] = 30: Changes the value of all items within the array to 30one by one.
  • myArray = 30: Changes the type of the myArray from array to integer and sets it’s value to 30.
  • newArray = myArray.copy(): Creates a copy of myArray and sets it to the newArray. Note that if you didn’t use the .copy() method, you’d see all changes you’ve made to the newArray reflecting on the myArray as well.

Matrix Indexes

Let’s declare a matrix as myMatrix = ( [5, 10, 15] , [1, 2, 3] , [0, 0, 0] ).

  • myMatrix[0]: Returns the item with the row index 0, which is an array. Therefore returns [5, 10, 15].
  • myMatrix[0] [1]: Returns the item with the column index 1 and the row index 0, so in this case returns the value 10.
  • myMatrix[0,1]: Returns the item with the column index 1 and the row index 0, so in this case returns the value 10.
  • myMatrix[1, 1:]: Takes the row with index 1 as usual, but returns the items with the column indexes starting from 1(including 1 as the splice always works with Python) so in this case returns [2, 3].
  • myMatrix[ [0,1] ]: Returns the value of indexes 0 and 1 so in our case returns ( [5, 10, 15] , [1, 2, 3] ).

NumPy Operations

Let’s define an array myArray to work with as myArray = [ 5,10,15,20]

  • myArray > 10: Returns an array of true and false judging by the condition, so in our case returns [false, false, true, true]. Note that tihs doesn’t change the original value of myArray but instead creates a new awway.
  • myArray [myArray > 10]: Returns a new array with the items that actually follow the condition as [15, 20].
  • myArray + myArray: Returns an array where the values are summed index by index and placed onto the corresponding index, in our case returns [10, 20, 30, 40].
  • myArray — myArray: Returns an array where the values are subtracted index by index and placed onto the corresponding index, in our case returns [0, 0, 0, 0].
  • myArray/myArray: Returns an array where the values are divided index by index and placed onto the corresponding index, in our case returns [1,1,1,1]. Note that if there was a 0 in any indexes as a value, since a number can’t be devided by 0, this would throw a warning & return nan for that index but divide the other indexes as usual.
  • np.sqrt(myArray): Returns an array with the square root if each value on the same index. So if myArray was [25, 16, 36] the function would return [ 5, 4, 6].

--

--