NumPy:
The Best
Poorly Documented
Swiss-Army Knife
Ever

Daniel Arbuckle

SoCal Piggies

Quick and Dirty: What is it?

[any material that should appear in print but not on the slide]

Relation to other projects

[any material that should appear in print but not on the slide]

Things you have to know

[any material that should appear in print but not on the slide]

Arrays


>>> a = numpy.arange(0, 9, dtype = float).reshape((3, 3))
>>> b = numpy.arange(10, 19, dtype = int).reshape((3, 3))
>>> c = numpy.arange(20, 29, dtype = int).reshape((3, 3))
>>> c
array([[20, 21, 22],
       [23, 24, 25],
       [26, 27, 28]])

>>> (a + b) / c
array([[ 0.5       ,  0.57142857,  0.63636364],
       [ 0.69565217,  0.75      ,  0.8       ],
       [ 0.84615385,  0.88888889,  0.92857143]])

>>> _ > 0.7
array([[False, False, False],
       [False, True, True],
       [True, True, True]], dtype=bool)
[any material that should appear in print but not on the slide]

Shape


>>> c.shape
(3, 3)
>>> c.reshape((9))
array([20, 21, 22, 23, 24, 25, 26, 27, 28])
[any material that should appear in print but not on the slide]

Extended indexing and slicing

[any material that should appear in print but not on the slide]

Broadcasting


>>> x = numpy.arange(3)
>>> y = numpy.arange(3).reshape(3, 1)

>>> x
array([0, 1, 2])

>>> y
array([[0],
       [1],
       [2]])

>>> x * y
array([[0, 0, 0],
       [0, 1, 2],
       [0, 2, 4]])

[any material that should appear in print but not on the slide]

array and matrix


>>> y
array([[0],
       [1],
       [2]])
>>> m
matrix([[20, 21, 22],
        [23, 99, 25],
        [26, 27, 28]])
>>> x = m.I * y
>>> x
matrix([[  3.66666667e+00],
        [ -7.28583860e-17],
        [ -3.33333333e+00]])
>>> (y == (m * x).round(7)).all()
True
[any material that should appear in print but not on the slide]

Error handling

[any material that should appear in print but not on the slide]

Useful URLs

[any material that should appear in print but not on the slide]