3 functions that will make you better at NumPy

NumPy is clearly one of the most useful libraries in Python.

Knowingly or unknowingly, you might be using NumPy arrays and operations directly or indirectly.

Let’s go over 3 of its many useful functions:

  1. where() returns the indexes of the elements of an np array where the filter condition is satisfied.

You can also give arguments to replace elements which satisfy and don’t satisfy the condition with certain values.

  1. intersect1d() returns the common elements or the intersection of 2 np arrays. Just like intersection of sets.

You can also extend it to more than 2 arrays by using the reduce function from functools!

  1. allclose() returns true if 2 np arrays have elements which are not exactly equal, but are equal given a certain tolerance level.

This comes in very handy when you have many values very close to each other but technically not equal.

Allclose, with tolerance value specified, will return true if the values are equal ± tolerance value.

Another similar function isclose() does the same thing but for single values rather than complete arrays.

#python #datascience #machinelearning