In [1]:
my_list = [1, 2, 3]
In [2]:
import numpy as np
In [3]:
arr = np.array(my_list)
In [4]:
arr
Out[4]:
array([1, 2, 3])
In [5]:
my_mat = [[1,2,3], [4,5,6], [7,8,9]]
In [6]:
np.array(my_mat)
Out[6]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
In [7]:
np.arange(0,11,2)
Out[7]:
array([ 0,  2,  4,  6,  8, 10])
In [8]:
np.zeros((2,3))
Out[8]:
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
In [9]:
np.ones((4,5))
Out[9]:
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])
In [10]:
np.linspace(0,5,10)
Out[10]:
array([ 0.        ,  0.55555556,  1.11111111,  1.66666667,  2.22222222,
        2.77777778,  3.33333333,  3.88888889,  4.44444444,  5.        ])
In [11]:
np.eye(6)
Out[11]:
array([[ 1.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  1.]])
In [12]:
np.random.rand(5,3)
Out[12]:
array([[ 0.42162754,  0.95209219,  0.03863609],
       [ 0.97368764,  0.32488703,  0.27710062],
       [ 0.20310345,  0.11553538,  0.29748249],
       [ 0.73735179,  0.83111281,  0.72444372],
       [ 0.18823924,  0.24592357,  0.80646367]])
In [13]:
np.random.randn(2)
Out[13]:
array([-1.76091738, -0.33921038])
In [14]:
np.random.randint(1,10,10)
Out[14]:
array([8, 6, 5, 4, 8, 4, 8, 4, 6, 7])
In [15]:
arr = np.arange(20)
In [16]:
arr
Out[16]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])
In [17]:
arr.reshape(2,10)
Out[17]:
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]])
In [18]:
np.arange(20).reshape(4,5)
Out[18]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
In [19]:
arr
Out[19]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])
In [20]:
arr.resize(4,5)
In [21]:
arr
Out[21]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
In [22]:
ranarr = np.random.randint(0,100,10)
In [23]:
ranarr
Out[23]:
array([52, 29, 10, 91,  8, 76, 41, 16, 51, 11])
In [24]:
ranarr.max()
Out[24]:
91
In [25]:
ranarr.min()
Out[25]:
8
In [26]:
ranarr.argmax()
Out[26]:
3
In [27]:
ranarr.argmin()
Out[27]:
4
In [28]:
arr.shape
Out[28]:
(4, 5)
In [29]:
arrr = np.linspace(0,100,20)
In [30]:
arrr
Out[30]:
array([   0.        ,    5.26315789,   10.52631579,   15.78947368,
         21.05263158,   26.31578947,   31.57894737,   36.84210526,
         42.10526316,   47.36842105,   52.63157895,   57.89473684,
         63.15789474,   68.42105263,   73.68421053,   78.94736842,
         84.21052632,   89.47368421,   94.73684211,  100.        ])
In [31]:
arrr.shape
Out[31]:
(20,)
In [32]:
arrr.dtype
Out[32]:
dtype('float64')
In [33]:
arr.dtype
Out[33]:
dtype('int64')
In [34]:
from numpy.random import randint
In [35]:
randint(2,10,5)
Out[35]:
array([3, 6, 8, 4, 2])

Array Indexing

In [36]:
ranarr
Out[36]:
array([52, 29, 10, 91,  8, 76, 41, 16, 51, 11])
In [37]:
ranarr[1:5]
Out[37]:
array([29, 10, 91,  8])
In [38]:
ranarr[3:]
Out[38]:
array([91,  8, 76, 41, 16, 51, 11])
In [39]:
arr = np.arange(0,11)

broadcasting a value

In [40]:
arr
Out[40]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
In [41]:
arr[0:5] = 100
In [42]:
arr
Out[42]:
array([100, 100, 100, 100, 100,   5,   6,   7,   8,   9,  10])
In [43]:
lst = list(range(0,11))
In [44]:
lst
Out[44]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [45]:
lst[0:5] = 100  # this make the difference between using numpy or not
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-45-472999481055> in <module>()
----> 1 lst[0:5] = 100  # this make the difference between using numpy or not

TypeError: can only assign an iterable

Copying or Viewing

