Thrive in AI
Published in

Thrive in AI

Numpy explained- Language of Numpy

Numpy is the most important library for numerical processing in python. There are many great tutorials available that explain the details of functions of Numpy.

For learning Numpy basics I refer to Numpy official documentation and the following book.

https://jakevdp.github.io/PythonDataScienceHandbook/index.html

But many times you stumble upon complicated tasks in numpy where if you look for answers in stack overflow you will find them to belong formulas spanning multiple lines.

In this article, I explain how you can make complicated expressions using Numpy.

First, the most important thing to understand is python itself is much slower than Numpy. So you have to avoid python for loops and use vectorization and Numpy universal functions as much as possible to speed up your code.

The key to cracking complicated problems in numpy is to understand numpy as a programming language itself. Programming language can be broken into key components like loops, error handling, conditional statements etc. There are numpy functions for each of the components.

Numpy has more than 60 universal functions for arithmetic operations, logical comparisons, and other mathematical functions. For a detailed list of numpy universal functions you can refer to the link below:

Let's start with sum universal function. We can apply sum universal function across axis in the following way:

We can apply every universal across the axis of our choice by passing value to the axis parameter.

Except for the universal functions which take only one input like np.negative, every universal function supports the following five methods.

  1. ufunc.reduce(): Reduces array’s dimension by one, by applying ufunc along one axis.

The following function applies add function along with elements of the above array

2. ufunc.accumulate(): Accumulate the result of applying the operator to all elements.

The above function applies add function on rolling basis along elements of the array.

3. ufunc.reduceat(): Performs a (local) reduce with specified slices over a single axis.

Applies add function cumulatively on specified indices.

4. ufunc.outer(): Apply the ufunc op to all pairs (a, b) with a in A and b in B.

Performs additions of elements of arrays a and b for each of their element. The shape of array a is (13,1). The shape of array b is (4,1) so the above operation returns an array of size (13,4)

5. ufunc.at(): Performs unbuffered in place operation on operand ‘a’ for elements specified by ‘indices’.

above command adds 5 to array ‘a’ at indices specified by array a

Next, I show we can combine conditional operators with numpy universal functions.

  1. Conditional Operator on array

We can use conditional operators of python on numpy arrays

We can also do element-wise comparisons using python operators or numpy universal functions

We can also combine various conditions using logical and/or.

We can obtain the index of array where the condition is true by using nonzero function

We can use the output of nonzero function to obtain elements of the array for which the conditions are true

We can also implement if else type of functionality using np.where function. The following function replaces element of the array with 0 for which condition is not true.

2. Combining cumulative and conditional operator

We can combine conditional function along with reduce functions of numpy universal functions.

We can add elements for which conditions are true by the following function

We can obtain the cumulative sum of elements for which conditions are true

If we want to retain the value of array where the condition is not true then we can use .at method of universal functions. The following function will add 100 to array where the condition is true

Summary

Using numpy functions as shown above you can solve any complicated problem in numpy.

If you liked the article, please like and subscribe to my newsletter.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rohit Raj

Rohit Raj

75 Followers

Studied at IIT Madras and IIM Indore. Love Data Science