Prev: Array operations | Next: -
# adding scalar to array
import numpy as np
ar = np.arange(0,20,2)
x = 10
print(np.add(ar, x))
# adding arrays
import numpy as np
ar = np.arange(0,20,2)
br = np.arange(10)
print(ar)
print(br)
asum = np.add(ar, br) # The add ufunc operates on ar and br arrays
print(asum)
import numpy as np
ar = np.array([i**4 for i in range(10)])
np.sqrt(ar)
import numpy as np
ar = np.array([np.random.randint(100) for i in range(9)]).reshape(3,3)
print(ar)
print('Min of the entire array: ', ar.min())
print('Max of the entire array: ', ar.max())
print('Index of min value in the array: ', ar.argmin())
print('Index of max value in the array: ', ar.argmax())
# Note that the indexes returned above are into the flattened array
- '0': down each column
- '1': across each row
import numpy as np
ar = np.array([np.random.randint(100) for i in range(9)]).reshape(3,3)
print(ar)
print("Mins down each column: ", ar.min(axis = 0), 'and indexes: ',ar.argmin(axis = 0))
print("Maxs down each column: ", ar.max(axis = 0), 'and indexes: ',ar.argmax(axis = 0))
print("Mins across each row: ", ar.min(axis = 1), 'and indexes: ',ar.argmin(axis = 1))
print("Maxs across each row: ", ar.max(axis = 1), 'and indexes: ',ar.argmax(axis = 1))
- maximum(): being a ufunc, maximum() performs an element-by-element comparison of two or more arrays, selecting the larger member item in the item-pairs examined.
- max(): by contrast, max() applies on one specific array and returns the greatest value to be found in this array only.
import numpy as np
ar = np.array([np.random.randint(100) for i in range(9)]).reshape(3,3)
br = np.array([np.random.randint(100) for i in range(9)]).reshape(3,3)
print(ar,2*'\n',br,2*'\n')
print(np.maximum(ar,br),2*'\n',ar.max(),br.max())
. Free learning material
. See full copyright and disclaimer notice