In [2]:
import numpy as np # stands for numerical python
In [3]:
# Numpy array comes in two flavors namely vectors and matrices
# vectors are 1-dimensional arrays
# matrices are multi-dimensional arrays
In [4]:
# Creating a numpy array
my_list = [1,2,3]
x = np.array(my_list)
type(x)
Out[4]:
In [5]:
x
Out[5]:
In [6]:
my_matrix = [[1,2,3],[4,5,6], [7,8,9]]
np.array(my_matrix)
Out[6]:
In [8]:
# regular
list(range(0,5))
Out[8]:
In [10]:
# numpy style (array range)
np.arange(0,5)
Out[10]:
In [11]:
np.arange(1,11,2)
Out[11]:
In [13]:
np.zeros(3) # returns an array of three zeros
Out[13]:
In [15]:
np.zeros((3,5)) # (rows, columns)
Out[15]:
In [16]:
np.ones(5)
Out[16]:
In [17]:
np.ones((3,2))
Out[17]:
In [20]:
np.linspace(0,10,3) #linearly spaced (spaced out evenly)
Out[20]:
In [22]:
np.linspace(0,10,51)
Out[22]:
In [25]:
np.eye(4) # identity matrix in numpy
Out[25]:
In [30]:
# numpy random library'
# uniform distribution
np.random.rand(1)
Out[30]:
In [31]:
np.random.rand(5,4)
Out[31]:
In [33]:
# normal distribution
np.random.randn(5) # 5 numbers from normal distribution
Out[33]:
In [34]:
np.random.randn(5,4)
Out[34]:
In [35]:
np.random.randint(1,100) # random integer
Out[35]:
In [36]:
np.random.randint(1,100,5)
Out[36]:
In [37]:
np.random.randint(1,100,(5,4))
Out[37]:
In [38]:
# reshaping arrays in numpy
arr = np.arange(25)
randarr = np.random.randint(0,50,10)
In [39]:
print(arr)
print(randarr)
In [41]:
arr.reshape(5,5) # since 5 x 5 = 25
Out[41]:
In [42]:
arr.shape
Out[42]:
In [43]:
arr.reshape(5,5).shape
Out[43]:
In [44]:
arr.reshape(25,1)
Out[44]:
In [47]:
arr.reshape(25,1).shape #difference between (25,) and (25,1)
Out[47]:
In [48]:
arr.dtype # datatype attribute
Out[48]:
In [50]:
randarr.max() # max() method
Out[50]:
In [51]:
randarr.argmax() # returns the index location of maximum value
Out[51]:
In [52]:
randarr.min() # min() method
Out[52]:
In [53]:
randarr.argmin() # returns the index location of minimum value
Out[53]:
In [54]:
import numpy as np
In [55]:
arr = np.arange(0,10)
arr
Out[55]:
In [56]:
arr + arr # elementwise addition
Out[56]:
In [57]:
arr * arr # elementwise multiplication
Out[57]:
In [58]:
arr - arr # elementwise subtraction
Out[58]:
In [60]:
arr / arr # elementwise division, #this gives zero divide by zero warning
Out[60]:
In [62]:
1 / arr # infinity warning
Out[62]:
In [63]:
arr ** 3
Out[63]:
In [64]:
arr + 100
Out[64]:
In [65]:
# taking square root
np.sqrt(arr)
Out[65]:
In [66]:
# taking exponential
np.exp(arr)
Out[66]:
In [67]:
np.max(arr)
Out[67]:
In [68]:
arr.max()
Out[68]:
In [70]:
np.sin(arr)
Out[70]:
In [72]:
np.log(arr) # log of zero warning
Out[72]:
In [73]:
import numpy as np
In [74]:
arr = np.arange(0,11)
In [75]:
arr
Out[75]:
In [78]:
# grabing notations
arr[:5]
Out[78]:
In [79]:
arr[6:]
Out[79]:
In [80]:
arr[3:6]
Out[80]:
In [82]:
# broadcasting
arr
Out[82]:
In [85]:
arr[0:5] = 100 # awesome :D
In [86]:
arr
Out[86]:
In [87]:
arr = np.arange(0,11)
arr
Out[87]:
In [89]:
slice_of_arr = arr[:6]
slice_of_arr
Out[89]:
In [91]:
slice_of_arr[:] = 99
slice_of_arr
Out[91]:
In [93]:
arr # original arr changes with slice_of_arr
Out[93]:
In [94]:
arr_copy = arr.copy()
arr_copy
Out[94]:
In [95]:
arr_copy[:] = 33
arr_copy
Out[95]:
In [96]:
arr
Out[96]:
In [97]:
# Indexing
# mat[row, col]]
# mat[row][col]
In [98]:
mat = np.array([[5,10,15],[20,25,30],[35,40,45]])
mat
Out[98]:
In [99]:
mat[0]
Out[99]:
In [100]:
mat[-1]
Out[100]:
In [101]:
mat[1,1]
Out[101]:
In [102]:
mat[1][1]
Out[102]:
In [103]:
# 2-d array slicing
mat
Out[103]:
In [105]:
# grab everything uptill row index 2 and grab from col index 1 to everything
mat[:2,1:]
Out[105]:
In [106]:
mat[1:,:2]
Out[106]:
In [109]:
# conditional selection
arr = np.arange(1,11)
arr
Out[109]:
In [110]:
arr > 4
Out[110]:
In [111]:
bool_arr = arr > 4
In [112]:
arr[bool_arr]
Out[112]:
In [113]:
arr[arr > 4]
Out[113]:
In [115]:
arr[arr <= 9]
Out[115]:
In [ ]: