8 machine learning algorithms in Python explained in 8 minutes
Machine learning is undoubtedly on the rise, slowly climbing into buzzword territory. This is in large part due to misuse and a simple misunderstanding of the topics that come with the term. Take a quick glance at the chart below and you’ll see this illustrated quite clearly, thanks to Google Trends’ analysis of interest in the term over the last few years.

Interest in machine learning over time.
However, the goal of this article is not to simply reflect on the popularity of machine learning. It is rather to explain and implement relevant machine learning algorithms in a clear and concise way. If I am successful, then you will walk away with a better understanding of the algorithms or, at the very least, some code to get you started when you try them out for yourself.
The breakdown
I will be covering a total of eight different machine learning algorithms. Feel free to jump around or skip an algorithm if you’ve got it down. Use this guide however your heart desires. So, without further ado, here’s how it’s broken down:
- Linear regression
- Logistic regression
- Decision trees
- Support vector machines
- K-nearest neighbors
- Random forests
- K-means clustering
- Principal components analysis
Housekeeping
I’m including this because trying to utilize someone else’s code only to find that you need three new packages and that the code was run in an older version of your language is incredibly frustrating.
So, in the interest of making both our lives easier, I am using Python 3.5.2. Below are the packages I imported prior to these exercises. I also took my sample data from the Diabetes and Iris datasets within the UCI Machine Learning Repository. Lastly, if you want to skip all this and just see all the code, feel free to give it a look on Github.
import pandas as pd import matplotlib.pyplot as plt import numpy as np import seaborn as sns %matplotlib inline
Linear regression
This is perhaps the most popular machine learning algorithm out there and definitely the most underappreciated. Many data scientists have a tendency to forget that simple is almost always preferred over complex when it comes to performance.
Linear regression is a supervised learning algorithm that predicts an outcome based on continuous features. It is versatile in the sense that it can be run on a single variable (simple linear regression) or on many features (multiple linear regression). It works by assigning optimal weights to the variables in order to create a line (ax + b) that will be used to predict an output. Check out the video below for a more thorough explanation.
Now that you’ve got a grasp of the concepts behind linear regression, let’s go ahead and implement it in Python.
Getting started
from sklearn import linear_model df = pd.read_csv(‘linear_regression_df.csv’) df.columns = [‘X’, ‘Y’] df.head()
Logistic regression
Decision trees
Support vector machines
K-nearest neighbors
Random forests
K-means clustering
Principal components analysis
Wrapping up
Stay updated on the go with our mobile app.
Get latest insights with smoother, more personalized experience through TIA mobile app.






