Complete Guide to Numpy for Beginners — Part 3
This is my 3rd and final blog post on NumPy in which I will be discussing operations, joining, splitting, and filtering of arrays and also about different math functions available in NumPy.

If you haven’t checked out my first 2 blog posts on Numpy discussing initializing a NumPy array, indexing, and basic functions available. Then check out the link below:
http://www.letsdiscussstuff.in/complete-guide-to-numpy-for-beginners-part-1/
http://www.letsdiscussstuff.in/complete-guide-to-numpy-for-beginners-part-2/

Topic Discussed
- Arithmetic Operations in Numpy Arrays
- Operations with a Scalar Value
- Mathematical Functions Available in Numpy
- Numpy doesn’t show an error when you are wrong
- The dot product of arrays
- Joining Numpy Arrays
- Sorting of Numpy Arrays
- Filtering Values from Arrays
Arithmetic Operations in Numpy Arrays
You can perform all basic arithmetic operations like addition, subtraction, multiplication, and division with NumPy arrays.
+, -, * and \ are used to do this.
import numpy as np
arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
arr3 = np.array([7,8,9,10])
arr1 + arr2
array([5, 7, 9])
arr1 - arr2
array([-3, -3, -3])
arr1 * arr2
array([ 4, 10, 18])
arr1 / arr2
array([0.25, 0.4 , 0.5 ])
For using these operators, the arrays should be of the same shape, as the operations are done elementwise. As you can see the first element of arr1 + arr2 is 5, which is 1+4.
Trending AI Articles:
1. Fundamentals of AI, ML and Deep Learning for Product Managers
3. Graph Neural Network for 3D Object Detection in a Point Cloud
4. Know the biggest Notable difference between AI vs. Machine Learning
If you use, arrays of different sizes then it will result in an ValueError.
arr1 + arr3
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-9-9cd496795bf2> in <module>()
----> 1 arr1 + arr3
ValueError: operands could not be broadcast together with shapes (3,) (4,)
Instead of using the operators, you can also use add(), subtract() , multiply() and divide() functions to do the same operation as before. You need to pass the arrays as arguments to the function.
print(np.add(arr1,arr2))
print(np.subtract(arr1,arr2))
print(np.multiply(arr1,arr2))
print(np.divide(arr1,arr2))
[5 7 9]
[-3 -3 -3]
[ 4 10 18]
[0.25 0.4 0.5 ]
You can also perform these operations for only a selected group of elements in an array. Like if you want to add only the first 3 elements of the arrays, then you can use arr1[:3] + arr2[:3]
arr1 = np.array([1,2,3,4,5])
arr2 = np.array([2,3,4,5,6,7,8])
print(arr1[:3] + arr2[:3])
[3 5 7]
See that in this case, the arrays need not be of the same size, but the portions of the array you are adding must be of the same size.
Operations with a Scalar Value
You can add, subtract, multiply, or divide an array with a scalar value. It will perform the operation will all the elements that are present in the array.
arr1 = np.array([1,2,3,4,5])
print(arr1 + 10)
print(arr1 - 10)
print(arr1 * 10)
print(arr1 / 10)
[11 12 13 14 15]
[-9 -8 -7 -6 -5]
[10 20 30 40 50]
[0.1 0.2 0.3 0.4 0.5]
Mathematical Functions Available in Numpy
There are several functions available to find the sine of the values, exponent of the values in the NumPy array. If you use any of the functions, then the operations will be performed on the entire array.
np.sin() – gives the sine of the values in the array.,
import numpy as np
arr = np.array([1,2,3])
np.sin(arr)
array([0.84147098, 0.90929743, 0.14112001])
np.cos() – gives the cosine of the values in the array
np.tan() – gives the tangential value of the elements in the array
Similarly, functions are available for other trigonometric operations too — arcsinh(), arccosh() and arctanh() which will output the inverse hyperbolic values of the elements.
print(np.cos(arr))
print(np.tan(arr))
[ 0.54030231 -0.41614684 -0.9899925 ]
[ 1.55740772 -2.18503986 -0.14254654]
np.exp() – gives the exponential value of the elements in the array.
np.log() – gives the logarithmic value of the elements in the array.
print(np.exp(arr))
print(np.log(arr))
[ 2.71828183 7.3890561 20.08553692]
[0. 0.69314718 1.09861229]
np.sqrt() – gives the square root of the elements in the array
print(np.sqrt(arr))
[1. 1.41421356 1.73205081]
Numpy doesn’t show an error when you are wrong
In some cases, NumPy will just show warnings and not produce an error, if you pass arguments that do not match with the function. Like in np.log() function, if you pass zero0 which should never be passed into a log function, then it will just show a warning and will not show any error, as it will stop the whole program from running.
print(np.log(0))
-inf
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: RuntimeWarning: divide by zero encountered in log
"""Entry point for launching an IPython kernel.
Dot Product of Numpy Arrays
You know that dot products element wise multiplication of 2 arrays and then adding them to get the final output. You can do it by using np.dot() function in NumPy.
arr1 = [1,2,3]
arr2 = [4,5,6]
print(np.dot(arr1,arr2))
32
Joining Numpy Arrays
np.concatenate() the function is used to join 2 arrays. The second array will be joined at the end of the first array if you pass axis=0.
arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
arr3 = np.concatenate((arr1,arr2),axis=0)
arr3
array([1, 2, 3, 4, 5, 6])
To join 2d arrays, along the rows, then you need to use axis=1.
arr1 = np.array([[1,2],[3,0]])
arr2 = np.array([[0,7],[2,1]])
arr3 = np.concatenate((arr1,arr2),axis=1)
arr3
array([[1, 2, 0, 7],
[3, 0, 2, 1]])
For joining them along the column, use axis=0.
arr3 = np.concatenate((arr1,arr2),axis=0)
arr3
array([[1, 2],
[3, 0],
[0, 7],
[2, 1]])
You can also use stacking to join arrays. hstack() is used to stack along rows and vstack() is used to stack along columns. Pass the arrays you need to stack as arguments to the function.
arr1 = np.array([[1,2],[3,0]])
arr2 = np.array([[0,7],[2,1]])
arr3 = np.vstack((arr1,arr2))
arr4 = np.hstack((arr1,arr2))
print(arr3)
print(" ")
print(arr4)
[[1 2]
[3 0]
[0 7]
[2 1]]
[[1 2 0 7]
[3 0 2 1]]
Sorting Arrays in Numpy
sort() the function is used to sort arrays in NumPy.
arr1 = np.array([1,2,5,6,7,3,4])
arr1.sort()
arr1
array([1, 2, 3, 4, 5, 6, 7])
There is no separate operation for sorting an array in descending order. But you can use arr1[::-1].sort() to sort in descending order.
arr1 = np.array([1,2,5,6,7,3,4])
arr1[::-1].sort()
arr1
array([7, 6, 5, 4, 3, 2, 1])
In the case of 2-D arrays, the array will be sorted row-wise.
arr1 = np.array([[1,2,6,5],[7,3,4,8]])
arr1.sort()
arr1
array([[1, 2, 5, 6],
[3, 4, 7, 8]])
For alphabetical values, the array will be started alphabetically in a lexicographical manner.
arr1 = np.array(['sheep','apple','bat','dog','camel'])
arr1.sort()
arr1
array(['apple', 'bat', 'camel', 'dog', 'sheep'], dtype='<U5')
Filtering Values from Arrays
First, check whether the elements in the array are following a certain condition and store the boolean values in a separate array.
arr1 = np.array([1,2,3,5,4,8,7])
filter=[]
for i in arr1:
if i>=4:
filter.append(True)
else:
filter.append(False)
Pass this filter into the array, to get only the required values.
arr1[filter]
array([5, 4, 8, 7])
You can also pass the function directly into the array to get the final output.
print(arr1[arr1>=4])
[5 4 8 7]
See that both of them give the same output.
That’s it we have come to the end of the blog post. Thanks for reading through my blog post series. If you like my blog post, make sure you give it a like and subscribe to my website — Let’s Discuss Stuff if you want to get notified for more content like this.
Next Series: Complete Guide to Seaborn for Beginners
Don’t forget to give us your ? !



Complete Guide to Numpy for Beginners — Part 3 was originally published in Becoming Human: Artificial Intelligence Magazine on Medium, where people are continuing the conversation by highlighting and responding to this story.
