Show Menu
Cheatography

Numpy reference sheet

Numpy - Single dimens­ional arrays

Creating an array from a list
x = np.arr­ay(­['a', 'b', '9', '8']
Array Data Types
Consist of integers, floati­ng-­point numbers, or strings. Data type must be consis­tent. Numpy's record array gives mixed DTypes
Find the length on an array
np.len(x)
Accessing elements from an array
x[inde­x_num] x[1]
Assign elements from index
x[1]=c
Slicing x[star­t:end])
print(­x[1:2]) ['b', '9']
Slicing x[star­t:end: step])
print(­x[1­:3:2]) ['b', '8']
Modify a new version of an array without changing the original
y = x.copy()
Negative slices, single value x[-Dis­tance from end]
x[-2] b
Negative slices, reversal x[star­t:end: -step])
x[3:0:-2]) 8, b
Adding to an array
x.appe­nd('7')
Saving to binary file
np.sav­e(o­pen­('d­ata.npy', 'wb'), data)
 

Useful Numpy Functions

Array Creation:
arange, array, copy, empty, empty_­like, eye, fromfile, fromfu­nction, identity, linspace, logspace, mgrid, ogrid, ones, ones_like, r_, zeros, zeros_like
Conver­sions
ndarra­y.a­stype, atleas­t_1d, atleas­t_2d, atleas­t_3d, mat
Manipu­lat­ions:
array_­split, column­_stack, concat­enate, diagonal, dsplit, dstack, hsplit, hstack, ndarra­y.item, newaxis, ravel, repeat, reshape, resize, squeeze, swapaxes, take, transpose, vsplit, vstack
Questions:
all, any, nonzero, where
Ordering:
argmax, argmin, argsort, max, min, ptp, search­sorted, sort
Operat­ions:
choose, compress, cumprod, cumsum, inner, ndarra­y.fill, imag, prod, put, putmask, real, sum
Basic Statis­tics:
cov, mean, std, var
Basic Linear Algebra:
cross, dot, outer, linalg.svd, vdot

Combining Arrays

np.vta­ck((a1, a2) =
np.con­cat­ena­te((a1, a2, axis = 0)
np.hst­ack­(a1,a2) =
np.con­cat­ena­te((a1, a2, axis = 1)
vsplit
splits along the vertical axis
hsplit
splits along the horizontal axis
 

Universal Functions (ufuncts)

Implement vector­ization (opera­tions applied to whole arrays instead of individual elements) in NumPy which is way faster than iterating over elements.
Also provide broadc­asting (when smaller array is cast across the larger array so that they have compatible shapes)
x = [1, 2, 3, 4] y = [4, 5, 6, 7] z = np.add(x, y)
[ 5 7 9 11]
Check if a function is a ufunc:
print(­typ­e(n­p.add))
arr1 = np.arr­ay([10, 20, 30, 40, 50, 60])
arr2 = np.arr­ay([20, 21, 22, 23, 24, 25])
np.sub­tra­ct(­arr1, arr2)
newarr = np.mul­tip­ly(­arr1, arr2)
newarr = np.div­ide­(arr1, arr2)
newarr = np.pow­er(­arr1, arr2)
newarr = np.rem­ain­der­(arr1, arr2)
newarr = np.abs­olu­te(arr)

Iterating

arr = np.arr­ay([[1, 2, 3], [4, 5, 6]])
for x in arr: print(x)
[1, 2, 3], [4, 5, 6]
for x in arr: for y in x: print(y)
123456
for x in np.ndi­ter­(arr):
123456
for x in np.ndi­ter­(arr, flags=­['b­uff­ered'], op_dty­pes­=['­S']): print(x)
b '1', b'2', b'3'
for x in np.ndi­ter­(arr[:, ::2]): print(x)
1, 3, 5, 7
Enumer­ation means mentioning sequence number of somethings one by one.
for idx, x in np.nde­num­era­te(­arr): print(idx, x)
​ for idx, x in np.nde­num­era­te(­arr): print(idx, x) ​ (
0,) 1 (1,) 2 (2,) 3
for idx, x in np.nde­num­era­te(­arr): print(idx, x)
Result Size: 1425 x 1251 import numpy as np ​ arr = np.arr­ay([[1, 2, 3, 4], [5, 6, 7, 8]]) ​ for idx, x in np.nde­num­era­te(­arr): print(idx, x) ​ (0, 0) 1 (0, 1) 2 (0, 2) 3 (0, 3) 4 (1, 0) 5 (1, 1) 6 (1, 2) 7 (1, 3) 8
           
 

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.

          More Cheat Sheets by datamansam

          Jupyter Notebook Keyboard Shortcuts
          DataBase Concepts Cheat Sheet
          Reg Ex CheatSheet Cheat Sheet