The world’s leading publication for data science, AI, and ML professionals.

Top 4 Numpy Functions You Don’t Know About (Probably)

Functions that saved me a lot of time and frustration

In today’s article, I want to discuss a couple of essential Numpy functions I use daily for data analysis purposes. It is not a comprehensive list by any means, but it will be enough to get you started, and I’m sure they will suit you well.

Photo by Nadine Shaabana on Unsplash
Photo by Nadine Shaabana on Unsplash

I can’t even stress how many times have I thought "What I need to implement is not so straightforward, there’s no chance a pre-built function exists". More often than not, I was wrong. Pure Numpy is so powerful, and I’ll take my chances that you’re not aware of everything it offers.

I’ve written before about some great lesser-known Pandas functions that will save you a huge amount of time, so make sure to check those out if you’re using Pandas daily:

Top 3 Pandas Functions You Don’t Know About (Probably)

So, without further ado, let’s dive in. The only import you’ll need is Numpy:


where()

The where() function will return elements from an array that satisfy a certain condition. Let’s explore it with an example.

I’ll declare an array of grades of some sort (really arbitrary):

You can now use where() to find, let’s say, all grades that are greater than 3:

Note how it returns the index position.

The party doesn’t stop here my friend, you can provide two additional parameters:

  • the first one will replace values that satisfy the given condition
  • the second one will, of course, replace those that don’t satisfy the condition

As I want to keep the article concise, I think that this will give you enough to keep exploring on your own.


argmin(), argmax(), argsort()

Okay, what’s with the fancy names?

Really nothing fancy goes on here, but those 3 functions will suit you greatly if you don’t feel like implementing the logic by yourself (hint: you shouldn’t).

The argmin() function will return the indices of the minimum values. On the same array we used before it will return the index position of the smallest value:

The argmax() will, you guessed it, do the opposite – return the indices of the maximum values:

One other nifty function is argsort(), and it will return the indices of a sorted array. There are so many situations when this might come in handy:


intersect1d()

What intersect1d() function will do is, it will return the intersection of 2 arrays – meaning the items that are common in both arrays. Unlike the previous functions, it won’t return the index position, it will return the actual values.

To start out, let’s declare two arrays that have some elements in common:

You can now use the mentioned function to find the elements in common:


allclose()

And last, but by no means least, is the allclose() function. It will return True if items in two arrays are equal within a tolerance. It will provide you with a great way of checking if two arrays are similar, which can in some cases be a pain in the bottom to implement manually.

To start, I’ll declare two lists, and will make sure the difference of respective items isn’t greater than 0.2 anywhere:

If I now call the allclose() function with a tolerance of 0.1, it should return False:

But if I decide to change the threshold to 0.2 for example, it should return True:


Before you go

So many times I’ve been (and sometimes still am) guilty of reinventing the wheel. It’s easy to think that your problem is unique and that no one has thought to make a function for solving it. While it might be true in some cases, more often than not you’ll feel like a complete idiot when you find a pre-made function for something you’ve been implementing manually for the last couple of hours.

Make sure you’re aware of the power of libraries you’re using, they are so popular for a reason.

Thanks for reading. Take care.


Related Articles