April 2018
Beginner to intermediate
282 pages
6h 52m
English
Go to your Terminal and run the following command:
$ pip install line_profiler
Once installation is finished, you can save the preceding snippet to a filename knn_prediction.py.
As you have noticed, nearest_neighbors_prediction is decorated as follows:
@profiledef nearest_neighbors_prediction(x, data, labels, k): …
It allows line_profiler to know which function to profile. Run the following command to save the profile results:
$ kernprof -l knn_prediction.py
The output will be as follows:
StartWrote profile results to knn_prediction.py.lprof
You can view the profiler results as follows:
$ python -m line_profiler knn_prediction.py.lprofTimer unit: 1e-06 sTotal time: 0.001079 sFile: knn_prediction.py ...