1 NumPy Cheat Sheet - Beginner To Advanced PDF
1 NumPy Cheat Sheet - Beginner To Advanced PDF
InCheat
HTML thisSheet
NumpyCSSCheat sheetJS for
Cheat Sheet CheatData
Sheet Analysis, we’ve
Bootstrap Cheat Sheetcovered
jQuery the
Cheatbasics
Sheet to
Angular C
advanced functions of Numpy including creating arrays, Inspecting
properties as well as file handling, Manipulation of arrays, Mathematics
Operations in Array and more with proper examples and output. By the end
of this Numpy cheat sheet, you will gain a fundamental comprehension of
NumPy and its application in Python for data analysis.
What is NumPy?
NumPy was initially created by Travis Oliphant in 2005 as an open-source
project. NumPy is a powerful Python library that provides support for large,
multi-dimensional arrays and matrices, along with a wide collection of
mathematical functions to operate on these arrays. It is an essential tool for
scientific computing and data analysis in Python.
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 1/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Table of Content:
Creating Arrays Commands
Initial Placeholders
Inspecting Properties
Saving and Loading File
Sorting Array
NumPy Array Manipulation
Combining and Splitting Commands
Indexing, Slicing and Subsetting
Copying and Viewing Array
NumPy Array Mathematics
Benefits of Using NumPy Cheat Sheet
Applications of NumPy
Feature of NumPy
NumPy Cheat Sheet – FAQs
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 2/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Python3
Output:
[1 2 3 4]
[5 6 7 8]
[0. 1. 2. 3. 4. 5. 6. 7.]
Numpy multi-dimensional arrays are stored in a tabular form, i.e., in the form
of rows and columns.
Using
np.empty([4, 3], dtype=int)
empty()
Python3
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 3/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Output:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
2. Initial Placeholders
Example 1: For 1-Dimensional NumPy Arrays
random.rand() np.random.rand(5)
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 4/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Python3
Output:
[1 2 3 4 5 6 7 8 9]
[ 1. 5.5 10. ]
[0 0 0 0 0]
[1 1 1 1 1]
[0.31447226 0.89090771 0.45908938 0.92006507 0.37757036]
[4 3 2 3 1 2 4 1 4 2]
Initial placeholders for Numpy two dimension arrays can be created by using
various NumPy functions.
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 5/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
eye() np.eye(4)
Python3
Output:
[[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]]
[[1 1 1]
[1 1 1]
[1 1 1]
[1 1 1]]
[[67 67]
[67 67]]
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
3. Inspecting Properties
NumPy arrays possess some basic properties that can be used to get
information about the array such as the size, length, shape, and datatype of
the array. Numpy arrays can also be converted to a list and be change their
datatype.
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 6/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Size arr.size
Length len(arr)
Shape arr.shape
Datatype arr.dtype
Python3
Output:
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 7/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Size: 4
len: 4
Shape: (4,)
Datatype: int64
[1. 2. 3. 4.]
Datatype: float64
List: [1.0, 2.0, 3.0, 4.0]
<class 'list'>
Python3
Output:
Size: 12
Length: 3
Shape: (3, 4)
Datatype: int64
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 8/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
[[ 1. 2. 3. 4.]
[ 5. 6. 7. 8.]
[ 9. 10. 11. 12.]]
float64
List: [[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 10.0, 11.0,
12.0]]
<class 'list'>
The np.info() function is used to get information about any Numpy function,
class, or module along with its parameters, return values, and any other
information.
Python3
import sys
print(np.info(np.add, output=sys.stdout))
Output:
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 9/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Numpy arrays can be stored on the disk using the save() function and loaded
using the load() function.
Python3
Output:
[0 1 2 3 4]
Numpy arrays can be stored on a text file using the savetxt() function.
Python3
x = np.arange(0, 10, 1)
print(x)
Output:
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 10/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
[0 1 2 3 4 5 6 7 8 9]
The “myfile.txt” has the following content which is loaded using the
loadtxt() function.
0 1 2
3 4 5
Python3
d = np.loadtxt("myfile.txt")
print(d)
Output:
[[0. 1. 2.]
[3. 4. 5.]]
We can also load a CSV file in Python using Numpy using another method
called genfromtxt() function. The ‘myfilecsv’ has the following content:
1,2,3
4,5,6
Python3
Output:
[[1. 2. 3.]
[4. 5. 6.]]
5. Sorting Array
Numpy arrays can be sorted using the sort() method based on their axis.
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 11/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Python3
Output:
Python3
Output:
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 12/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
col = np.arange(5,
Append to 2D array column wise 11).reshape(1, 6)
row = np.array([1,
Append to 2D array row-wise 2]).reshape(2, 1)
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 13/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Numpy arrays can be manipulated by appending the new values at the end
of the array using the append() function
Python3
Output:
Python3
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 14/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Output:
Original Array
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
Array after appending the values column wise
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]
[ 5 6 7 8 9 10]]
Array after appending the values row wise
[[ 1 2 3 4 5 6 1]
[ 7 8 9 10 11 12 2]]
Python3
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 15/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Output:
1D arr: [1 2 3 4]
Shape: (4,)
Array after insertion: [1 9 2 3 4]
Shape: (5,)
Elements from a NumPy array can be removed using the delete() function.
Python3
Output:
Original arr: [1 2 3 4]
Shape : (4,)
deleteing the value at index 2 from array:
[1 2 4]
Shape : (3,)
Reshaping Array
NumPy arrays can be reshaped, which means they can be converted from
one dimension array to an N-dimension array and vice-versa using the
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 16/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
reshape() method. The reshape() function does not change the original array.
Python3
# printing array
print("Array: " + str(array))
Output:
Array: [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
First Reshaped Array:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
Second Reshaped Array:
[[ 1 2 3 4 5 6 7 8]
[ 9 10 11 12 13 14 15 16]]
Python3
# printing array
print(" 2-D Array:")
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 17/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
print(arr)
Output:
2-D Array:
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
Reshaped 1-D Array:
[ 1 2 3 4 5 6 7 8 9 10 11 12]
Resizing an Array
Numpy arrays can be resized using the resize() function. It returns nothing
but changes the original array.
Python3
Output:
[[1 2 3 4]
[5 6 0 0]
[0 0 0 0]]
Python3
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 18/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
print(arr.flatten())
Output:
[1 2 3 4 5 6 7 8]
Transpose
Python3
# before transpose
print(gfg, end ='\n\n')
# after transpose
print(gfg.transpose(1, 0))
Output:
[[1 2]
[4 5]
[7 8]]
[[1 4 7]
[2 5 8]]
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 19/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Combining two arrays into a single Numpy array can be done using
concatenate() method.
Python3
# combining on axis 0
gfg = np.concatenate((arr1, arr2), axis = 0)
print(gfg)
# combining on axis 1
gfg = np.concatenate((arr1, arr2), axis = 1)
print("\n", gfg)
Output:
[[2 4]
[6 8]
[3 5]
[7 9]]
[[2 4 3 5]
[6 8 7 9]]
[2 4 6 8 3 5 7 9]
[[[2 4]
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 20/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
[3 5]]
[[6 8]
[7 9]]]
Python3
Output:
[[0 1 2]
[3 4 5]
[6 7 8]]
Splitted array in horizontal form:
[array([[0],
[3],
[6]]), array([[1],
[4],
[7]]), array([[2],
[5],
[8]])]
Splitted array in vertical form:
[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
Python3
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 21/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Output:
[[0 1 2]
[3 4 5]
[6 7 8]]
Splitted array in horizontally to form: [array([[0],
[3],
[6]]), array([[1],
[4],
[7]]), array([[2],
[5],
[8]])]
The vsplit() splits the Numpy array into multiple vertical arrays.
Python3
Output:
Sclicing arr[-2:7:1]
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 22/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Python3
Output:
[1 2 3 4 7]
Elements are: [2 4 3]
Python3
print(arr)
# a[start:stop:step]
print("a[-2:7:1] = ",arr[-2:7:1])
print("a[1:] = ",arr[1:])
Output:
[1 2 3 4 7]
a[-2:7:1] = [4 7]
a[1:] = [2 3 4 7]
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 23/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Python3
# Integer Indexing
a = np.array([[1 ,2 ],[3 ,4 ],[5 ,6 ]])
print(a[[0 ,1 ,2 ],[0 ,0 ,1]])
# Boolean Indexing
a = np.array([10, 40, 80, 50, 100])
print(a[a>50])
Output:
[1 3 6]
[ 80 100]
Copying Array
Shallow copy
Python3
# updating nc
nc[0]= 12
Output:
Deep Copy
Python3
Output:
id of arr: 139656514596912
id of c: 139656514596432
original array: [12 2 3 4]
copy: [1 2 3 4]
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 25/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Viewing Array
The view is also known as a shallow copy which is just a view of the original
array. It has a separate id but any changes made to the original will also
reflect on the view.
Python3
# Creating a view of a
# NumPy array
# creating view
v = arr.view()
Output:
id of arr: 139656514598640
id of v: 139656514599216
original array: [12 2 3 4]
view: [12 2 3 4]
Arithmetic Operations
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 26/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Adds
np.add(a, b)
elements of 2 Array
Substracts
np.subtract(a, b)
elements of 2 Array
Multiply
np.multiply(a, b)
elements of 2 Array
Divide
np.divide(a, b)
elements of 2 Array
Modulo
np.mod(a, b)
of elements of 2 Array
Remainder
np.remainder(a,b)
of elements of 2 Array
Power
np.power(a, b)
of elements of 2 Array
Exponant
np.exp(b)
value of elements of 2 Array
Python3
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 27/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
# Performing Exponentiation
print("Exponentiation:", np.exp(b))
Output:
Addition: [ 7 77 23 130]
Subtraction: [ 3 67 3 70]
Multiplication: [ 10 360 130 3000]
Division: [ 2.5 14.4 1.3 3.33333333]
Mod: [ 1 2 3 10]
Remainder: [ 1 2 3 10]
Power: [ 25 1934917632 137858491849
1152921504606846976]
Exponentiation: [7.38905610e+00 1.48413159e+02 2.20264658e+04
1.06864746e+13]
Comparison
Numpy array elements can be compared with another array using the
array_equal() function.
Python3
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 28/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
print(equal_arrays)
Output:
True
Vector Math
Numpy arrays can also determine square root, log, absolute, sine, ceil, floor,
and round values.
Square root
np.sqrt(arr)
of each element
Log
np.log(arr)
value of each element
Absolute
np.absolute(arr)
value of each element
Sine
np.sin(arr)
value of each element
Ceil
np.ceil(arr)
value of each element
Floor
np.floor(arr)
value of each element
Round
np.round_(arr)
value of each element
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 29/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Python3
Output:
Statistic
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 30/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
Statistic Example
Mean np.mean(arr)
Median np.median(arr)
Maximum np.max(arr)
Python3
# 1D array
arr = [20, 2, 7, 1, 34]
# mean
print("mean of arr:", np.mean(arr))
# median
print("median of arr:", np.median(arr))
# sum
print("Sum of arr(uint8):", np.sum(arr, dtype = np.uint8))
print("Sum of arr(float32):", np.sum(arr, dtype = np.float32))
# var
print("var of arr:", np.var(arr))
print("var of arr(float32):", np.var(arr, dtype = np.float32))
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 31/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
# standard deviation
print("std of arr:", np.std(arr))
print ("More precision with float32", np.std(arr, dtype = np.float32))
Output:
corrcoef
Python3
print(rslt)
Output:
[[1. 1.]
[1. 1.]]
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 32/38
10/23/24, 12:46 AM NumPy Cheat Sheet: Beginner to Advanced (PDF)
manipulation.
2. Mathematical Functions: The library includes an extensive collection of
mathematical functions for operations like trigonometry, statistics, linear
algebra, and more.
3. Broadcasting: NumPy allows broadcasting, which enables element-wise
operations on arrays of different shapes, reducing the need for explicit
loops.
4. Interoperability: It integrates with other libraries like Pandas, SciPy, and
Matplotlib, improving its functionality for data analysis and visualization.
5. Memory Optimization: NumPy optimizes memory usage, making it ideal
for working with large datasets without consuming excessive RAM.
6. Multidimensional Arrays: The library supports multidimensional arrays,
enabling easy manipulation of complex data structures.
7. Open-source and Community Support: NumPy is open-source, and its
active community provides regular updates, bug fixes, and additional
functionalities.
Applications of NumPy
The various applications of Numpy other than data analysis are given below
Scientific Computing
Data Analysis and Preprocessing
Image Processing
Machine Learning
Signal Processing
Feature of NumPy
Here are some features of Numpy that why Numpy is famous for data
analysis and scientific computing
Conclusion
https://www.geeksforgeeks.org/numpy-cheat-sheet/ 33/38