0% found this document useful (0 votes)
29 views1 page

Numpy Cheatsheet

This document provides a summary of NumPy functions for generating, transforming, inspecting, slicing, and performing mathematical operations on NumPy arrays. It lists functions for creating arrays, accessing and modifying array elements, calculating statistics, linear algebra, and other common array operations. The cheatsheet is intended to serve as a quick reference guide to the main NumPy functions and their usage.

Uploaded by

Chris Sej
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)
29 views1 page

Numpy Cheatsheet

This document provides a summary of NumPy functions for generating, transforming, inspecting, slicing, and performing mathematical operations on NumPy arrays. It lists functions for creating arrays, accessing and modifying array elements, calculating statistics, linear algebra, and other common array operations. The cheatsheet is intended to serve as a quick reference guide to the main NumPy functions and their usage.

Uploaded by

Chris Sej
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

A//B, np.

floor_divide(A,B)
A%B, [Link](A,B) # Fancy indexing
NumPy Cheatsheet A**B,
A==B,
[Link](A,B)
[Link](A,B)
A[:, [0,1,0,0,1]]
A[[0,1,0],[2,3,3]]
# use indices on axis
# 1D array A[0,2],A[1,3],A[0,3]
©2022 [Link] A!=B, np.not_equal(A,B)
A>B, [Link](A,B) # Pick elements from A if cond. is True, B otherwise
A>=B, np.greater_equal(A,B) [Link](A>B, A, B)
import numpy as np
# print help for a function A<B, [Link](A,B)
A<=B, np.less_equal(A,B) # Select based on multiple conditions with default
print([Link]([Link])) # All three below are equivalent
# Power and log [Link](A, 0.1, 0.9)
Generating array [Link](A), [Link](2, A), [Link](A, 3) [Link]([A<0.1, A>0.9], [0.1, 0.9], A)
[Link]([1, 1.5, 2]) [Link](A), np.log10(A), np.log2(A) [Link](A>0.1, [Link](A<0.9, A, 0.9), 0.1)
[Link]([1, 1.5, 2]) # Square, square root, cube root
[Link](A), [Link](A), [Link](A) Transforming arrays
[Link](0,1,5) # Trigonometric functions Z = [Link](A) # clone array
# »> array([0., 0.25, 0.5, 0.75, 1.]) [Link](A), [Link](A), [Link](A) Z = [Link](A) # 1D array of unique elements
[Link](0,1,0.25) [Link](A), [Link](A), [Link](A)
# »> array([0., 0.25, 0.5, 0.75]) # absolute value, sign Z = [Link](A) # sort on each row
[Link](A), [Link](A) Z = [Link](A, axis=0) # sort on each columns
# Create array of shape (3,4) # NaN and infinity [Link](axis=0) # in-place sort
[Link]((3,4)) # all elements are 0 [Link](A) Z = [Link](axis=0) # return indices that sorts
[Link]((3,4)) # all elements are 1 [Link](A), [Link](A), [Link](A)
[Link]((3,4), 2) # all elements are 2 # ceiling, floor, and rounding to 2 d.p. # Transpose matrix: all below are equivalent
[Link](A), [Link](A), [Link](A, 2) A.T, [Link](0,1), [Link](A)
[Link](3) # 3x3 identity matrix # clip to between lower and upper bound # flatten array to 1D
[Link](A, 0.1, 0.9) [Link](), [Link](-1)
# make random arrays # use elements from A to fill an array of shape (3,4)
[Link]((3,4)) # between 0 and 1 Aggregate operations [Link](A, (3,4))
[Link](10, size=(3,4)) # between 0 and 9 # All can take argument ”axis=0” to aggregate along # add a new dimension and becomes axis=2
[Link](3,4) # normal distrib. # an axis. Otherwise aggregate on flattened array np.expand_dims(A, 2)
[Link]((3,4)) # uninitialized # padding for width=2 in each dimension
# Mean, standard deviation, median [Link](A, 2)
a = [Link]([1,2]) [Link](), [Link](), [Link](A)
b = [Link]([3,4,5]) [Link](axis=0), [Link](axis=0), [Link](A, axis=0) # concatenate arrays vertically
[Link](a,b) # Correlation matrix of two vector, or rows of a matrix [Link]((A,B)), np.r_[A,B]
# » [array([[1, 2],[1, 2],[1, 2]]), [Link](u, v), [Link](A) [Link](A, B, axis=0)
# array([[3, 3],[4, 4],[5, 5]])] [Link]((A,B), axis=0)
# Aggregate to boolean value, non-zero is True # concatenate horizontally
Inspecting array [Link](), [Link]() [Link]((A,B)), np.c_[A,B]
A = [Link](3,4) # Numerical sum, product, min, max
[Link] # shape = (3,4) [Link](), [Link]() # insert value to/overwrite/delete column 2
[Link] # num of elements = 12 [Link](), [Link]() [Link](A, 2, 0.2, axis=1)
len(A) # axis 0 length = 3 # argmin/argmax: Return index of flattened array, A[:, 2] = 0.25
[Link] # num dimension = 2 # or indices on the axis [Link](A, 2, axis=1)
[Link] # dtype('float64') [Link](), [Link]()
[Link](axis=0), [Link](axis=0) # split array into 2 equal-shape subarray
[Link](int) # cast array to integers # cumulative sum/product [Link](A, 2), [Link](A, 2)
[Link](), [Link]()
Vector dot-product/Matrix multiplication Linear algebra
Slicing # pseudo-inverse, inverse, determinant, rank, norm
A @ B # all three are identical
[Link](A,B) A[2] # select one ”row” [Link](A), [Link](M)
[Link](A,B) A[2:3] # subarray on axis 0 [Link](M), [Link].matrix_rank(A)
A[2:3, 1:3] # subarray on multiple axes [Link](A), [Link](A, ord=1)
A[2:3, ..., 1:3] # subarray on first & last axes
Element-wise operations A[2, 1:3], A[2][1:3] # these two are equivalent L = [Link](M) # lower triangular
A+B, [Link](A,B) U, diag, V = [Link](A) # compact SVD
A-B, [Link](A,B) # Boolean indexing w, V = [Link](M) # eigenvector=columns in V
A*B, [Link](A,B) A[(B>0.1) & (B<0.9)] # return 1D array
A/B, [Link](A,B) A[:, [Link](axis=1)>1] # select ”columns” v1.0.20220405

You might also like