hacking-numpy

Vinay Kudari
hacking-datascience
2 min readJul 24, 2018

The core python packages like pandas, scikit-learn are built on top of numpy, So in this story I’m going to list some key functions of numpy package which made my computations much easier/efficient.

Will keep on appending new stuff to the story.

Get the ipython notebook of this story from here.

import numpy as np #importing the package

np.random.seed( )

generating random number’s is one of the major task of any data science problem, numpy does a very good job in this regard. There will be times when we want numpy to generate same set of random values on every invocation, In this scenario we could set a seed instead of copying the random array.

example

why?

when we want to check the performance of our model we often need to test on same data to get the accurate results, using seed will help save memory.

np.reshape(array, shape) / array.reshape(shape)

as the name says it resizes the given ndarray where shape = (rows, columns).

example

why?

reshape is especially useful when we want to reshape matrices.

np.nditer( )

it is an efficient multi dimension iterator object, it does iterate over every element in a ndarray.

example

why?

efficiently iterate over multi dimensional arrays.

np.flatnonzero( )

returns the indices which are non zero in the input array.

example

why?

selecting specific sequence in an array obeying required condition.

np.argsort( )

returns the index of each element of the input array after sorting.

example

why?

useful when we need to sort an array according to another array.

np.ufunc.at(input_array, indices, second_operand_ufunc)

applies ufunc(Universal Functions) at particular indices, the list of ufunc are available here

example

why?

it can used to apply mathematical operations to a part of array satisfying certain condition.

See you again 😃

--

--