Show Menu
Cheatography

Numpy Cheat Sheet (DRAFT) by

The NumPy library is the core library for scientific computing in Python. It provides a high-performance multidimensional array object and tools for working with these arrays.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Creating Arrays

a = np.arr­ay([1, 2, 3])                      # 1D array
b = np.arr­ay(­[(1.5, 2, 3), (4, 5, 6)], dtype=­float) # 2D array with float type
c = np.zer­os((3, 4)) # Array of zeros
d = np.one­s((2, 3, 4),, dtype=­np.i­nt16) # Array of ones
e = np.ara­nge(10, 25, 5dtype­=np.int16) # Array of evenly spaced values (step value)
f = np.lin­spa­ce(0, 2, 9) # Array of evenly spaced values (number of samples)
g = np.ful­l((2, 2), 7) # Constant array
h = np.eye(2) # 2x2 identity matrix
i = np.ran­dom.ra­ndo­m((2, 2)) # Array with random values
j = np.emp­ty((3, 2)) # Empty array
 

Getting Info

np.inf­o(n­p.n­dar­ray.dtype)
 

Comparison

a == b # Elemen­t-wise comparison
a < 2 # Elemen­t-wise comparison
np.array_equal(a, b) # Array-wise comparison