100% found this document useful (1 vote)
69 views1 page

Numpy Basics Cheatsheet 1.0

Numpy Basics Cheatsheet provides concise summaries of key Numpy functions and operations for importing and exporting data, creating arrays, inspecting array properties, copying/sorting/reshaping arrays, and performing element-wise math operations on arrays. It covers importing Numpy, loading data from files, creating arrays of various types, viewing array properties like shape and size, and manipulating arrays through operations like concatenation, splitting, addition, subtraction and more.

Uploaded by

Akshay Yawale
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
100% found this document useful (1 vote)
69 views1 page

Numpy Basics Cheatsheet 1.0

Numpy Basics Cheatsheet provides concise summaries of key Numpy functions and operations for importing and exporting data, creating arrays, inspecting array properties, copying/sorting/reshaping arrays, and performing element-wise math operations on arrays. It covers importing Numpy, loading data from files, creating arrays of various types, viewing array properties like shape and size, and manipulating arrays through operations like concatenation, splitting, addition, subtraction and more.

Uploaded by

Akshay Yawale
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

Numpy Basics Cheatsheet 1.

0
Import Numpy Copying/sorting/reshaping Adding/removing Elements

import numpy as np Copies arr to new memory np.copy(arr) Returns the elements at indices 0,1,2 (On a 2D arr[0:3]
Creates view of arr elements with type arr.view(dtype) array: returns rows 0,1,2)
Shorthand used: arr | A NumPy Array object Sorts arr arr.sort() Returns the elements on rows 0,1,2 at column 4 arr[0:3,4]
Sorts specific axis of arr arr.sort(axis=0) Returns the elements at indices 0,1 (On a 2D arr[:2]
Importing/Exporting Data Flattens 2D array two_d_arr to 1D two_d_arr.flatten() array: returns rows 0,1)
Transposes arr (rows become columns and vice arr.T Returns the elements at index 1 on all rows arr[:,1]
From a text file np.loadtxt('file.txt') versa) Returns an array with boolean values arr<5
From a CSV file np.genfromtxt('file.csv',delimiter=',')
Reshapes arr to 3 rows, 4 columns without arr.reshape(3,4) Returns an array with boolean values (arr1<3) & (arr2>5)
Writes to a text file np.savetxt('file.txt',arr,delimiter=' ')
Writes to a CSV file np.savetxt('file.csv',arr,delimiter=',') changing data Inverts a boolean array ~arr
Changes arr shape to 5x6 and fills new values arr.resize((5,6)) Returns array elements smaller than 5 arr[arr<5]
Creating Arrays with 0
Scalar Math
One dimensional array np.array([1,2,3]) Adding/removing Elements
Two dimensional array np.array([(1,2,3),(4,5,6)]) Add 1 to each array element np.add(arr,1)
1D array of length 3 all values 0 np.zeros(3) Appends values to end of arr np.append(arr,values)
Subtract 2 from each array element np.subtract(arr,2)
3x4 array with all values 1 np.ones((3,4)) Inserts values into arr before index 2 np.insert(arr,2,values)
5x5 array of 0 with 1 on diagonal np.eye(5) Multiply each array element by 3 np.multiply(arr,3)
Deletes row on index 3 of arr np.delete(arr,3,axis=0)
(Identity matrix) Divide each array element by 4 (returns np.nan for np.divide(arr,4)
Deletes column on index 4 of arr np.delete(arr,4,axis=1)
Array of 6 evenly divided values np.linspace(0,100,6) division by zero)
from 0 to 100 Combining/Splitting Raise each array element to the 5th power np.power(arr,5)
Array of values from 0 to less than 10 with np.arange(0,10,3)
step 3 (eg [0,3,6,9]) Statistics
2x3 array with all values 8 np.full((2,3),8) Adds arr2 as rows to the end of arr1 np.concatenate((arr1,arr2),axis=0)
Inspecting
4x5 array ofProperties
random floats between 0–1 np.random.rand(4,5) Adds arr2 as columns to end of arr1 np.concatenate((arr1,arr2),axis=1)
6x7 array of random floats between 0–100 np.random.rand(6,7)*100 np.split(arr,3) Returns mean along specific axis np.mean(arr,axis=0)
Splits arr into 3 sub-arrays
2x3 array with random ints between 0–4 np.random.randint(5,size=(2,3)) np.hsplit(arr,5) Returns sum of arr arr.sum()
Splits arr horizontally on the
Returns minimum value of arr arr.min()
5th index
Inspecting Properties Returns maximum value of specific axis arr.max(axis=0)
Returns number of elements in arr arr.size Returns the variance of array np.var(arr)
Adding/removing Elements
Returns dimensions of arr (rows,columns) arr.shape Returns the standard deviation of specific axis np.std(arr,axis=1)
Returns type of elements in arr arr.dtype Returns the element at index 5 arr[5] Returns correlation coefficient of array arr.corrcoef()
Convert arr elements to type arr.astype(dtype) Returns the 2D array element on index [2][5] arr[2,5] Elementwise add arr2 to arr1 np.add(arr1,arr2)
Convert arr to a Python dtypearr.tolist() Assigns array element on index 1 the value 4 arr[1]=4 Elementwise subtract arr2 from arr1 np.subtract(arr1,arr2)
View documentation for np.eye np.info(np.eye) Assigns array element on index [1][3] the value 10 arr[1,3]=10 Elementwise multiply arr1 by arr2 np.multiply(arr1,arr2)

www.insaid.co

You might also like