Code Snippets

The best Python 3 Machine Learning code snippets for your favourite Data Science tasks.

Find quick, concise, and clearly commented code snippets for your day to day Machine Learning tasks. Parameters are well defined and explained, and examples are given so you know just how to use them!

The Confusion Matrix

A fully explained code to get an aesthetically pleasing visualisation of the confusion matrix in Python to address the performance of classification algorithms. Find more about this evaluation method here: The Confusion Matrix in Python.

python confusion matrix

The ROC Curve

Learn to plot the ROC curve in Python and calculate the Area under the curve or AUC score with this code snippet in Python with all the needed parameters explained. You can learn about the ROC Curve with this article: The ROC Machine Learning evaluation method.

roc curve python

The Lift Curve

A unique Machine Learning code snippet: The fully detailed code to get an aesthetically pleasing visualisation of the Lift Curve in Python to address the performance of classification algorithms. Find more about this evaluation method here: The Lift Curve in Machine Learning.

lift curve code

MinMaxScaler in Python (Normalization)

MinMaxScaler Python code

Learn how to use the MinMaxScaler Python object from Scikit-Learn to scale the feature of your data with this easy and delightfully explained code snippet. Also, if you want, learn all about Feature Scaling for Machine learning with this other post!

Python Map, Filter & Reduce functions

Python map filter reduce

Learn how to use and the main differences between the Map Filter Reduce Python built-in functions with some great examples! These Python built-in functions are some of the most useful tools out there to have in your arsenal when working with iterable objects like arrays or sets. Check out our article!


We hope you found this section on Python Machine learning code snippets useful. In case you are looking for further material to learn Python check out our Python Books section, where you will find intro to Python books as well as advanced Python Material to cover all your needs and learn to code Python. Enjoy!

advanced python fundamentals

Thanks for reading How to Learn Machine learning, and have a fantastic day!