Show Menu

NumPy Cheat Sheet Cheat Sheet by

python     arrays     numpy

NumPy array basics

np.ar­ray­([1­,2,3])
Creates a 1D array with values 1, 2, 3. (shape 1x3)
np.ar­ray­([(­1,2,3), (4,5,6)])
Creates 2D array of shape 2x3 with values 1,2,3,­4,5,6
np.ar­ray­([[­(1,­2,3), (4,5,6)], [(7,8,9), (10,11­,12­)]])
Creates a 3D array with shape 2x2x3
np.ze­ros­(3,4)
Creates a 3x4 array of zeros
np.ar­ran­ge(­1,6­0,5)
Creates a 1D array of values 1 through 60 at steps of 5
a.res­hap­e(2­,3,4)
Reshapes array a into an array of shape 2x3x4
a.shape
To get the shape of the array a

Subsetting

a[0]
Gets the 0th element in 1D array a
b[0,0]
Gets the 0th element in 2D array b
c[0,0,0]
Gets the 0th element in 3D array c
c[9,0,0]
Gets the element which is the 0th element in the 0th row in the 9th depth

Slicing arrays

1D arrays
a[:]
Selects everything
a[2:5]
Selects the 2nd through the 4th rows (does not include the 5th row)
2D arrays
b[:,:]
Selects all rows and all columns
b[:,0]
Selects all rows, and the zeroth column
b[0,:]
Selects the zeroth row, and all columns in that row
b[0:2,:]
Selects the zeroth and first row, but NOT the second. It includes all the columns in that row
b[0:2­,0:2]
Selects the zeroth and first row, and the zeroth and first column
3D arrays
c[:,:,:]
Selects all rows and columns on all depths
c[0,:,:]
Selects the everything in the first depth
c[:,0,:]
Selects the first row of each depth
c[:,:,0]
Selects the first column of each depth

Download the NumPy Cheat Sheet Cheat Sheet

1 Page
//media.cheatography.com/storage/thumb/mdesai96_numpy-cheat-sheet.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.