Machine learning as a service is real. Clean data with a well organized schema can be fed to cloud-based machine learning services with a decent ML model returned in less than 30 minutes. The resulting model can be used for inferring target values of new datasets in the form of batch or real-time predictions. All three public cloud vendors (AWS, Microsoft, Google) are competing in this space which makes the services cheap and easy to consume.
In our last discussion we ran the Kaggle red wine quality dataset through the Amazon Machine Learning service. The data was fed to AWS without any manipulation which AWS interpreted as a regression problem with a linear regression model returned. Each of the subjective wine quality ratings were treated as an integer from 0 (worse) to 10 (best) with a resulting model that could predict wine quality scores. Honestly, the results weren’t spectacular – we could have gotten similar results by just guessing the median value (6) every time and we almost would have scored just as well on our RMSE value.
Our goal was to demonstrate Amazon’s machine learning capabilities in solving a regression problem and was not to create the most accurate model. Linear regression may not be the best way to approach our Kaggle red wine quality dataset. A (somewhat) arbitrary judge’s score from 0-10 probably does not have a linear relationship with all of the wine’s chemical measurements.
What other options do we have to solve this problem using the Amazon Machine Learning service?
What is a machine learning binary classification problem? It’s one that tries to predict a yes/no or true/false answer – the outcome is binary. A dataset is labeled with the yes/no or true/false values. This dataset is used to create a model to predict yes/no or true/false values on new data that is unlabeled.
An example of binary classification is a medical test for a specific disease. Data is collected from a large group of patients who are known to have the disease and known not to have the disease. New patients can then be tested by collecting the same data points and feeding them to a model. The model will predict (with error rates) whether it believes the new patients have tehe disease.
Let’s explore how Amazon Machine Learning performs with a simple binary classification dataset.
Kaggle Red Wine Quality Dataset
We ran the Kaggle Red Wine Quality dataset through the Amazon machine learning regression algorithms in the last post. Why this dataset? Because it was clean data with a relatively simple objective – to predict the wine quality from its chemical measurements. We also had no data wrangling to perform – we simply uploaded the CSV to AWS and had our model created with an RMSE evaluation score ready to review.
This time we want to change things a bit. Rather than treat the 0-10 sensory quality ratings as integers, we want to turn the quality ratings into a binary rating. Good or bad. Great or not-so-great. This greatly simplifies our problem – rather than have such a wide swing of a ten point wine rating, we can simply categorize the wine as great and no-so-great. In order to do this we need to edit our dataset and change the quality ratings to a one (great) or a zero (not-so-great).
Data preparation – feature engineering
Go ahead and download the “winequality-red.csv” file from Kaggle. Open up the .CSV file as a spreadsheet. We need to replace the 0-10 quality scores with a 1 (great) or 0 (not-so-great). Let’s assume most wines in this dataset are fairly average – rated a 5 or 6. The truly great wines are rated > 6 and the bad wines are < 5. So for the sake of this exercise, we’ll say wines rated 7 and up are great and wines rated 6 and under are no-so-great.
All we have to do is edit our CSV files with the new 0 or 1 categories, easy right? Well, kind of. The spreadsheet has ~1600 ratings and manually doing a search and replace is tedious and not easily repeatable. Most machine learning datasets aren’t coming from simple and small CVS files but rather from big datasets hosted in SQL/NoSQL databases, object stores, or even distributed filesystems like HDFS. Manual editing often won’t work and definitely won’t scale for larger problems.
Most data scientists will spend a decent amount of time manipulating and cleaning up datasets with tools that utilize some type of high level programming language. Jupyter notebooks are a popular tool and can support your programming language of choice. Jupyter notebook are much more efficient for data wrangling using code instead of working manually with spreadsheets. Amazon even hosts cloud-base Jupyter notebooks within Amazon Sagemaker.
Converting the wine ratings from 0-10 to a binary 0/ 1 is pretty easy in Python. Just open the CSV file, test if each quality rating is a 7 or higher (> 6.5) and convert the true/false to an integer by multiplying by 1. Then we’ll write out the changes to a new CSV file which we’ll use for our new datasource.
import pandas as pd wine = pd.read_csv('winequality-red.csv') wine['quality'] = (wine['quality'] > 6.5)*1 wine.to_csv('binary.wine.csv')
Running the binary classification dataset through Amazon Machine Learning
Our dataset now has a binary wine rating – 1 for great wine and 0 for no-so-great wine, Upload this new CSV file to an AWS S3 bucket that we will use for machine learning.
The process to create a dataset, model, and evaluation of the model is the same for binary classification as we documented in the blog post about linear regression. Create a new datasource and ML model from the main dashboard and point the datasource to the S3 bucket with our new binary CSV file. The wizard will verify the schema and confirm that our file is clean.
What is different from linear regression is when we look at the schema, we want to make sure the ‘quality’ column is a ‘binary’ type rather than a ‘numerical’ type. All other values are numerical, only the quality rating is binary. This should be the default behavior but best to double check. Also select “yes” to show that your first line in the CSV file is your column name to remove this name from the model.
After finishing the schema, select your target prediction value which is the ‘quality’ column. Continue through the rest of the wizard and accept all the default values. Return to the machine learning dashboard and wait for the model to complete and get evaluated.
How did we do?
If all went well, we should have a completed model and evaluation of that model with no failures in our dashboard. Let’s check out the results by opening the evaluation.
Looking at our evaluation summary, things look pretty good. We have a summary saying our ML model’s quality score was measured with a AUC score of .778. That summary is somewhat vague so lets click “explore model performance”.
By default the wizard saves 30% of our dataset for evaluation so we can measure the accuracy of our model’s predictions. Binary classification algorithms are measured with an AUC or Area Under the Curve score. The measurement is a value between 0 and 1 with 1 being a perfect model that predicts 100% of the values correctly.
Our score shows our model got 83% of our predictions correct and 17% incorrect. Not bad! What is nice about this type of scoring is we can also see our false positives (not-so-great wine classified as great) and false negatives (great wine that was predicted as no-so-great). Specifically our model had 55 false positives and 25 false negative.
Predicting quality wine is not exactly a matter of life and death. Which means we aren’t necessarily concerned with false positives or false negatives as long as we have a decent prediction model. But for other binary classification problems we may want to adjust our model to avoid false positives or false negatives. This adjustment is made using the slider on the model performance screen shown above.
The adjustments come at a cost – if we want less false positives (bad wine predicted as great) then we’ll have more false negatives (great wine that accidentally predicted as bad). The reverse is also true, if we want less false negatives (great wine predicted as bad), we will have more false positives (bad wine predicted as great).
AWS uses specific learning algorithms for solving the three types of machine learning problems supported. For binary classification, Amazon ML uses logistic regression which is a logistic loss function plus SGD. Most beginners won’t mind these limitations but if we want to use other algorithms we’ll have to look at more advanced services like Amazon SageMaker.
Machine learning as a service is easy, fast, and cheap. By using the AWS black box approach we built a working binary classification model in less than 30 minutes with minimal coding and only a high level knowledge of machine learning.
Thanks for reading!