NumPy-Cheat-Sheet
NumPy-Cheat-Sheet
This cheat sheet offers a quick and practical reference for Importing Data Scalar Math
essential NumPy commands, focusing on array creation, IMPORT, loadtxt, genfromtxt, savetxt add, subtract, multiply, divide, power
floor, round
Inspecting Properties
You’ll also find easy-to-follow instructions on inspecting array size, shape, dtype, astype, tolist, Statistics
properties, combining and splitting arrays, Boolean filtering, INFO mean, sum, min, max, var, std,
corrcoef
and computing statistics like mean, variance, and standard
Copying, Sorting, & Reshaping
deviation. Whether you’re analyzing 1D or 2D arrays, this cheat
copy, view, sort, flatten, T, reshape, Working with Data
sheet helps you leverage NumPy’s capabilities for efficient data resize Creating ndarrays, Converting a
handling.
Statements
import numpy as np
Imports NumPy using its linspace arr = np.linspace(0, 100, 6)
Array of 6 evenly divided
IMPORT
standard alias, np values from 0 to 100 ([0,
20, 40, 60, 80, 100])
loadtxt np.loadtxt('file.txt') Create an array from a .txt
Array of values from 0 to less
file arange arr = np.arange(0, 10, 3)
than 10 with step 3 ([0, 3,
genfromtxt np.genfromtxt('file.csv', delimiter=',') Create an array from a .csv 6, 9])
file
full arr = np.full((2, 3), 8) 2x3 array with all values set
savetxt np.savetxt('file.txt', arr, delimiter=' ') Writes an array to a .txt file to 8
astype arr.astype(dtype)
Convert arr elements to
copy np.copy(arr) Copies arr to new memory
type dtype
tolist arr.tolist() Convert arr to a Python list view arr.view(dtype) Creates view of arr
elements with type dtype
info
View documentation for
sort arr.sort() Sorts arr
np.info(np.eye)
np.eye
size arr.size Returns number of elements sort arr.sort(axis=0) Sorts specific axis of arr
in arr
Returns dimensions of arr flatten two_d_arr.flatten() Flattens 2D array two_d_arr
shape arr.shape
(rows, columns) to 1D
Conditional
(arr1 < 3) & (arr2 > 5)
To be True , both must be add E lementwise add arr1 to
np.add(arr1, arr2)
Statements True a rr2
~arr Inverts a boolean array subtract np.subtract(arr1, arr2) E lementwise subtract arr2
from arr1
Returns array elements multiply np.multiply(arr1, arr2) Elementwise multiply
arr[arr < 5]
less than 5 arr1 by arr2
element np.sin(arr)
Sine of each element in the
sin
array
multiply np.multiply(arr, 3) Multiply each array
floor np.floor(arr)
Rounds down each element Creating
import numpy as np
Create a 1D or 2D ndarray
to the nearest integer ndarrays array_1d = np.array([1, 2, 3, 4, 5])
taxi = np.array(taxi_list)
Syntax for How to use Explained
min arr.min() Returns minimum value of Select the element from the
fifth_row_second_column = taxi[4, 1]
arr fifth row and second column
in taxi
max arr.max(axis=0) Returns maximum value of
Selecting
Select all values from the
arr along specified axis second_column = taxi[:, 1]
Columns second column in taxi
var np.var(arr) Returns the variance of arr second_third_columns = taxi[:, 1:3]
Select the second and third
cols = [1, 3, 5]
columns, then the second,
Returns the standard deviation second_fourth_sixth_columns = taxi[:, cols] fourth, and sixth columns in
std np.std(arr, axis=1) taxi
of arr along specified axis
twod_slice = taxi[1:4, :3] Select a slice of rows 2 to 4
corrcoef arr.corrcoef()
Returns correlation and columns 1 to 3 in taxi
coefficient of arr
* p
Element-wise multi lication
vector_a vector_b
of two ndarray objects
Working with
p
n .arra ( y [2, 4, 6, 8]) < 5 y
Create a Boolean arra for
Boolean
elements less than 5
Element-wise division of two Arrays
vector_a / vector_b
ndarray objects p y [2, 4, 6, 8])
a = n .arra (
Use Boolean filtering to
return elements less than 5
filter = a < 5
Assigning
slice in taxi
Values taxi[550:552, 7] = taxi[:, 7].mean()
Return the maximum value for
Statistics for
array_2d.max()
2D ndarrays
the entire array_2d
[
taxi taxi [:, 5] == 2, 15] = 1
Use Boolean indexing to
assign a value of 1 in column
Return the maximum value in
array_2d.max(axis=1) # returns a 1D ndarray index 15 to rows where the
each row in array_2d
6th column equals 2
Return the maximum value in
array_2d.max(axis=0) # returns a 1D ndarray
each column in array_2d