Everything You Need To Know To Get Started With NumPy (Part 3)

Everything You Need To Know To Get Started With NumPy (Part 3)

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:

https://morioh.com/p/8ba92cdf6221

https://morioh.com/p/d20fa94171b5

Topic Discussed

  1. Arithmetic Operations in Numpy Arrays
  2. Operations with a Scalar Value
  3. Mathematical Functions Available in Numpy
  4. Numpy doesn’t show an error when you are wrong
  5. The dot product of arrays
  6. Joining Numpy Arrays
  7. Sorting of Numpy Arrays
  8. 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.

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.

numpy python machine-learning programming developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Machine Learning Developers in India

We supply you with world class machine learning experts / ML Developers with years of domain experience who can add more value to your business.

Hire Python Developers

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

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.

Hire Python Developers India

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...

Python Tricks Every Developer Should Know

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.