#!/usr/bin/env python # coding: utf-8 # In[1]: import numpy as np # ### Arrays # In[2]: # numpy array arr = np.array([0,1,2,3]) arr # numpy calc is faster than native python # In[3]: # testing the same L = range(100000) get_ipython().run_line_magic('timeit', '[i**2 for i in L]') # In[4]: L = np.arange(100000) get_ipython().run_line_magic('timeit', 'L**2') # there are so many ways to create array in numpy, we will see them one by one # #### starting with 1-D array # In[5]: a = np.array([1,2,5,3,5,9]) a # In[6]: a = np.arange(1, 20) # start, end a # In[7]: a = np.arange(1, 20, 3) # start, end, step a # In[8]: # check dimension and size print("dimension - ", a.ndim) print("size - ", a.shape) print("length - ", len(a)) # #### 2-D array # In[9]: b = np.array([[1,2,3], [2,3,4], [3,4,5]]) b # In[10]: # check dimension and size print("dimension - ", b.ndim) print("size - ", b.shape) print("length - ", len(b)) # 3-D array # In[11]: c = np.array([[[5,7], [8,9], [0,7]], [[1,2], [2,3], [6,7]], [[5,7], [8,9], [0,7]]]) c # In[12]: # check dimension and size print("dimension - ", c.ndim) print("size - ", c.shape) print("length - ", len(c)) # Simplest way to create test array # In[13]: # by no a = np.arange(1, 10, 2) print(a) print(a.shape) # In[14]: # by number of points a = np.linspace(1, 10, 4) # pick any 4 no from 1 to 10 print(a) print(a.shape) # In[15]: # by number of points a = np.linspace(1, 10, 6, endpoint=False) # pick any 4 no from 1 to 10 print(a) print(a.shape) # Common arrays to use : # In[16]: np.ones(4) # In[17]: np.ones((2,4)) # In[18]: np.zeros((2,3)) # In[19]: np.eye((3)) # In[20]: np.diag([1,2,3,4]) # #### Random no generation # with help of numpy random library # In[21]: np.random.rand(5) # pick any 5 value between 0 and 1 # In[22]: np.random.randn(5) # includes negative no as well (Gaussian) # In[23]: np.random.randint(9) # this will pick any random no from 0 to 8 (integer) # In[24]: np.random.randint(10, 13) # start, end end exclusive # In[25]: np.random.randint(10, 13, 2) # start, end, no of element end exclusive # In[26]: np.random.randint(10, 13, 4) # start, end, no of element end exclusive # In[27]: np.random.randint(1, 20, (4, 3)) # If you set the np.random.seed(a_fixed_number) every time you call the numpy's other random function, the result will be the same: # In[28]: np.random.seed(123) np.random.randint(1, 10, 2) # In[29]: np.random.seed(123) np.random.randint(1, 10, 2) # #### Data Type # In[30]: a = np.random.randint(3,9, 3) print(a) print(a.dtype) # In[31]: a = np.random.rand(3) print(a) print(a.dtype) # You can explicitly specify which data-type you want: # In[32]: c = np.array([1, 2, 3], dtype=float) c.dtype # In[33]: c = np.array([1, 2, 3]) c.dtype # In[34]: a = np.ones((3, 3)) a.dtype # In[35]: # complex data type d = np.array([1+2j, 3+4j, 5+6*1j]) d.dtype # In[36]: # Boolean e = np.array([True, False, False, True]) e.dtype # In[37]: # String f = np.array(['Bonjour', 'Hello', 'Hallo']) f.dtype # unicode string with max 7 letters # ### Indexing and Slicing # In[38]: a = np.arange(10) print(a) a[2], a[0], a[8], a[-1] # In[39]: a[2:7] # In[40]: # reversing the array a[::-1] # In[41]: a[1:8:3] #start, end, step # In[42]: a[:4] #start, 4 # In[43]: a[6:] #6, end # In[44]: a[:] #start, end # numpy array supports all python Indexing and Slicing operation # #### Numpy indexing and Slicing # In[45]: a = np.arange(10) a # In[46]: a[5:] = 11 a # In[47]: a[6:] = a[::-1][6:] a # ### Array Copies and Views # In[48]: a = np.arange(10) a # In[49]: b = a[6:] b # In[50]: a[7] = 13 print(a) print(b) # In[51]: print(np.may_share_memory(a,b)) # #### Copy # In[52]: b = a[6:].copy() b # In[53]: a[8] = 16 print(a) print(b) # In[54]: print(np.may_share_memory(a,b)) # #### Boolean indexing or Fancy indexing # In[55]: a = np.arange(10) a # In[56]: b = a%3==0 b # boolean array # In[57]: # picking only those for which a%3==0 is TRUE a[b] # boolean indexing # In[58]: a[b] = a[b]+200 a # In[59]: b = np.random.randint(0, 6, 3) b # In[60]: a[b] # In[ ]: