Tired of ads? Enjoy an ad-free experience by signing up.
Conor Dewey · · 8 min read

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 ahead in Asia’s tech landscape

You've reached your 2 free content limit for the month. Sign up for free to read the full story.

🏄 For casual readers / 👶 Free

Basic

US$0

Free forever

Get instant access to this article and more every month

0 premium content

Unlimited news briefs

5

5 articles

Ad-free reading experience

Just US$0 per day

⌛Sign up in 20s. No payment details needed.

📖 For learners / 👍 Starter

Lite

US$4.92/month

Billed annually at US$59/year

Get instant access to this article and more every month

4

4 premium content

Unlimited news briefs & articles

Ad-free reading experience

Just US$0.17 per day

Cancel anytime

Our subscriber community includes professionals from these companies:

Stay updated on the go with our mobile app.

Get latest insights with smoother, more personalized experience through TIA mobile app.

Community Writer

Conor Dewey

Data scientist. Aspiring entrepreneur. Hokie.