Numpy Cheatsheet

Also check the numpy quickstart tutorial!

Array

Action Command Result
Create Array a=np.array([1,2,3]) [1,2,3]
Create multi-dim Array b=np.array([[0.9, 0.8, 7.0],[0.3, 0.11, 7.0]]) [[0.9 0.8 7. ]
[0.3 0.11 7. ]]
Shape a.shape
b.shape
(3,)
(2, 3)
Mem Type a.dtype
b.dtype
int32
float64
Item Size a.itemsize
b.itemsize
4
8
Single Element b[1,3] 7.0
Single Row b[0,:] [0.9 0.8 7. ]
Single Collumn b[:,2] [7. 7.]
Sub-Array b[0, 1:-1:2] elements row 0 from 1 to -1 by step 2 [0.8]
Assign Element a[1]=20 [1 20 3]

Initializing Different Types of Arrays

Action Command Result
All zero array np.zeros(5)
np.zeros((2,3))
[0. 0. 0. 0. 0.]
[[0. 0. 0.]
[0. 0. 0.]]
All-ones array np.ones(5) [1. 1. 1. 1. 1.]