├── copyview.py ├── filtering.py ├── fun.py ├── intro.py ├── it.py ├── s.py ├── searcher.py ├── shaper.py └── slice.py /copyview.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | # Copy Vs. View 4 | np1 = np.array([0,1,2,3,4,5]) 5 | 6 | # Create a view 7 | np2 = np1.view() 8 | 9 | print(f'Original NP1 {np1}') 10 | print(f'Original NP2 {np2}') 11 | 12 | np2[0] = 41 13 | 14 | print(f'Changed NP1 {np1}') 15 | print(f'Original NP2 {np2}') 16 | 17 | 18 | ''' 19 | # Create a Copy 20 | np2 = np1.copy() 21 | print(f'Original NP1 {np1}') 22 | print(f'Original NP2 {np2}') 23 | 24 | np2[0] = 41 25 | 26 | print(f'Changed NP1 {np1}') 27 | print(f'Original NP2 {np2}') 28 | ''' -------------------------------------------------------------------------------- /filtering.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | # Filtering Numpy Arrays With Boolean Index Lists 4 | np1 = np.array([1,2,3,4,5,6,7,8,9,10]) 5 | 6 | #x = [True, True, False, False, False, False, False, False, False, False] 7 | #print(np1) 8 | #print(np1[x]) 9 | 10 | ''' 11 | filtered = [] 12 | for thing in np1: 13 | if thing % 2 == 0: 14 | filtered.append(True) 15 | else: 16 | filtered.append(False) 17 | 18 | 19 | print(np1) 20 | print(filtered) 21 | print(np1[filtered]) 22 | ''' 23 | 24 | # Shortcut! 25 | filtered = np1 > 5 26 | print(np1) 27 | print(filtered) 28 | print(np1[filtered]) -------------------------------------------------------------------------------- /fun.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | # Numpy Universal Function 4 | np1 = np.array([-3,-2,-1,0,1,2,3,4,5,6,7,8,9]) 5 | print(np1) 6 | 7 | # Square root of each element 8 | #print(np.sqrt(np1)) 9 | 10 | # Absolute Value 11 | #print(np.absolute(np1)) 12 | 13 | # Exponents 14 | #print(np.exp(np1)) 15 | 16 | # Min/Max 17 | #print(np.max(np1)) 18 | #print(np.min(np1)) 19 | 20 | # Sign positive or negative 21 | #print(np.sign(np1)) 22 | 23 | # Trig sin cos log 24 | print(np.log(np1)) -------------------------------------------------------------------------------- /intro.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | list1 = [1,2,3,4,5] 4 | #print(list1) 5 | #print(list1[0]) 6 | 7 | list2 = ["John Elder", 41, list1, True] 8 | #print(list2) 9 | 10 | 11 | # Numpy - Numeric Python 12 | # ndarray = n-dimensional array 13 | 14 | np1 = np.array([0,1,2,3,4,5,6,7,8,9]) 15 | print(np1) 16 | print(np1.shape) 17 | 18 | # Range 19 | np2 = np.arange(10) 20 | print(np2) 21 | 22 | # Step 23 | np3 = np.arange(0,10, 2) 24 | print(np3) 25 | 26 | # Zeros 27 | np4 = np.zeros(10) 28 | print(np4) 29 | 30 | # Multidimensional zeros 31 | np5 = np.zeros((2,10)) 32 | print(np5) 33 | 34 | # Full 35 | np6 = np.full((10), 6) 36 | print(np6) 37 | 38 | # Multidimensional Full 39 | np7 = np.full((2,10), 6) 40 | print(np7) 41 | 42 | # Convert Python lists to np 43 | my_list = [1,2,3,4,5] 44 | np8 = np.array(my_list) 45 | print(np8) 46 | 47 | print(np8[0]) 48 | 49 | -------------------------------------------------------------------------------- /it.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | # 1-d 4 | np1 = np.array([1,2,3,4,5,6,7,8,9,10]) 5 | #for x in np1: 6 | # print(x) 7 | 8 | 9 | # 2-d Array 10 | ''' 11 | np2 = np.array([[1,2,3,4,5],[6,7,8,9,10]]) 12 | for x in np2: 13 | # Print rows 14 | #print(x) 15 | for y in x: 16 | print(y) 17 | ''' 18 | 19 | # 3-D Array 20 | np3 = np.array([[[1,2,3],[4,5,6]], [[7,8,9],[10,11,12]]]) 21 | ''' 22 | for x in np3: 23 | #print(x) 24 | for y in x: 25 | #print(y) 26 | for z in y: 27 | print(z) 28 | ''' 29 | 30 | # Use np.nditer() 31 | for x in np.nditer(np1): 32 | print(x) 33 | -------------------------------------------------------------------------------- /s.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | # np.sort() Numerical 4 | np1 = np.array([6,7,4,9,0,2,1]) 5 | #print(np1) 6 | #print(np.sort(np1)) 7 | 8 | 9 | # Alphabetical 10 | #np2 = np.array(["John", "Tina", "Aaron", "Zed"]) 11 | #print(np2) 12 | #print(np.sort(np2)) 13 | 14 | 15 | # Booleans T/F 16 | #np3 = np.array([True, False, False, True]) 17 | #print(np3) 18 | #print(np.sort(np3)) 19 | 20 | # Return a copy not change the original 21 | #print(np1) 22 | #print(np.sort(np1)) 23 | #print(np1) 24 | 25 | # 2-d 26 | np4 = np.array([[6,7,1,9],[8,3,5,0]]) 27 | print(np4) 28 | print(np.sort(np4)) -------------------------------------------------------------------------------- /searcher.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | # Search 4 | np1 = np.array([1,2,3,4,5,6,7,8,9,10, 3]) 5 | 6 | #x = np.where(np1 == 3) 7 | #print(np1) 8 | #print(x[0]) 9 | #print(np1[x[0]]) 10 | 11 | 12 | # Return even items 13 | #y = np.where(np1 % 2 == 0) 14 | #print(np1) 15 | #print(y[0]) 16 | 17 | # Return odd items 18 | z = np.where(np1 % 2 == 1) 19 | print(np1) 20 | print(z[0]) -------------------------------------------------------------------------------- /shaper.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | # Create 1-D Numpy Array and Get Shape 4 | np1 = np.array([1,2,3,4,5,6,7,8,9,10,11,12]) 5 | print(np1) 6 | print(np1.shape) 7 | 8 | # Create 2-D Array and get Shape, (rows/columns-elements) 9 | #np2 = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]]) 10 | #print(np2) 11 | #print(np2.shape) 12 | 13 | # Reshape 2-D 14 | #np3 = np1.reshape(3,4) 15 | #print(np3) 16 | #print(np3.shape) 17 | 18 | # Reshape 3-D 19 | np4 = np1.reshape(2,3,2) 20 | print(np4) 21 | print(np4.shape) 22 | 23 | # Flatten to 1-D 24 | np5 = np4.reshape(-1) 25 | print(np5) 26 | print(np5.shape) 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /slice.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | # Slicing Numpy Arrays 4 | np1 = np.array([1,2,3,4,5,6,7,8,9]) 5 | 6 | # Return 2,3,4,5 7 | print(np1[1:5]) 8 | 9 | # Return from something till the end of the array? 10 | # 4-9 11 | print(np1[3:]) 12 | 13 | # Return Negative Slices 14 | # 7, 8 15 | print(np1[-3:-1]) 16 | 17 | # Steps 18 | print(np1[1:5]) # 2-5 19 | print(np1[1:5:2]) # 2-5 in steps of 2 20 | 21 | # Steps on the enitre array 22 | print(np1[::2]) 23 | print(np1[::3]) 24 | 25 | # Slice a 2-d array 26 | np2 = np.array([ 27 | [1,2,3,4,5], 28 | [6,7,8,9,10]]) 29 | # Pull out a single item 30 | print(np2[1,2]) 31 | 32 | # Slice a 2-d array 2,3 33 | print(np2[0:1, 1:3]) 34 | 35 | # Slice from both rows 2,3 and 7,8 36 | print(np2[0:2, 1:3]) --------------------------------------------------------------------------------