0% found this document useful (0 votes)
3 views

NumPy-Cheat-Sheet

This NumPy Cheat Sheet provides a concise reference for essential NumPy commands, focusing on array creation, manipulation, and analysis with practical examples from the NYC Taxis Dataset. It covers topics such as importing data, performing scalar and vector math, inspecting array properties, and computing statistics. The guide is designed to facilitate efficient data handling and analysis using NumPy's capabilities.

Uploaded by

vivak21iya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

NumPy-Cheat-Sheet

This NumPy Cheat Sheet provides a concise reference for essential NumPy commands, focusing on array creation, manipulation, and analysis with practical examples from the NYC Taxis Dataset. It covers topics such as importing data, performing scalar and vector math, inspecting array properties, and computing statistics. The guide is designed to facilitate efficient data handling and analysis using NumPy's capabilities.

Uploaded by

vivak21iya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

NumPy Cheat Sheet Table of Contents

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

manipulation, and analysis, using examples drawn from the


NYC Taxis Dataset. It covers critical topics such as importing Creating Arrays Vector Math
array, zeros, ones, eye, linspace, add, subtract, multiply, divide, power,
data from files, creating and reshaping arrays, and performing
arange, full, random array_equal, sqrt, sin, log, abs, ceil,
scalar and vector math.

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.

List of Lists, Selecting

Adding & Removing Elements Rows/columns, Vector Operations,


Statistics for 1D/2D ndarrays, Creating
Designed to be clear and actionable, this reference ensures that append, insert, delete an ndarray from CSV File, working with
you can quickly apply NumPy’s powerful array operations in Boolean Arrays, Assigning ndarray
Combining & Splitting Values
your data analysis workflow.
concatenate, split, hsplit

Indexing & Slicing


INDEXING, slicing, Conditional

Statements

NumPy Cheat Sheet Free resources at: dataquest.io/guide


Importing Data
Syntax for How to use Explained Syntax for How to use Explained

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

rand arr = np.random.rand(4, 5)


4x5 array of random floats

np.savetxt('file.csv', arr, delimiter=',') Writes an array to a .csv file between 0 and 1

arr = np.random.rand(6, 7) * 100


6x7 array of random

floats between 0-100


Creating Arrays randint arr = np.random.randint(5, size=(2, 3))
2x3 array with random
integers between 0 and 4
Syntax for How to use Explained

array arr = np.array([1, 2, 3]) Create a 1D array

arr = np.array([(1, 2, 3), (4, 5, 6)]) Create a 2D array

zeros arr = np.zeros(3) 1D array of length 3; all


values set to 0

ones arr = np.ones((3, 4)) 3x4 array with all values


set to 1
arr = np.eye(5)
5x5 array of 0 with 1 on
eye
diagonal (identity matrix)

NumPy Cheat Sheet Free resources at: dataquest.io/guide


Inspecting Properties Copying, Sorting, & Reshaping
Syntax for How to use Explained Syntax for How to use Explained

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

Returns type of elements in T arr.T Transposes arr (rows become


dtype arr.dtype
arr columns and vice versa)

Reshapes arr to 3 rows, 4

reshape arr.reshape(3, 4) columns without changing


data

resize arr.resize((5, 6)) Changes arr shape to 5x6

and fills new values with 0

NumPy Cheat Sheet Free resources at: dataquest.io/guide


Adding & Removing Elements Indexing & Slicing
Syntax for How to use Explained Syntax for How to use Explained

append Appends values to end


INDEXING Returns the element at
np.append(arr, values) arr[5]
of arr index 5

insert np.insert(arr, 2, values)


Inserts values into arr Returns the 2D array
before index 2 arr[2, 5] element on index [2][5]
delete np.delete(arr, 3, axis=0) Deletes row on index 3 of
arr
arr[1] = 4
Assigns array element on
Removes the 5th column index 1 the value 4
np.delete(arr, 4, axis=1)
from arr
Assigns array element on
arr[1, 3] = 10
index [1][3] the value 10
Combining & Splitting Returns the elements at
slicing arr[0:3] indices 0, 1, 2
Syntax for How to use Explained
Adds arr2 as rows to the arr[0:3, 4]
Returns the elements on rows
concatenate np.concatenate((arr1, arr2), axis=0)
end of arr1 0, 1, 2 in column index 4