In [46]:
arr = np.arange(0,11)
In [47]:
slice_of_arr = arr[0:6]   # note: it is not copying but "viewing"
In [48]:
slice_of_arr
Out[48]:
array([0, 1, 2, 3, 4, 5])
In [49]:
slice_of_arr[:]
Out[49]:
array([0, 1, 2, 3, 4, 5])
In [50]:
slice_of_arr[:] = 99
In [51]:
slice_of_arr
Out[51]:
array([99, 99, 99, 99, 99, 99])
In [52]:
arr  # notice that the change of the items
Out[52]:
array([99, 99, 99, 99, 99, 99,  6,  7,  8,  9, 10])
In [53]:
arr_copy = arr.copy()  # note: yes it is copying
In [54]:
arr_copy
Out[54]:
array([99, 99, 99, 99, 99, 99,  6,  7,  8,  9, 10])
In [55]:
arr_copy[:] = 1
In [56]:
arr_copy
Out[56]:
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
In [57]:
arr # no change for the items
Out[57]:
array([99, 99, 99, 99, 99, 99,  6,  7,  8,  9, 10])

2 D Array

In [58]:
arr_2d = np.array([[1,2,3],[4,5,6],[7,8,9]])
In [59]:
arr_2d
Out[59]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
In [60]:
arr_2d[0]
Out[60]:
array([1, 2, 3])
In [61]:
arr_2d[0][0]
Out[61]:
1
In [62]:
arr_2d[2,1]
Out[62]:
8
In [63]:
arr_2d[:2,1:] # slice everything but not including row of index 2, slice the column from index 1 onward
Out[63]:
array([[2, 3],
       [5, 6]])
In [64]:
arr_2d[:,2]
Out[64]:
array([3, 6, 9])
In [65]:
arr_2d[:2]
Out[65]:
array([[1, 2, 3],
       [4, 5, 6]])

Conditional Selection

In [66]:
arr = np.arange(1,11)
In [67]:
arr
Out[67]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
In [68]:
bool_arr = arr > 5
In [69]:
bool_arr
Out[69]:
array([False, False, False, False, False,  True,  True,  True,  True,  True], dtype=bool)
In [70]:
arr[bool_arr]
Out[70]:
array([ 6,  7,  8,  9, 10])
In [71]:
arr[arr > 6]
Out[71]:
array([ 7,  8,  9, 10])

Numpy Operations

In [72]:
arr = np.arange(1,11)
In [73]:
arr + arr
Out[73]:
array([ 2,  4,  6,  8, 10, 12, 14, 16, 18, 20])
In [74]:
arr - arr
Out[74]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
In [75]:
arr * arr
Out[75]:
array([  1,   4,   9,  16,  25,  36,  49,  64,  81, 100])
In [76]:
arr / arr
Out[76]:
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])
In [77]:
arr + 10
Out[77]:
array([11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
In [78]:
arr - 10
Out[78]:
array([-9, -8, -7, -6, -5, -4, -3, -2, -1,  0])
In [79]:
arr ** 2
Out[79]:
array([  1,   4,   9,  16,  25,  36,  49,  64,  81, 100])

divide by zero

In [80]:
1/0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-80-9e1622b385b6> in <module>()
----> 1 1/0

ZeroDivisionError: division by zero
In [81]:
arr = np.arange(11)
In [82]:
arr
Out[82]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
In [83]:
arr / arr
/Users/adeirawan/.virtualenvs/dl4cv/lib/python3.6/site-packages/ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in true_divide
  """Entry point for launching an IPython kernel.
Out[83]:
array([ nan,   1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.])

Universal array function

In [84]:
np.sqrt(arr)
Out[84]:
array([ 0.        ,  1.        ,  1.41421356,  1.73205081,  2.        ,
        2.23606798,  2.44948974,  2.64575131,  2.82842712,  3.        ,
        3.16227766])
In [85]:
np.exp(arr)
Out[85]:
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 [86]:
np.max(arr)
Out[86]:
10
In [88]:
arr.max()
Out[88]:
10
In [89]:
np.sin(arr)
Out[89]:
array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ,
       -0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849,
       -0.54402111])
In [90]:
np.log(arr)
/Users/adeirawan/.virtualenvs/dl4cv/lib/python3.6/site-packages/ipykernel_launcher.py:1: RuntimeWarning: divide by zero encountered in log
  """Entry point for launching an IPython kernel.
Out[90]:
array([       -inf,  0.        ,  0.69314718,  1.09861229,  1.38629436,
        1.60943791,  1.79175947,  1.94591015,  2.07944154,  2.19722458,
        2.30258509])
In [91]:
np.log2(arr)
/Users/adeirawan/.virtualenvs/dl4cv/lib/python3.6/site-packages/ipykernel_launcher.py:1: RuntimeWarning: divide by zero encountered in log2
  """Entry point for launching an IPython kernel.
Out[91]:
array([       -inf,  0.        ,  1.        ,  1.5849625 ,  2.        ,
        2.32192809,  2.5849625 ,  2.80735492,  3.        ,  3.169925  ,
        3.32192809])