# Top 5 metrics for evaluating regression models

In my previous posts, I have covered some regression models (simple linear regression, polynomial regression) and classification models (k-nearest neighbors, support vector machines). However, I haven’t really discussed in-depth different ways to evaluate these models. Without proper metrics, not only can you not claim the accuracy of your models confidently but you also cannot compare different models to pick the most accurate one.

In this post, I want to focus on some of the most popular metrics that are used to evaluate regression models. These metrics are (in no particular order):

• Explained Variance Score (EVS)
• Mean Absolute Error (MAE)
• Mean Squared Error (MSE)
• R Squared Score (R2 Score)

These metrics were calculated in my post (except for adjusted R2 score) about implementing polynomial regression model.

# Implementing Support Vector Machine (SVM) algorithm in python

As you have probably noticed by now, there are several machine learning algorithms available at your disposal. In my previous post, I covered a very popular classification algorithm called K-Nearest Neighbors. In today’s post, I will cover another very common and powerful classification algorithm called Support Vector Machine (SVM).

### What is SVM and how does it work?

Just like KNN, SVM is a supervised learning model which means that it learns from the training set that we feed it. It can be used for both classification and regression problems but it’s mostly used for classification. In this post, we will focus on using SVM for classification.

SVM consists of picking support vectors and then using them to define a decision boundary for classifying features into different classes. The decision boundary is more formally known as hyperplane. Points on different sides of the plane belong to different classes. However, different sets of points can be segregated by numerous hyperplanes so how do you decide which hyperplane to select? That’s where the support vectors come into the picture.

# Implementing k-nearest neighbors in python

Last time, we looked into one of the simplest classification algorithms in machine learning called binomial logistic regression. In this post, I am going to cover another common classification algorithm called K Nearest Neighbors, otherwise known as KNN.

To recap, we have mostly discussed regression models such as simple and multivariate linear regression and polynomial regression which are used for predicting a quantity. On the other hand, classification models are used for predicting a category such as yes/no, will buy car/scooter/truck, will turn pink/green/red etc.

# Parsing command line arguments in python

Python is a very popular language for many reasons and one of them is the ability to use it for quick scripting or for an enterprise application. Professionally, I have used python for writing many scripts; some that are quick and temporary, and others that are more complex and long-term.

Whatever the purpose of the script, most of them start with parsing command line arguments. It’s always a good idea to allow users to customize the behavior of script by passing in different values for command line arguments. For example, if you have a script that runs some data quality checks against a database, you might want to pass an argument that decides for which day to run the checks.

So, how do you parse command line arguments in python? Python has a built-in module called argparse that does the job for you.

# Why documentation is more important than code

Taking a break from the machine learning heavy posts, I would like to talk about something slightly different but very important: documentation. Which is more important: code or documentation? If asked, most developers would say that code is more important than documentation. As a developer, you are given some business requirements and are asked to deliver a solution. That solution is your code. It works and solves the problem. Done!

There is nothing wrong with this argument. At the end of the day, code gets the job done. And if you’re on a tight schedule such as trying to fix a production issue then yes, your code is way more important. But that’s not the scenario I am talking about. I am talking about most of the development that gets done where developers are given sufficient amount of time. That’s when you must document your code. Whether that be through comments or separately on a wiki page, it is your responsibility as a developer to add that additional information for your team.

# Implementing a Binomial Logistic Regression model in Python

So far, we have only discussed regression modelling. However, there is another type of modelling called classification modelling. The primary difference between regression models and classification models is that while regression models are used to predict a quantity, classification models are used to predict a category.

For example, in my post on simple linear regression, we tried to predict soda sales through day’s temperature. Total sales of soda (our label) is a quantitative value and hence we used a regression model. In the example today, we are going to predict whether someone will purchase soda or not by looking at day’s temperature. Here we have two categories, whether customer will purchase or not purchase soda. This makes our label (dependent variable) categorical and suitable for logistic regression.As there were different variations of linear regression model, we also have different types of logistic regression model.

# 2017: Year in Review

And just like that, 2017 is almost gone. Last year, around this time, I wrote a post through which I reflected what I did or did not accomplish in 2016. It is now time to do the same for 2017. A lot happened in 2017 and I would like to take a step back and reflect to make sure I am still pursuing my goals.

Let’s begin!

### I started learning AWS

There are a lot of options out there these days when it comes to cloud computing such as Azure, AWS, Google Cloud etc. I decided to pick AWS and learn the basics. There is no doubt that cloud is the way to go. 5-10 years from now, most companies will not have an infrastructure department. It will be provided by third party companies as a service. As far as I am concerned, I am interested in spinning up some EC2 instances on AWS, using S3/Glacier to store files and storing data in an AWS database such as RedShift.

# Implementing a Polynomial Regression Model in Python

So far, we have looked at two types of linear regression models and how to implement them in python using scikit-learn. To recap, we began with a simple linear regression (SLR) model where we have one independent variable (feature) and one dependent variable (label). We then expended it slightly to a more general use case where we had multiple independent variables and one dependent variable. We called it multivariate linear regression model.

Both of these models result in a straight line or plane (if in multiple dimensions) which is very convenient but a bit too simplistic in the real world. Most real world problems cannot be easily modeled by a simple or multivariate linear regression model. For them, you need a non-linear model such as a polynomial regression model.

A polynomial regression model can be represented by an equation of this form:

Polynomial regression model is a type of linear regression model which can be confusing to some. The reason is that while the model is nonlinear, the regression function that is used to estimate the coefficients is linear. In fact, polynomial regression is a special case of multivariate linear regression.

### How can I implement polynomial regression model?

Implementing a polynomial regression model is slightly different than implementing a simple or multivariate linear regression model. You still use the linear regression model but before you do that, you have to construct polynomial features of your coefficients.

Here are the steps we are going to follow as usual:

• Exploring the dataset
• Splitting the dataset into training and testing set
• Building the model
• Evaluating the model

# Analyzing NYC motor vehicle data in Spark

A while back I wrote about analyzing NYC’s traffic (motor vehicle) data in q/kdb+. Then, soon afterwards, I showed how to analyze that data in python using pandas library. Now, I would like to again analyze the same dataset but this time, in Apache Spark. As I mentioned in my last post, I am currently learning Spark so you will be seeing a lot more posts about it in the near future.

If you don’t have Spark installed, please see my previous post on how to set it up on AWS.

In this post, I will show you how to :

• Load data from a csv
• Transform dataframe
• Aggregating data
• Sorting data
• Filter data

# Setting up Apache Spark on an AWS EC2 instance

I am currently learning Apache Spark and how to use it for in-memory analytics as well as machine learning (ML). Scikit-learn is a great library for ML but when you want to deploy an ML model in prod to analyze billions of rows (‘big data’), you want to be working with some technology or framework such as hadoop that supports distributed computing.

Apache Spark is an open-source engine built on top of hadoop and provides significant improvement over just native hadoop MapReduce operations due to its support for in-memory computing. Spark also has a very nice api available in scala, java, python and R which makes it easy to use. Of course, I will be focusing on python since that’s the language I am most familiar with.

Moreover, when working with a distributed computing system, you want to make sure that it’s running on some cloud system such as AWS, Azure or Google Cloud which would allow you to scale your cluster flexibly. For example, if you had to quickly analyze billions of rows, you can spin up a bunch of EC2 instances with spark running and run your ML models on the cluster. After you are done, you can easily terminate your session.

In this blog post, I will be showing you how to spin up a free instance of AWS Elastic Compute Cloud (EC2) and install Spark on it. Let’s get started!