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

Numpy Cheat Sheet

This document provides a cheat sheet on key NumPy functions and methods for importing and exporting data, creating arrays, inspecting array properties, copying and reshaping arrays, indexing and slicing, combining and splitting arrays, performing math operations on arrays, and calculating statistics. It includes shorthand references, examples, and descriptions for common NumPy tasks.

Uploaded by

Giova Rossi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
274 views

Numpy Cheat Sheet

This document provides a cheat sheet on key NumPy functions and methods for importing and exporting data, creating arrays, inspecting array properties, copying and reshaping arrays, indexing and slicing, combining and splitting arrays, performing math operations on arrays, and calculating statistics. It includes shorthand references, examples, and descriptions for common NumPy tasks.

Uploaded by

Giova Rossi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Data Science Cheat Sheet

NumPy

KEY IMPORTS
We’ll use shorthand in this cheat sheet Import these to start
arr - A numpy Array object import numpy as np

I M P O RT I N G/ E X P O RT I N G arr.T - Transposes arr (rows become columns and S C A L A R M AT H


np.loadtxt('file.txt') - From a text file vice versa) np.add(arr,1) - Add 1 to each array element
np.genfromtxt('file.csv',delimiter=',') arr.reshape(3,4) - Reshapes arr to 3 rows, 4 np.subtract(arr,2) - Subtract 2 from each array
- From a CSV file columns without changing data element
np.savetxt('file.txt',arr,delimiter=' ') arr.resize((5,6)) - Changes arr shape to 5x6 np.multiply(arr,3) - Multiply each array
- Writes to a text file and fills new values with 0 element by 3
np.savetxt('file.csv',arr,delimiter=',') np.divide(arr,4) - Divide each array element by
- Writes to a CSV file A D D I N G/ R E M OV I N G E L E M E N TS 4 (returns np.nan for division by zero)
np.append(arr,values) - Appends values to end np.power(arr,5) - Raise each array element to
C R E AT I N G A R R AYS of arr the 5th power
np.array([1,2,3]) - One dimensional array np.insert(arr,2,values) - Inserts values into
np.array([(1,2,3),(4,5,6)]) - Two dimensional arr before index 2 V E C TO R M AT H
array np.delete(arr,3,axis=0) - Deletes row on index np.add(arr1,arr2) - Elementwise add arr2 to
np.zeros(3) - 1D array of length 3 all values 0 3 of arr arr1
np.ones((3,4)) - 3x4 array with all values 1 np.delete(arr,4,axis=1) - Deletes column on np.subtract(arr1,arr2) - Elementwise subtract
np.eye(5) - 5x5 array of 0 with 1 on diagonal index 4 of arr arr2 from arr1
(Identity matrix) np.multiply(arr1,arr2) - Elementwise multiply
np.linspace(0,100,6) - Array of 6 evenly divided C O M B I N I N G/S P L I T T I N G arr1 by arr2
values from 0 to 100 np.concatenate((arr1,arr2),axis=0) - Adds np.divide(arr1,arr2) - Elementwise divide arr1
np.arange(0,10,3) - Array of values from 0 to less arr2 as rows to the end of arr1 by arr2
than 10 with step 3 (eg [0,3,6,9]) np.concatenate((arr1,arr2),axis=1) - Adds np.power(arr1,arr2) - Elementwise raise arr1
np.full((2,3),8) - 2x3 array with all values 8 arr2 as columns to end of arr1 raised to the power of arr2
np.random.rand(4,5) - 4x5 array of random floats np.split(arr,3) - Splits arr into 3 sub-arrays np.array_equal(arr1,arr2) - Returns True if the
between 0-1 np.hsplit(arr,5) - Splits arr horizontally on the arrays have the same elements and shape
np.random.rand(6,7)*100 - 6x7 array of random 5th index np.sqrt(arr) - Square root of each element in the
floats between 0-100 array
np.random.randint(5,size=(2,3)) - 2x3 array I N D E X I N G/S L I C I N G/S U B S E T T I N G np.sin(arr) - Sine of each element in the array
with random ints between 0-4 arr[5] - Returns the element at index 5 np.log(arr) - Natural log of each element in the
arr[2,5] - Returns the 2D array element on index array
I N S P E C T I N G P R O P E RT I E S [2][5] np.abs(arr) - Absolute value of each element in
arr.size - Returns number of elements in arr arr[1]=4 - Assigns array element on index 1 the the array
arr.shape - Returns dimensions of arr (rows, value 4 np.ceil(arr) - Rounds up to the nearest int
columns) arr[1,3]=10 - Assigns array element on index np.floor(arr) - Rounds down to the nearest int
arr.dtype - Returns type of elements in arr [1][3] the value 10 np.round(arr) - Rounds to the nearest int
arr.astype(dtype) - Convert arr elements to arr[0:3] - Returns the elements at indices 0,1,2
type dtype (On a 2D array: returns rows 0,1,2) STAT I ST I C S
arr.tolist() - Convert arr to a Python list arr[0:3,4] - Returns the elements on rows 0,1,2 np.mean(arr,axis=0) - Returns mean along
np.info(np.eye) - View documentation for at column 4 specific axis
np.eye arr[:2] - Returns the elements at indices 0,1 (On arr.sum() - Returns sum of arr
a 2D array: returns rows 0,1) arr.min() - Returns minimum value of arr
C O P Y I N G/S O RT I N G/ R E S H A P I N G arr[:,1] - Returns the elements at index 1 on all arr.max(axis=0) - Returns maximum value of
np.copy(arr) - Copies arr to new memory rows specific axis
arr.view(dtype) - Creates view of arr elements arr<5 - Returns an array with boolean values np.var(arr) - Returns the variance of array
with type dtype (arr1<3) & (arr2>5) - Returns an array with np.std(arr,axis=1) - Returns the standard
arr.sort() - Sorts arr boolean values deviation of specific axis
arr.sort(axis=0) - Sorts specific axis of arr ~arr - Inverts a boolean array arr.corrcoef() - Returns correlation coefficient
two_d_arr.flatten() - Flattens 2D array arr[arr<5] - Returns array elements smaller than 5 of array
two_d_arr to 1D

You might also like