Numpy Mathematics Cheat Sheet



NumPy is the library that gives Python its ability to work with data at speed. Originally, launched in 1995 as ‘Numeric,’ NumPy is the foundation on whichmany important Python data science libraries are built, including Pandas, SciPy and scikit-learn.

import numpy as np Selective import from math import pi help(str) Python For Data Science Cheat Sheet Python Basics Learn More Python for Data Science Interactively at www.datacamp.com Variable Assignment Strings x=5 x 5 x + 2 Sum of two variables 7 x. Bonus Cheat Sheet: From NumPy to xtensor xtensor is a C library, similar to NumPy, made for numerical analysis. The cheat sheet provides a two-column view, where the first column is NumPy, and the second column contains the xtensor equivalents. The sheet focuses on array initialization, reshaping, and slicing functions.

Key and Imports

In this cheat sheet, we use the following shorthand:

arrA NumPy Array object

Eventually, I compiled over 20 Machine Learning-related cheat sheets. Some I reference frequently and thought others may benefit from them too. This post contains 27 of the better cheat sheets I.

You’ll also need to import numpy to get started:

Importing/exporting

np.loadtxt('file.txt')From a text file
np.genfromtxt('file.csv',delimiter=',')From a CSV file
np.savetxt('file.txt',arr,delimiter=' ')Writes to a text file
np.savetxt('file.csv',arr,delimiter=',')Writes to a CSV file

Creating Arrays

np.empty((1, 2)) | create an empty 1x2 array. The value at each position is uninitialized (random value depending on the memory location).np.array([1,2,3]) | One dimensional array. Keyword argument dtype converts elements into specified type.np.array([(1,2,3),(4,5,6)]) | Two dimensional array
np.zeros(3) | 1D array of length 3 all values 0
np.ones((3,4)) | 3x4 array with all values 1
np.eye(5) | 5x5 array of 0 with 1 on diagonal (Identity matrix)
np.linspace(0,100,6) | Array of 6 evenly divided values from 0 to 100
np.arange(0,10,3) | Array of values from 0 to less than 10 with step 3 (eg [0,3,6,9])
np.full((2,3),8) | 2x3 array with all values 8
np.random.rand(4,5) | 4x5 array of random floats between 0-1
np.random.rand(6,7)*100 | 6x7 array of random floats between 0-100
np.random.randint(5,size=(2,3)) | 2x3 array with random ints between 0-4

Inspecting Properties

arr.sizeReturns number of elements in arr
arr.shapeReturns dimensions of arr (rows,columns)
arr.dtypeReturns type of elements in arr
arr.astype(dtype)Convert arr elements to type dtype
arr.tolist()Convert arr to a Python list
np.info(np.eye)View documentation for np.eye
np.copy(arr)Copies arr to new memory
arr.view(dtype)Creates view of arr elements with type dtype
arr.sort()Sorts arr
arr.sort(axis=0)Sorts specific axis of arr
two_d_arr.flatten()Flattens 2D array two_d_arr to 1D
arr.TTransposes arr (rows become columns and vice versa)
arr.reshape(3,4)Reshapes arr to 3 rows, 4 columns without changing data
arr.resize((5,6))Changes arr shape to 5x6 and fills new values with 0

Adding/removing Elements

np.append(arr,values)Appends values to end of arr
np.insert(arr,2,values)Inserts values into arr before index 2
np.delete(arr,3,axis=0)Deletes row on index 3 of arr
np.delete(arr,4,axis=1)Deletes column on index 4 of arr

Combining

Numpy And Pandas Cheat Sheet

np.vstack((arr1, arr2))Vertically stack multiple arrays. Think of it like the second arrays’s items being added as new rows to the first array.
np.hstack((arr1, arr2))horizontally stack multiple arrays.
np.concatenate((arr1,arr2),axis=0)Adds arr2 as rows to the end of arr1. It’s a general-purpose vstack.
np.concatenate((arr1,arr2),axis=1)Adds arr2 as columns to end of arr1. It’s a general-purpose hstack.
np.split(arr,3)Splits arr into 3 sub-arrays
np.hsplit(arr,5)Splits arr horizontally on the 5th index

