Prev: Array operations       |       Next: -

# Universal functions¶

• A universal function (ufunc) is a function that operates on ndarrays in an element-wise fashion (that is, on each ndarray member item one by one).
• In other words, a ufunc implements a vectorized version of a typical function that would iterate over an array, taking a fixed number of scalar inputs and producing a fixed number of scalar outputs.
• See some ufunc examples below
In [1]:
# adding scalar to array
import numpy as np

ar = np.arange(0,20,2)
x = 10

[10 12 14 16 18 20 22 24 26 28]

In [2]:
# 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)

[ 0  2  4  6  8 10 12 14 16 18]
[0 1 2 3 4 5 6 7 8 9]
[ 0  3  6  9 12 15 18 21 24 27]

• Using ufuncs makes obsolete the use of loop in your code
In [3]:
import numpy as np

ar = np.array([i**4 for i in range(10)])
np.sqrt(ar)

Out[3]:
array([  0.,   1.,   4.,   9.,  16.,  25.,  36.,  49.,  64.,  81.])

## Normal functions¶

• Numpy is also equipped with various functions (for example statistical functions) that operate either on the entire array or on discrete dimensions (columns/rows). These functions, of course, are not considered 'ufuncs'.
• See the examples below for the min(), max(), argmin() and argmax() functions
In [4]:
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

[[86 32 50]
[65 12 99]
[92 18 70]]
Min of the entire array:  12
Max of the entire array:  99
Index of min value in the array:  4
Index of max value in the array:  5


## Axis¶

• When we want to apply a statistical operation on a specific column/row we need to assign a value 0 or 1 to the 'axis' argument. The meaning of the specific value is:
• '0': down each column
• '1': across each row
• Thus calculations of the min, max, etc. will be done for the specified dimension. Note that each of them returns an ndarry with the requested values
In [5]:
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))

[[48 75 66]
[40 31 83]
[92 70  2]]
Mins down each column:  [40 31  2] and indexes:  [1 1 2]
Maxs down each column:  [92 75 83] and indexes:  [2 0 1]
Mins across each row:  [48 31  2] and indexes:  [0 1 2]
Maxs across each row:  [75 83 92] and indexes:  [1 2 0]


## Difference between a ufunc and a normal function¶

• To better explain this difference we provide the example below comparing the ufunc 'maximum()' and the normal statistical function 'max()'. The behavior of maximum() is different than that of max():
• 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.
In [6]:
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())

[[87 69 86]
[ 1 44 23]
[81 56 41]]

[[96 52 84]
[27 56 69]
[91 85 73]]

[[96 69 86]
[27 56 69]
[91 85 73]]

87 96