NumPy is the foundational Python module for scientific applications. It is among the most widely used packages for statistical analysis in various areas, including data scientist, advanced analytics, and statistical analysis.
The following article will discuss the amazing function and things you can do with using NumPy.
1. allclose() function
A comparison function that checks whether two arrays are equal or pretty similar. This function compares two arrays of the data item by element to see if they are equal within a threshold, then returns a Boolean value as a result.
If two arrays’ items are not equal within a specified tolerance, it returns false; otherwise, it returns true. It’s worth noting that the tolerance levels by default are quite modest and favourable.
a = np.array([0.16,0.26,0.365])
b = np.array([0.15,0.25,0.36])
tolerance1 = 0.1
tolerance2 = 0.05
print(np.allclose(a,b,tolerance1))
print(np.allclose(a,b,tolerance2))
Output
True
False
2. where() function
Where() is a function that returns elements from an array that meets a set of criteria. In addition, this function returns the index position of elements that meet a set of criteria. This is essentially identical to the where clause in SQL.
list1 = np.array([1,4,7,6,1,7,3,8,9])
# Where the elements in list1 are less than 7, returns the index position
np.where(list1<7)
Output
(array([0, 1, 3, 4, 6]),)
3. extract() function
As the name implies, Extract() is used to retrieve specific members with an array depending on criteria. We may also use criteria like and, and or using extract().
Let’s define a random array.
# Random integers
array_test = np.random.randint(18, size=12)
#Output: array_test
array([ 2, 13, 16, 9, 2, 8, 12, 6, 6, 1, 15, 3])
Let’s make a boolean array from our array based on certain conditions.
# Divide by 2 and check if the remainder is 1
cond = np.mod(array_test, 2)==1
Output: cond
array([False, True, False, True, False, False, False, False, False, True, True, True])
Let’s see how extract() can help us now.
# Use extract to get the values
np.extract(cond, array_test)
Output
array([13, 9, 1, 15, 3])
We can also apply conditions directly in the extract function.
# Apply condition on extract directly
np.extract(((array_test < 2) | (array_test > 14)), array_test)
Output
array([16, 1, 15])
4. reshape() function
The reshape array function in NumPy is used to give an array a new form. It’s one of NumPy’s most popular functions. The reshape function is quite helpful; we may use it to give arrays a new form or the shape we desire.
It may increase or decrease the array dimension while keeping the total number of items the same.
arr = np.arange(1,36,2)
print(arr)
#reshape
print("reshape1n",arr.reshape(3,6))
print('reshape2n',arr.reshape(2,3,3))
Output
[1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35]
reshape1
[[ 1 3 5 7 9 11]
[ 13 15 17 19 21 23]
[25 27 29 31 33 35]]
reshape2
[[[ 1 3 5]
[ 7 9 11]
[ 13 15 17]]
[[19 21 23]
[25 27 29]
[31 33 35]]]
5. einsum() function
One of Numpy’s most useful functions is the einsum() function, commonly known as the Einstein summation convention. It can beat our conventional array functions in terms of performance and memory economy because of its expressive capability and clever loops.
The tough aspect is that learning notation takes time, and it might take several attempts to apply it effectively to complicated situations. The einsum() makes NumPy functions such as array np.multiply
, np.addition
, np.transpose
, and np.diag
available to speed up and simplify our work.
We’ll go through a few basic operations that we’ll need to know when we first start using einsum().
find array diagonal, transpose using einsum
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
a_diag = np.einsum('ii->i', a)
print("option1n",a_diag)
print('option2n',np.einsum(a,[0,0],[0]))

a_transpose = np.einsum("ij->ji",a)
print("option1n",a_transpose)
print("option2n",np.einsum(a,[1,0]))

6. clip() function
Clip() is a function that keeps values in an array inside a specified range. We sometimes need to keep the numbers inside a certain range. Outside values are trimmed to the interval edges when given an interval.
x = np.array([3, 4, 12, 20, 2, 2, 3, 7, 1, 2, 12, 0])
print(np.clip(x,2,5))
Output
array([3, 4, 5, 5, 2, 2, 3, 5, 2, 2, 5, 2])
Here is a book that I would definitely recommend for all Python & Data Science beginners. Must check it out!
Conclusion
Numpy is not simply a faster and more convenient Python array calculation package. In addition to its obvious scientific uses, NumPy may be used as multi-dimensional storage of ordinary data.
There is no limit to the number of data types that can be defined. As a result, NumPy can interact with a variety of databases with simplicity and efficiency.
The various amazing functions mentioned in the articles can be very useful while using the Numpy Library.
Note: This article contains an affiliate link. This means that if you click on it and choose to buy the resource I linked above, a small portion of your subscription fee will go to me.
However, the recommended resource is experienced by me and helped me in my data science career journey.
Before you go…
If you liked this article and want to stay tuned with more exciting articles on Python & Data Science – do consider becoming a medium member by clicking here https://pranjalai.medium.com/membership.
Please do consider signing up using my referral link. In this way, the portion of the membership fee goes to me, which motivates me to write more exciting stuff on Python and Data Science.
Also, feel free to subscribe to my free newsletter: Pranjal’s Newsletter.