5.20 - Numpy Operations


In [1]:
import numpy as np

In [2]:
arr = np.arange(0,11)

In [3]:
arr


Out[3]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

Arithmetic


In [4]:
arr + arr


Out[4]:
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18, 20])

In [5]:
arr - arr


Out[5]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

In [14]:
arr * arr


Out[14]:
array([  0,   1,   4,   9,  16,  25,  36,  49,  64,  81, 100])

In [15]:
arr / arr # world a splode?


/home/iannesbitt/bin/.ve/main/lib/python2.7/site-packages/ipykernel/__main__.py:1: RuntimeWarning: divide by zero encountered in divide
  if __name__ == '__main__':
Out[15]:
array([0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

In [12]:
arr + 100


Out[12]:
array([   0,  100,  200,  300,  400,  500,  600,  700,  800,  900, 1000])

In [13]:
arr * 100


Out[13]:
array([   0,  100,  200,  300,  400,  500,  600,  700,  800,  900, 1000])

In [16]:
arr - 100


Out[16]:
array([-100,  -99,  -98,  -97,  -96,  -95,  -94,  -93,  -92,  -91,  -90])

In [17]:
arr / 5.0


Out[17]:
array([ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8,  2. ])

In [18]:
1 / arr


/home/iannesbitt/bin/.ve/main/lib/python2.7/site-packages/ipykernel/__main__.py:1: RuntimeWarning: divide by zero encountered in divide
  if __name__ == '__main__':
Out[18]:
array([0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])

In [20]:
arr ** arr # well that escalated quickly


Out[20]:
array([          1,           1,           4,          27,         256,
              3125,       46656,      823543,    16777216,   387420489,
       10000000000])

Basic math and trig functions


In [34]:
np.sqrt(arr)


Out[34]:
array([ 0.        ,  1.        ,  1.41421356,  1.73205081,  2.        ,
        2.23606798,  2.44948974,  2.64575131,  2.82842712,  3.        ,
        3.16227766])

In [22]:
np.exp(arr)


Out[22]:
array([  1.00000000e+00,   2.71828183e+00,   7.38905610e+00,
         2.00855369e+01,   5.45981500e+01,   1.48413159e+02,
         4.03428793e+02,   1.09663316e+03,   2.98095799e+03,
         8.10308393e+03,   2.20264658e+04])

In [26]:
np.max(arr) # same as below, but passing the array directly to the max() function


Out[26]:
10

In [27]:
arr.max() # same as above, but calling method off of array object


Out[27]:
10

In [32]:
np.sin(arr) # also np.cos(), np.tan(), np.arccos(), etc


Out[32]:
array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ,
       -0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849,
       -0.54402111])

In [33]:
np.log(arr) # sploded


/home/iannesbitt/bin/.ve/main/lib/python2.7/site-packages/ipykernel/__main__.py:1: RuntimeWarning: divide by zero encountered in log
  if __name__ == '__main__':
Out[33]:
array([       -inf,  0.        ,  0.69314718,  1.09861229,  1.38629436,
        1.60943791,  1.79175947,  1.94591015,  2.07944154,  2.19722458,
        2.30258509])

In [ ]: