I will be discussing operations, joining, splitting, and filtering of arrays and also about different math functions available in NumPy. NumPy is a python library used for working with arrays. Numpy stands for Numerical Python. A simple beginner's tutorial that teaches you everything you need to know to get started with NumPy
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:
You can perform all basic arithmetic operations like addition, subtraction, multiplication, and division with NumPy arrays.
\ 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
If you use, arrays of different sizes then it will result in an
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
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.
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]
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 –
arctanh() which will output the inverse hyperbolic values of the elements.
[ 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.
[ 2.71828183 7.3890561 20.08553692] [0\. 0.69314718 1.09861229]
np.sqrt() – gives the square root of the elements in the array
[1\. 1.41421356 1.73205081]
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 zero
0 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.
-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.
We supply you with world class machine learning experts / ML Developers with years of domain experience who can add more value to your business.
Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, our full-stack Python development services...
How To Plot A Decision Boundary For Machine Learning Algorithms in Python, you will discover how to plot a decision surface for a classification machine learning algorithm.
Looking to build robust, scalable, and dynamic responsive websites and applications in Python? At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, we constantly endeavor to give you exactly what you need. If you need to...
In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.