Indexing

arr[5]Returns the element at index 5
arr[2,5]Returns the 2D array element on index [2][5]
arr[1]=4Assigns array element on index 1 the value 4
arr[1,3]=10Assigns array element on index [1][3] the value 10
arr[0:3]Returns the elements at indices 0,1,2 (On a 2D array: returns rows 0,1,2)
arr[0:3,4]Returns the elements on rows 0,1,2 at column 4
arr[:2]Returns the elements at indices 0,1 (On a 2D array: returns rows 0,1)
arr[:,1]Returns the elements at index 1 on all rows
arr<5Returns an array with boolean values
(arr1<3) & (arr2>5)Returns an array with boolean values
~arrInverts a boolean array
arr[arr<5]Returns array elements smaller than 5

Conditional Selecting

NumPy makes it possible to test to see if rows match certain values usingmathematical comparison operations like <, >, >=, <=, and . Forexample, if we want to see which wines have a quality rating higher than 5,we can do this:

We get a Boolean array that tells us which of the wines have a quality ratinggreater than 5. We can do something similar with the other operators. Forinstance, we can see if any wines have a quality rating equal to 10:

One of the powerful things we can do with a Boolean array and a NumPy array isselect only certain rows or columns in the NumPy array. For example, the belowcode will only select rows in wines where the quality is over 7:

We select only the rows where high_quality contains a True value, and allof the columns. This subsetting makes it simple to filter arrays for certaincriteria. For example, we can look for wines with a lot of alcohol and highquality. In order to specify multiple conditions, we have to place eachcondition in parentheses, and separate conditions with an ampersand (&):

We can combine subsetting and assignment to overwrite certain values in anarray:

Reshaping NumPy Arrays

Numpy Mathematics Cheat Sheet
numpy.transpose(arr)Transpose the array.
numpy.ravel(arr)Turn an array into a one-dimensional representation.
numpy.reshape(arr)Reshape an array to a certain shape we specify.

Scalar Math

If you do any of the basic mathematical operations (/, *, -, +, ^) with an array and a value, it will apply the operation to each of the elements in the array.

np.add(arr,1) or arr + 1Add 1 to each array element
np.subtract(arr,2) or arr - 2Subtract 2 from each array element
np.multiply(arr,3) or arr * 3Multiply each array element by 3
np.divide(arr,4) or arr / 4Divide each array element by 4 (returns np.nan for division by zero)
np.power(arr,5) or arr ^ 5Raise each array element to the 5th power

Note that the above operation won’t change the wines array – it will return a new 1-dimensional array where 10 has been added to each element in the quality column of wines.

If we instead did +=, we’d modify the array in place.

Vector Math

All of the common operations (/, *, -, +, ^) will work between arrays.

np.add(arr1,arr2)Elementwise add arr2 to arr1
np.subtract(arr1,arr2)Elementwise subtract arr2 from arr1
np.multiply(arr1,arr2)Elementwise multiply arr1 by arr2
np.divide(arr1,arr2)Elementwise divide arr1 by arr2
np.power(arr1,arr2)Elementwise raise arr1 raised to the power of arr2
np.array_equal(arr1,arr2)Returns True if the arrays have the same elements and shape
np.sqrt(arr)Square root of each element in the array
np.sin(arr)Sine of each element in the array
np.log(arr)Natural log of each element in the array
np.abs(arr)Absolute value of each element in the array
np.ceil(arr)Rounds up to the nearest int
np.floor(arr)Rounds down to the nearest int
np.round(arr)Rounds to the nearest int

Statistics

np.mean(arr,axis=0)Returns mean along specific axis
arr.sum()Returns sum of arr
arr.min()Returns minimum value of arr
arr.max(axis=0)Returns maximum value of specific axis
np.var(arr)Returns the variance of array
np.std(arr,axis=1)Returns the standard deviation of specific axis
arr.corrcoef()Returns correlation coefficient of array

Acknowledgement

The original post can be found at dataquest.io.

Little time to learn NumPy? This article shows you the ten most amazing NumPy cheat sheets. Download them, print them, and pin them to your wall — and watch your data science skills grow! 🐱‍🏍

