from numpy import * arr = array([[10,20,30,40,50],[11,22,33,4,5]])print(‘Original array\n’,arr) print(‘\nTransposed array: ‘) print(arr.T) Output Original array[[10 20 30 40 50][11 22 33 4 5]] Transposed array: [[10 11][20 22][30 33][40 4][50 5]]
Monthly Archives: December 2019
Numpy amin
from numpy import * arr = array([[10,20,30,40,50],[11,22,33,4,5]])print(arr) print() print(‘Smallest values across columns: ‘,amin(arr,axis=0)) print() print(‘Smallest values across rows: ‘,amin(arr,axis=1)) Output [[10 20 30 40 50][11 22 33 4 5]] Smallest values across columns: [10 20 30 4 5] Smallest values across rows: [10 4]
Numpy amax
from numpy import * arr = array([[10,20,30,40,50],[11,22,33,4,5]])print(arr) print() print(‘Max values across columns: ‘,amax(arr,axis=0)) print() print(‘Max values across rows: ‘,amax(arr,axis=1)) Output [[10 20 30 40 50][11 22 33 4 5]] Max values across columns: [11 22 33 40 50] Max values across rows: [50 33]
Numpy argmax
Numpy argmax function
Numpy extract
import numpy as np x = np.array([[2,3,4,5],[12,23,44,25],[12,33,64,85]]) print(x) print() evencondition = np.mod(x,2)==0 print(np.extract(x,evencondition)) print() print(x[evencondition]) Output [[ 2 3 4 5][12 23 44 25][12 33 64 85]] [ True False True False True False True False True False True False] [ 2 4 12 44 12 64]
Numpy cumulative product
import numpy as np arr = np.array([[6, 1, 3], [8, 2, 1]]) print(‘2D Array\n’,arr) b = np.cumprod(arr, dtype=int) print(‘Cumulative product of all elements\n’,b) c = np.cumprod(arr,axis=0) print(‘Cumulative product of elements axis=0\n’,c) x = np.array([6, 1, 3]) print(‘Single dimension array ‘,x) y = np.cumprod(x, dtype=int) print(‘Single Dimension array Cumulative product of all elements\n’,y) Output 2D Array[[6Continue reading “Numpy cumulative product”
Numpy accumulate
import numpy as np x = np.arange(5,11) print(x) y = np.add.accumulate(x) print(‘Addition: ‘,y) print() x1 = np.arange(1,7) print(x1) y1=np.multiply.accumulate(x1) print(‘Multiplication: ‘,y1) Output [ 5 6 7 8 9 10]Addition: [ 5 11 18 26 35 45] [1 2 3 4 5 6]Multiplication: [ 1 2 6 24 120 720]
Numpy Concatenate
import numpy as np x = np.array([8,5,3,1,77,1]) y = np.array([6,71,56,11,23]) print(x)print(y) z=np.concatenate([x,y]) print()print(‘Concatenated array: ‘,z) Output [ 8 5 3 1 77 1][ 6 71 56 11 23] Concatenated array: [ 8 5 3 1 77 1 6 71 56 11 23]
Numpy reduce function
Numpy reduce function
Matrix aggregate functions
from numpy import * x = matrix([[11,22,33],[44,55,66],[1,2,3]]) print(‘Matrix: \n’,x) print() print(‘Largest Element: ‘,x.max()) print() print(‘Smallest Element: ‘,x.min()) print() print(‘Sum: ‘,x.sum()) print() print(‘Mean: ‘,x.mean()) Output Matrix: [[11 22 33][44 55 66][ 1 2 3]] Largest Element: 66 Smallest Element: 1 Sum: 237 Mean: 26.333333333333332