Adds arr2 as columns to Returns the elements at


np.concatenate((arr1, arr2), axis=1) arr[:2]
end of arr1 indices 0, 1

split np.split(arr, 3) Splits arr into 3 sub-arrays


arr[:, 1]
Returns column index 1, all
rows
hsplit np.hsplit(arr, 5)
Splits arr horizontally on
the index 5 Conditional
arr < 5
Returns an array of
Statements boolean values

NumPy Cheat Sheet Free resources at: dataquest.io/guide


I ndexing & Slicing Vector Math
Syntax for How to use Explained Syntax for How to use Explained

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

To be True , at least one divide np.divide(arr1, arr2)


Elementwise divide arr1
(arr1 < 3) | (arr2 > 5)
must be True by arr2

power np.power(arr1, arr2)


Elementwise, raise arr1
to the power of arr2
Scalar Math
Returns True if the arrays
array_equal np.array_equal(arr1, arr2)
have the same elements
Syntax for How to use Explained
and shape
add np.add(arr, 1) Add 1 to each array element Square root of each
sqrt np.sqrt(arr)
element in the array
subtract np.subtract(arr, 2) Subtract 2 from each array

element np.sin(arr)
Sine of each element in the
sin
array
multiply np.multiply(arr, 3) Multiply each array

element by 3 log np.log(arr)


N atural log of each
element in the array
D ivide each array element by
divide np.divide(arr, 4)
4 (returns np.nan for Absolute value of each
abs np.abs(arr)
division by zero) element in the array
power np.power(arr, 5) Raise each array element to
Rounds up each element to
the power of 5 ceil np.ceil(arr)
the nearest integer

N umPy Cheat Sheet Free resources at: dataquest.io/guide


Vector Math Working with Data
Syntax for How to use Explained Syntax for How to use Explained

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])

array_2d = np.array([[1, 2, 3], [4, 5, 6]])


round np.round(arr) Rounds each element to
the nearest integer
Converting a
import csv
Convert a list of lists into a 2D
List of Lists f = open("nyc_taxis.csv", "r")
ndarray
Statistics taxi_list = list(csv.reader(f))

taxi = np.array(taxi_list)
Syntax for How to use Explained

mean np.mean(arr, axis=0) Returns mean of arr along


Selecting
second_row = taxi[1]
Select the second row in
specified axis
Rows taxi
sum arr.sum() Returns the sum of elements Select all rows from the
all_but_first_row = taxi[1:]
in arr second row onward in taxi

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

NumPy Cheat Sheet Free resources at: dataquest.io/guide


Working with Data
Syntax for How to use Explained Syntax for How to use Explained

Element-wise addition of two


Vector
vector_a + vector_b Creating an
import numpy as np
Load data from the
ndarray objects
Operations ndarray fro m
taxi = np.genfromtxt('nyc_taxis.csv',

nyc_taxis.csv file into an


Element-wise subtraction of CSV File delimiter=',', skip_header=1) ndarray, skipping the
vector_a - vector_b header row
two ndarray objects

* 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

Return the minimum value of from an ndarray


Statistics for
array_1d.min() a[filter] # returns [2, 4]
array_1d
1D ndarrays

Return the maximum value of


array_1d.max() p
ti _amount = taxi [:, 12]
Use Boolean filtering to
array_1d
return rows with
tip_bool = tip_amount > 50

tip_amount > 50 and


Calculate the average of top_tips = taxi[tip_bool, 5:14]
array_1d.mean() columns 6 to 14
values in array_1d

Assigning

Assign values to specific


Calculate the sum of the taxi[1066, 5] = 1

elements, a column, and a


array_1d.sum()
values in array_1d ndarray
taxi[:, 0] = 16

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

NumPy Cheat Sheet Free resources at: dataquest.io/guide

You might also like