5 Interesting Numpy Functions
Detailed explanation of the functions and common errors.
5 Numpy functions
list of the chosen numpy functions.
- argmax
- sort
- average
- transpose
- split
Let’s begin by importing Numpy and listing out the functions covered in this story.
import numpy as np
# List of functions explained
function1 = np.argmax
function2 = np.sort
function3 = np.average
function4 = np.transpose
function5 = np.split
Function 1 — np.argmax
This function returns the maximum element indices based on axis.
# Example 1 - working
arr1=np.random.randint(16,size=(4,4))
print(arr1)
print(np.argmax(arr1))result:
[[ 1 6 12 13]
[13 5 0 4]
[11 10 5 0]
[ 1 9 7 6]]
3
Here,argmax returns indices of maximum element.
# Example 2 -working sort
prob=np.random.rand(4,4)
print(prob)
print(np.argmax(prob,axis=0))
print(np.argmax(prob,axis=1))Result:
[[0.57753095 0.84995965 0.23614245 0.8725152 ]
[0.29165751 0.060839 0.15813323 0.52923348]
[0.12625635 0.07084457 0.71222193 0.11301533]
[0.2303861 0.12119381 0.43109111 0.44414143]]
[0 0 2 0]
[3 3 2 3]
In the above Example argmax returns indices of maximum element in a particular axis if specified 0->maximum indices by column,1->maximum indices by row, if not specifies returns the maximum of entire array.
# Example 3 - breaking (to illustrate when it breaks)
#meanarr1 = [[5, 6, 7],
[8, 9, 10]]np.argmax(arr1, axis=2)
Error:
--------------------------------------------------------------------
AxisError Traceback (most recent call last)
/tmp/ipykernel_35/605736401.py in <module>
5 [8, 9, 10]]
6
----> 7 np.argmax(arr1, axis=2)<__array_function__ internals> in argmax(*args, **kwargs)/opt/conda/lib/python3.9/site-packages/numpy/core/fromnumeric.py in argmax(a, axis, out)
1191
1192 """
-> 1193 return _wrapfunc(a, 'argmax', axis=axis, out=out)
1194
1195 /opt/conda/lib/python3.9/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
53 bound = getattr(obj, method, None)
54 if bound is None:
---> 55 return _wrapit(obj, method, *args, **kwds)
56
57 try:/opt/conda/lib/python3.9/site-packages/numpy/core/fromnumeric.py in _wrapit(obj, method, *args, **kwds)
42 except AttributeError:
43 wrap = None
---> 44 result = getattr(asarray(obj), method)(*args, **kwds)
45 if wrap:
46 if not isinstance(result, mu.ndarray):AxisError: axis 2 is out of bounds for array of dimension 2
In the example (why it breaks and how to fix it): This example failed due to axis value it can only be 0 ,1,-1 not 2 so changing axis value fixes the error
Some closing comments about when to use this function. when ever need to find maximum index,could be maximum probablity of some use case
Function 2 — sort
to sort elements in an ordered sequence
# Example 1 - working
a = np.array([[12, 15], [10, 1]])
print(np.sort(a,kind='mergesort'))
In the example np.sort returns a copy of a sorted array using kind we can use different sorting algorithms(eg:quicksort,mergesort,heapsort)
# Example 2 - working
arr1 = np.sort(a, axis = 0) #along axis 0 sorts contents along each column
print(arr1)
print(np.sort(a, axis = -1)) #along last axis sorts contents along each rowResult:
[[10 1]
[12 15]]
[[12 15]
[ 1 10]]
Explanation :Axis along which to sort. If None, the array is flattened before sorting. The default is -1, which sorts along the last axis
# Example 3 - breaking (to illustrate when it breaks)
dtype = [('name', 'S10'), ('height', float), ('age', int)]
values = [('Arthur', 1.6, 41), ('Lancelot', 1.9, 38),
('Galahad', 1.4, 38)]
a = np.array(values)
np.sort(a,order=['name'])------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipykernel_35/3397319452.py in <module>
4 ('Galahad', 1.4, 38)]
5 a = np.array(values)
----> 6 np.sort(a,order=['name'])<__array_function__ internals> in sort(*args, **kwargs)/opt/conda/lib/python3.9/site-packages/numpy/core/fromnumeric.py in sort(a, axis, kind, order)
994 else:
995 a = asanyarray(a).copy(order="K")
--> 996 a.sort(axis=axis, kind=kind, order=order)
997 return a
998 ValueError: Cannot specify order when the array has no fields.
Explanation:(why it breaks and how to fix it) In the example in order to sort using order parameter we need to have fields so adding dtype in above example into np.array will resolve this a=np.array(values,dtype)
Function 3 — average
Average function calculates weighted average along specified axis
# Example 1 - working
data = np.arange(1, 11)
np.average(data, weights=np.arange(10, 0, -1))Result:
[10 9 8 7 6 5 4 3 2 1]
Explanation: In the above example First pass the parameters data and its corresponding weights to find weighted average
# Example 2 - working
data = np.arange(8).reshape((4,2))
print(data)
np.average(data, axis=1, weights=[1./4, 3./4])
Result:
[[0 1]
[2 3]
[4 5]
[6 7]]
array([0.75, 2.75, 4.75, 6.75])
Explanation: In the example using axis since array and its associated weights differ
# Example 3 - breaking (to illustrate when it breaks)
data = np.arange(6).reshape((3,2))
print(data)
np.average(data, weights=[1./4, 3./4])
Results:
[[0 1]
[2 3]
[4 5]]
-----------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_35/3459501547.py in <module>
2 data = np.arange(6).reshape((3,2))
3 print(data)
----> 4 np.average(data, weights=[1./4, 3./4])<__array_function__ internals> in average(*args, **kwargs)/opt/conda/lib/python3.9/site-packages/numpy/lib/function_base.py in average(a, axis, weights, returned)
391 if a.shape != wgt.shape:
392 if axis is None:
--> 393 raise TypeError(
394 "Axis must be specified when shapes of a and weights "
395 "differ.")TypeError: Axis must be specified when shapes of a and weights differ.
Explanation about example (why it breaks and how to fix it) In this scenario axis must be specified because with out it function will not get which sense of direction to work
Function 4 — Transpose
This function helps to return transpose of matrix(switches rows and columns of array)
# Example 1 - working
x=np.array([[1,2,3],[4,5,6]])
np.transpose(x)
Explanation about example we got the 3x2 matrix which is transpose of 2x3
# Example 2 - working
x = np.ones((1, 2, 3))
np.transpose(x, (1, 0, 2)).shape
Explanation about example Here in transpose function second argument is axes(tuple) which helps to permute the axes in a given order
# Example 3 - breaking (to illustrate when it breaks)
x = np.ones((1, 2, 3))
np.transpose(x, (1, 0, 2,3)).shape
--------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipykernel_35/2967030568.py in <module>
1 # Example 3 - breaking (to illustrate when it breaks)
2 x = np.ones((1, 2, 3))
----> 3 np.transpose(x, (1, 0, 2,3)).shape<__array_function__ internals> in transpose(*args, **kwargs)/opt/conda/lib/python3.9/site-packages/numpy/core/fromnumeric.py in transpose(a, axes)
656
657 """
--> 658 return _wrapfunc(a, 'transpose', axes)
659
660 /opt/conda/lib/python3.9/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
56
57 try:
---> 58 return bound(*args, **kwds)
59 except TypeError:
60 # A TypeError occurs if the object does have such a method in itsValueError: axes don't match array
Explanation about example (why it breaks and how to fix it), The second argument doesnot have right number of axes so the function doesnot get how to reshape Here reducing axis tuple fixes the issue.
Function 5 — Split
Splitting an array into multiple sub arrays
# Example 1 - working
x = np.arange(20.0)
np.split(x, 2)
Explanation about example This function helps to split the array into 2 sub arrays
# Example 2 - working
x = np.arange(8.0)
np.split(x, [3, 5, 6,10])
Explanation about example
In This example second argument the entries indicate where along axis array can split As we can see if the array don’t have 10 element split returns empty array
# Example 3 - breaking (to illustrate when it breaks)
x=np.arange(7)
np.split(x,3)
-------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipykernel_35/3892306710.py in <module>
1 # Example 3 - breaking (to illustrate when it breaks)
2 x=np.arange(7)
----> 3 np.split(x,3)<__array_function__ internals> in split(*args, **kwargs)/opt/conda/lib/python3.9/site-packages/numpy/lib/shape_base.py in split(ary, indices_or_sections, axis)
870 N = ary.shape[axis]
871 if N % sections:
--> 872 raise ValueError(
873 'array split does not result in an equal division') from None
874 return array_split(ary, indices_or_sections, axis)ValueError: array split does not result in an equal division
Explanation about example (why it breaks and how to fix it)
Here array needs to be split into 3 equal arrays but our array size doesn’t match so changing either array size or spilt size resolves the error
Conclusion
This article has gone over some basic examples of how to use five NumPy functions, as well as some frequent problems that can occur when using these functions for the first time. Even if some of the functions aren’t widely used in Data Analysis, they might be incredibly useful in solving specific situations.
Reference Links
Provide links to your references and other interesting articles about Numpy arrays:
- Numpy official tutorial : https://numpy.org/doc/stable/user/quickstart.html