Profiling NumPy code to understand the performance

There are couple of helpful libraries to monitor performance metrics of a given python script. You have already seen the usage of cProfile library. This section will introduce vprof which is visual profiler library.

It will provide you runtime statistics and memory utilization of a given python program.

1D clustering function from Chapter 5, Clustering Clients of Wholesale Distributor Using NumPy, will be used here and following code snippet should be saved to a file named to_be_profiled.py:

import numpy as npX = np.array([1,2,3,2,1,3,9,8,11,12,10,11,14,25,26,24,30,22,24,27])n_clusters = 3def Kmeans_1D(X, n_clusters, random_seed=442):  # Randomly choose random indexes as cluster centers rng ...

Get Mastering Numerical Computing with NumPy now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.