All NumPy cheat sheets in this article are 100% free. All links open in a new tab (so feel free to click all links without worrying about losing this page).

Numpy mathematics cheat sheet pdf

Here’s a quick summary if you don’t have time reading all cheat sheets:

Here’s a quick download for you: I created this cheating sheet to explain some important NumPy concepts to my coding students.

NumPy is a widely used Python scientific computing package. It simplifies linear algebra, matrix computations, and speeds up data analysis. Knowing NumPy is a prerequisite for other Python packages like pandas or Scikit-Learn.

This article should serve as the ultimate NumPy reference. The cheat sheets are diverse and range from one page to multiple pages. They also involve cross-language comparison cheat sheets. While some resources are great beginner’s references, others are involved and require high-level expertise.

Cheat Sheet 1: DataCamp NumPy

Numpy

DataCamp is an online platform that offers data science training through videos and coding exercises. This cheat sheet is one of the most comprehensive one-page cheat sheets available. In a way, it adds to the previous cheat sheet with more examples and more functions. It is a good summary of creating arrays and basic array design. This cheat sheet provides functions for the specific datatypes. At the end of the sheet is more advanced stuff like slicing and indexing. There are also some introductory tools for data analysis and array manipulation. Though overall, this is a fantastic resource, the one drawback is the color palette. The bright orange is a distraction from the content. If you like the color palette, this could be your comprehensive go-to list of the NumPy basics.

  • Pros: Comprehensive, graphics, very dense
  • Cons: Overwhelming, color may distract

Cheat Sheet 2: Basic NumPy

This is a useful resource for the NumPy basics. It provides a summary of creating arrays and some basic operations. It is minimalistic, with a good overview of many basic functions. The sheet is divided into sections with headers for easier orientation. On the left-hand side of the sheet, the NumPy import convention is mentioned import numpy as np. A one-line explanation follows each function. The biggest advantage of this list is good readability. This enables a quick search for the right function.

  • Pros: Lightweight, basics, minimalistic, readable
  • Cons: Shallow, distracting background image, no visuals

Cheat Sheet 3: A Little Bit of Everything

The cheat sheet is divided into four parts. The first part goes into details about NumPy arrays, and some useful functions like or finding the number of dimensions. The 2nd part focuses on slicing and indexing, and it provides some delightful examples of Boolean indexing. The last two columns are a little bit disconnected. They provide a wide range of functions, ranging from matrix operations like transpose to sorting an array. However, the last two columns are not necessarily grouped conveniently. The advantage of this sheet is that it also includes Boolean and not only the numerical types.

  • Pros: Focus on slicing and dimensionality
  • Cons: Disconnected content, slightly confusing, no graphics

Cheat Sheet 4: Data Science

Dataquest is a similar online platform to DataCamp. It offers a variety of data science tracks and lessons, followed by coding exercises. This is another good resource of the most important NumPy functions and properties. The cheat sheet is readable with distinct sections, and each section has a clear title. Besides the sheet organization and excellent readability, it provides a range of functions and operations. Also, compared to the previous two cheat sheets, there is a math and statistics section. It divides the math sections into scalar and vector math, and there is a statistics section at the bottom.

  • Pros: Statistics, HTML-based, well-structured, comprehensive, readable
  • Cons: No graphics

Cheat Sheet 5: NumPy for Matlab Users

If you are a Matlab user and need a quick introduction to Python and NumPy, this could be your go-to. The sheet contains three columns – the first column is the Matlab/Octave, the second column are the Python and NumPy equivalents, and the third is a description column. The sheet’s focus is not solely on NumPy, but there are many Python basics listed. Since it is not a single sheet, the content is organized into separate sections. It provides math, logical and boolean operators, roots and round offs, complex numbers, extensive linear algebra, reshaping and indexing, some basic plots, calculus, and statistics.

  • Pros: Matlab, Python background, a wide spectrum of related content
  • Cons: Unfocused, not visually too appealing, font choice strange
Numpy Mathematics Cheat Sheet

Cheat Sheet 6: The Matrix

