260 likes | 279 Views
COMP4332/RMBI4310. Data Processing (Part 2) (Numpy Advanced Operations). Prepared by Raymond Wong Presented by Raymond Wong. Package numpy. Random No. Generation Basic Operation of numpy array Advanced Operation of numpy array Function “loadTxt” a dataset with 3 numeric attributes
E N D
COMP4332/RMBI4310 Data Processing (Part 2) (Numpy Advanced Operations) Prepared by Raymond Wong Presented by Raymond Wong
Package numpy • Random No. Generation • Basic Operation of numpy array • Advanced Operation of numpy array • Function “loadTxt” • a dataset with 3 numeric attributes • a dataset with 3 numeric attributes and 1 string attribute • Function “reshape” • a 1-dimensional data • a 2-dimensional data
Advanced Operation of numpy array • Find (Where) • Update • Delete • Append • Insert • Sort
Python pythonArray = [ [1, 21, 11], [2, 22, 12], [3, 23, 13], [4, 24, 14], [5, 25, 15], [6, 26, 5], [7, 27, 4], [8, 28, 3], [9, 29, 2], [10, 30, 1]] print(" pythonArray: ", pythonArray) Output pythonArray: [[1, 21, 11], [2, 22, 12], [3, 23, 13], [4, 24, 14], [5, 25, 15], [6, 26, 5], [7, 27, 4], [8, 28, 3], [9, 29, 2], [10, 30, 1]]
Python numpyArray = numpy.array(pythonArray) print(" numpyArray: ") print(numpyArray) print(" numpyArray.shape: ", numpyArray.shape) print(" numpyArray.ndim : ", numpyArray.ndim) Output numpyArray: [[ 1 21 11] [ 2 22 12] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]] numpyArray.shape: (10, 3) numpyArray.ndim : 2
Python find (where) “>”: greater than “<”: smaller than “>=”: greater than or equal to “<=”: smaller than or equal to “==”: equal to result = numpy.where(numpyArray < 3) print("result: ") print(result) print("result[0]: ") print(result[0]) print("result[1]: ") print(result[1]) [[ 1 21 11] [ 2 22 12] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]] index = (0, 0) index = (1, 0) index = (8, 2) Output index = (9, 2) result: (array([0, 1, 8, 9], dtype=int64), array([0, 0, 2, 2], dtype=int64)) result[0]: [0 1 8 9] result[1]: [0 0 2 2]
Advanced Operation of numpy array • Find (Where) • Update • Delete • Append • Insert • Sort
Python update numpyArray[1] = [42, 52, 62] print("numpyArray: ") print(numpyArray) Output numpyArray: [[ 1 21 11] [42 52 62] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]]
Advanced Operation of numpy array • Find (Where) • Update • Delete • Append • Insert • Sort
Python delete newNumpyArrayDelete = numpy.delete(numpyArray, 1, axis=0) print("numpyArray: ") print(numpyArray) print("newNumpyArrayDelete: ") print(newNumpyArrayDelete) Output numpyArray: [[ 1 21 11] [42 52 62] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]] newNumpyArrayDelete: [[ 1 21 11] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]]
Python newNumpyArrayDelete = numpy.delete(numpyArray, 1, axis=1) print("numpyArray: ") print(numpyArray) print("newNumpyArrayDelete: ") print(newNumpyArrayDelete) Output numpyArray: [[ 1 21 11] [42 52 62] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]] newNumpyArrayDelete: [[ 1 11] [42 62] [ 3 13] [ 4 14] [ 5 15] [ 6 5] [ 7 4] [ 8 3] [ 9 2] [10 1]]
Python newNumpyArrayDelete = numpy.delete(numpyArray, 1) print("numpyArray: ") print(numpyArray) print("newNumpyArrayDelete: ") print(newNumpyArrayDelete) First flatten and remove the element at index = 1 Output numpyArray: [[ 1 21 11] [42 52 62] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]] newNumpyArrayDelete: [ 1 11 42 52 62 3 23 13 4 24 14 5 25 15 6 26 5 7 27 4 8 28 3 9 29 2 10 30 1]
Advanced Operation of numpy array • Find (Where) • Update • Delete • Append • Insert • Sort
Python append newNumpyArrayAppend = numpy.append(numpyArray, [[62, 72, 82]], axis=0) print("numpyArray: ") print(numpyArray) print("newNumpyArrayAppend: ") print(newNumpyArrayAppend) Output newNumpyArrayAppend: [[ 1 21 11] [42 52 62] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1] [62 72 82]] numpyArray: [[ 1 21 11] [42 52 62] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]]
Python newNumpyArrayAppend = numpy.append(numpyArray, [[71], [72], [73], [74], [75], [76], [77], [78], [79], [70]], axis=1) print("numpyArray: ") print(numpyArray) print("newNumpyArrayAppend: ") print(newNumpyArrayAppend) Note that1. "newNumpyArrayAppend = numpy.append(numpyArray, 71, axis=1)” does not work 2. "newNumpyArrayAppend = numpy.append(numpyArray, [[71], [72]], axis=1)” does not work But, “insert” (to be introduced next) works for the above cases! Output numpyArray: [[ 1 21 11] [42 52 62] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]] newNumpyArrayAppend: [[ 1 21 11 71] [42 52 62 72] [ 3 23 13 73] [ 4 24 14 74] [ 5 25 15 75] [ 6 26 5 76] [ 7 27 4 77] [ 8 28 3 78] [ 9 29 2 79] [10 30 1 70]]
Python newNumpyArrayAppend = numpy.append(numpyArray, [62, 72, 82]) print("numpyArray: ") print(numpyArray) print("newNumpyArrayAppend: ") print(newNumpyArrayAppend) First flatten and append the elements Output numpyArray: [[ 1 21 11] [42 52 62] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]] newNumpyArrayAppend: [ 1 21 11 42 52 62 3 23 13 4 24 14 5 25 15 6 26 5 7 27 4 8 28 3 9 29 2 10 30 1 62 72 82]
Advanced Operation of numpy array • Find (Where) • Update • Delete • Append • Insert • Sort
Python insert newNumpyArrayInsert = numpy.insert(numpyArray, 2, [[62, 72, 82]], axis=0) print("numpyArray: ") print(numpyArray) print("newNumpyArrayInsert: ") print(newNumpyArrayInsert) Output numpyArray: [[ 1 21 11] [42 52 62] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]] newNumpyArrayInsert: [[ 1 21 11] [42 52 62] [62 72 82] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]]
Python newNumpyArrayInsert = numpy.insert(numpyArray, 2, 71, axis=1) print("numpyArray: ") print(numpyArray) print("newNumpyArrayInsert: ") print(newNumpyArrayInsert) Output numpyArray: [[ 1 21 11] [42 52 62] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]] newNumpyArrayInsert: [[ 1 21 71 11] [42 52 71 62] [ 3 23 71 13] [ 4 24 71 14] [ 5 25 71 15] [ 6 26 71 5] [ 7 27 71 4] [ 8 28 71 3] [ 9 29 71 2] [10 30 71 1]]
Python newNumpyArrayInsert = numpy.insert(numpyArray, 2, [[71], [72]], axis=1) print("numpyArray: ") print(numpyArray) print("newNumpyArrayInsert: ") print(newNumpyArrayInsert) Output numpyArray: [[ 1 21 11] [42 52 62] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]] newNumpyArrayInsert: [[ 1 21 71 72 11] [42 52 71 72 62] [ 3 23 71 72 13] [ 4 24 71 72 14] [ 5 25 71 72 15] [ 6 26 71 72 5] [ 7 27 71 72 4] [ 8 28 71 72 3] [ 9 29 71 72 2] [10 30 71 72 1]]
Python newNumpyArrayInsert = numpy.insert(numpyArray, 2, [62, 72, 82]) print("numpyArray: ") print(numpyArray) print("newNumpyArrayInsert: ") print(newNumpyArrayInsert) First flatten and insert the elements starting at index 2 of the new array Output numpyArray: [[ 1 21 11] [42 52 62] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]] newNumpyArrayInsert: [ 1 21 62 72 82 11 42 52 62 3 23 13 4 24 14 5 25 15 6 26 5 7 27 4 8 28 3 9 29 2 10 30 1]
Advanced Operation of numpy array • Find (Where) • Update • Delete • Append • Insert • Sort
Python sort newNumpyArraySort = numpy.sort(numpyArray, axis=0) print("numpyArray: ") print(numpyArray) print("newNumpyArraySort: ") print(newNumpyArraySort) Ascending order (Sort based on the first dimension, then the second dimension and the third dimension) Output newNumpyArraySort: [[ 1 21 1] [ 3 23 2] [ 4 24 3] [ 5 25 4] [ 6 26 5] [ 7 27 11] [ 8 28 13] [ 9 29 14] [10 30 15] [42 52 62]] numpyArray: [[ 1 21 11] [42 52 62] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]]
Python newNumpyArraySort = numpy.sort(numpyArray, axis=0)[::-1] print("numpyArray: ") print(numpyArray) print("newNumpyArraySort: ") print(newNumpyArraySort) Reverse ordering (i.e., descending ordering Output newNumpyArraySort: [[42 52 62] [10 30 15] [ 9 29 14] [ 8 28 13] [ 7 27 11] [ 6 26 5] [ 5 25 4] [ 4 24 3] [ 3 23 2] [ 1 21 1]] numpyArray: [[ 1 21 11] [42 52 62] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]]
Python newNumpyArraySort = numpy.sort(numpyArray, axis=1) print("numpyArray: ") print(numpyArray) print("newNumpyArraySort: ") print(newNumpyArraySort) Output numpyArray: [[ 1 21 11] [42 52 62] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]] newNumpyArraySort: [[ 1 11 21] [42 52 62] [ 3 13 23] [ 4 14 24] [ 5 15 25] [ 5 6 26] [ 4 7 27] [ 3 8 28] [ 2 9 29] [ 1 10 30]]
Python newNumpyArraySort = numpy.sort(numpyArray) print("numpyArray: ") print(numpyArray) print("newNumpyArraySort: ") print(newNumpyArraySort) Sort based on the “last” axis (the default axis) (i.e., axis = 1) Output numpyArray: [[ 1 21 11] [42 52 62] [ 3 23 13] [ 4 24 14] [ 5 25 15] [ 6 26 5] [ 7 27 4] [ 8 28 3] [ 9 29 2] [10 30 1]] newNumpyArraySort: [[ 1 11 21] [42 52 62] [ 3 13 23] [ 4 14 24] [ 5 15 25] [ 5 6 26] [ 4 7 27] [ 3 8 28] [ 2 9 29] [ 1 10 30]]