Skip to main content

Basic mathematical functions for Numpy Arrays-Part 1

In this post we are going to have a look at some of the mathematical functions which can be used with numpy arrays.Also there will be details regarding the syntax of these functions.There will be many mathematical functions to be used but this post will cover only the widely used functions.
Hello All,

This post is the first post of the tri series articles on the Mathematical functions for Numpy Arrays.We are going to take a look at some mathematical functions which can be used with the arrays. Also these functions will be very helpful while doing some common mathematical operations.

Following is the list of functions which we are going to take a look at:
1. Sum
2. Prod
3. Min
4. Argmin

Let us start going through the functions one at a time .

1.sum

Sum function is used for calculating the sum of all the elements inside the numpy array.We just need to call the function using the numpy array reference.The syntax for using the sum function is array_ref.sum().

Following code snippet specifies the same:

The first snippet declares the array and reshapes it into dimension of 5x4.In the next code snippet we calculate the sum of all the elements using the sum function and the result is 190.
2.prod
If we want to calculate the product of all the elements inside the arr_2 array we can use the prod function.The syntax to use the prod function is as follows: array_ref.prod().

Following code snippet specifies the code for the same:

The result of the prod function is negative because here the multiplication value tends to over shoot the range of the int data type.For further explanation you can refer these references:[1][2]

Next I have changed the contents of the arr_2 reference and also calculated the product to get the result value within the range of integer which is specified in the following code snippet:

3.min
For finding the minimum value inside the numpy array we can use the min() function. The min function returns minimum element as a return value.The syntax for using the min() function is as follows: array_ref.min()

Following code snippet specifies result of executing the min function on the arr_1 and arr_2.


4.argmin
Suppose if we want to get the index of the minimum value inside the array then we can use the argmin() function. The syntax for using the argmin() fuction is as follows: array_ref.argmin().

Following code snippet specifies the same:
In the code snippet specified above the argmin function contains an argument which specifies the axis value.Axis value specifies the direction to perform the given operation.Axis = 1 specifies the direction as Horizontal and Axis = 0 means the direction to perform the operation is Vertical.

If we just want to find the index of the minimum value over the entire array then we can refer the below code snippet:



Thank you.
That's all for this post!!
Thank you for reading this post.
If you have any suggestions regarding the post contents or if you need some more details on any other topic, please post it in the comments section.
Your suggestions are too valuable so they should not be missed.

 

References:
1.https://stackoverflow.com/questions/39089618/why-is-numpy-prod-incorrectly-returning-negative-results-or-0-for-my-long-li
2.https://stackoverflow.com/questions/1658714/how-to-get-the-range-of-valid-numpy-data-types/1658755#1658755
 

Comments

Popular posts from this blog

PyMuPDF vs PDFMiner

 As a developer , I was tasked to extract specific data from a PDF. Upon analysing it further, certain patterns were found based on keywords in the document. Since I was using Python language for the task I found 2 tools quite useful which are PyMuPDF and PDFMiner. These tools can then be used to extract the text from a page on which regular expression can be applied to further extract relevant data.     Next, we are going to take a deeper look into these tools, specifically focusing on the pros and cons of each.     PyMuPDF   Docs , PIP package Pros Simple and understandable API Extensive tools to work with text, images, and graphics Available as a PIP package (pip install PyMuPDF) Better support for a range of symbols comparer to PyPDF2   Cons Parsed text is not in sequence Dependency on other package-Fitz Text sequence information lost during extraction     PDFMiner   Docs ,  PIP package ...

Finding difference between 2 files in Python

In this post, we will take a look at how to compare two files using Python.   I was tasked to compare 2 files and then list the differences between them using Python. Initially, I started with filecmp module, but even with the function parameter ‘ shallow’ set to false, the Boolean result was not enough. Sure, it can act as an indicator to take some action, but it will not list the differences.   I was looking for something more visual, something like color coding and not like the git diff output, which is not very user-friendly. But, another Python internal module, difflib helped me to get the job done.   Inside Difflib, HtmlDiff is what I was looking for. The differences were highlighted with 3 different colors and also the line numbers were indicated in a table to locate the differences. The results are quite self-explanatory and it is easier to explain the differences to other people. Code for generating the above difference table: Note: File1...

Adding existing Anaconda environment to Jupyter notebook

In this post we are going to take a look at adding Anaconda environment to Jupyter notebook. Recently, I was working on a CSV file and wanted to work with Pandas package for tabular data manipulation using Python. The problem was even if I install Pandas package, I would have to install other Data Science package as needed. But, the Anaconda environment was already setup on my laptop, which I want to reuse.   Today, we will look into how to reuse the Anaconda environment within the Jupyter Notebook.   There are 4 basic steps to be followed for adding the environment: 1. Create a conda environment Go to Conda command prompt(Run in Admin mode) Run the following command: conda create –-name newenv O/P:   What if there is an existing conda environment? Go to Conda command prompt(No need for Admin mode) Run the following command: conda env list O/P: Since there was only one environment, only one entry was displayed. ‘*’ indicates the cur...