This cheat sheet provides the equivalents for four different languages – MATLAB/Octave, Python and NumPy, R, and Julia. The list is not a single PDF sheet, but it is a scrollable document. On each far left-hand and the right-hand side of the document, there are task descriptions. This is an extensive sheet, and it is extra useful because the output of each task is given. The sheet covers creating and designing of matrices, matrix shape manipulation, and some basic and more advanced matrix operations. The advanced section is particularly interesting because it lists many useful functions in data analysis, like finding a covariance and eigenvalues and creating random normally distributed variables.

  • Pros: Wide spectrum of related topics (Matlab, Python, NumPy, R, Julia), advanced-level features
  • Cons: Bad readability, no PDF download

Cheat Sheet 7: Numerical Analysis

This is the most comprehensive sheet on the list. Not only that includes side-to-side equivalents between MATLAB, R, NumPy, and Julia; and it also covers everything from functions and syntax, to loops and I/O. The most interesting and useful component is that certain lines like a function definition are given for MATLAB, R, and Julia, but not for NumPy because of the lack of that functionality. That makes it easy to compare and contrast and to find the best fit for a project.

  • Pros: Extremely comprehensive
  • Cons: Not well-structured, poor design, no PDF download

Numpy Mathematics Cheat Sheets

Cheat Sheet 8: NumPy for R (and S-plus) Users

Although there are other comparison cheat sheets in this collection, this one lists some advanced features. As the title says, it is a comparison between R(and S-plus) and NumPy. It is very detailed for each family of operations. For example, the sorting section provides eight ways to sort an array. Some operations are not possible in both languages, so it is easy to find the right function. This is the only cheat sheet in the collection that provides detailed plots and graphs. Moreover, some advanced math and statistics were given, like differential equations and Fourier analysis.

  • Pros: Advanced-level, comparison-based (R vs. NumPy), detailed, plots and graphs
  • Cons: Confusing, not focused

Cheat Sheet 9: Scientific Python

This is not a NumPy specific sheet. It covers many Python data science topics, but also some Python basics. It is easily navigated through because of the contents given in the beginning. The NumPy section is comprehensive. It covers NumPy basics like the array properties and operations. Also, it contains an extensive list of math functions and linear algebra functions. Some of the useful linear algebra functions are finding inner and outer products and eigenvalues. Others are functions for rounding off and generating random variables.

  • Pros: NumPy + science, maths, linear algebra, beautiful design
  • Cons: No PDF download, no visuals

Cheat Sheet 10: Finxter NumPy

The Finxter cheat sheet is different from all the previously mentioned sheets because it’s visually the clearest. It gives a detailed description of each function and lists the examples along with the outcome. The good thing about the visible outcome is that looking at it can help if you’re unsure about the name of the function. Along with the cheat sheet, there is an accompanying video with further detailed examples and explanations.

  • Pros: Easy, simple, visually clean, and focused on the most important functions
  • Cons: No graphics

Bonus Cheat Sheet: From NumPy to xtensor

xtensor is a C++ library, similar to NumPy, made for numerical analysis. The cheat sheet provides a two-column view, where the first column is NumPy, and the second column contains the xtensor equivalents. The sheet focuses on array initialization, reshaping, and slicing functions. Further, it continues with array manipulation like transpose or rotation functions. There are a lot of tensor operations, but the sheet is missing the descriptions. So, it’s not always easily deducible what a certain function does.

  • Pros: Only xtensor sheet, simple, comparison-based
  • Cons: Too specific for most coders

Attribution

This article is contributed by Finxter user Milica Cvetkovic. Milica is also a writer on Medium — check out her profile.

Where to Go From Here?

A thorough understanding of the NumPy basics is an important part of any data scientist’s education. NumPy is at the heart of many advanced machine learning and data science libraries such as Pandas, TensorFlow, and Scikit-learn.

If you struggle with the NumPy library — fear not! Become a NumPy professional in no time with our new coding textbook “Coffee Break NumPy”. It’s not only a thorough introduction into the NumPy library that will increase your value to the marketplace. It’s also fun to go through the large collection of code puzzles in the book.

Related Articles:

Related Posts