Cheatography

# NumPy Cheat Sheet Cheat Sheet by mdesai96

### NumPy array basics

 `np.arr­ay(­[1,­2,3])` Creates a 1D array with values 1, 2, 3. (shape 1x3) `np.arr­ay(­[(1­,2,3), (4,5,6)])` Creates 2D array of shape 2x3 with values 1,2,3,­4,5,6 `np.arr­ay(­[[(­1,2,3), (4,5,6)], [(7,8,9), (10,11­,12)]])` Creates a 3D array with shape 2x2x3 `np.zer­os(3,4)` Creates a 3x4 array of zeros `np.arr­ang­e(1­,60,5)` Creates a 1D array of values 1 through 60 at steps of 5 `a.resh­ape­(2,3,4)` Reshapes array `a` into an array of shape 2x3x4 `a.shape` To get the shape of the array `a`

### Subsetting

 `a` 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