Good Data and Machine Learning. Wield Data Correlation properly to help…
By: Soham Chatterjee
Tags: Data Analysis, Artificial Intelligence, Machine Learning, Data Visualization, Data Science

Deep Learning has become a hammer of sorts that can nail down almost any Machine Learning (ML) problem.

Deep learning is solving many problems that most other ML algorithms cannot. But many people in the ML field believe that it can solve any problem as long as you stack up enough layers and neurons.

Thinking this way is easy for a few reasons. Frameworks like Keras and TensorFlow are easily available. Amazon AWS and GCP offer cheap computational power. And the ML community is very open and supportive.

These days, anyone can take a few online courses or read a few papers. Then they can create, say, a convolutional neural network (CNN) to recognize Handwritten digits. And then call themselves an ML engineer.

Even I am guilty of doing the same, and I have done so with very little money and a laptop worth less than $500.

But people have forgotten the two most important parts of Machine Learning: Math and Data.

Machine Learning is, after all, Data Driven AI, and your model will be only as good or as bad as the data you have.

In general, you can’t have a dataset of car images and expect to use it to classify cats and dogs. You can’t use linear regression to train a model on a dataset that does not have a linear correlation.

In this article, I will focus on data and data correlation. Your data should drive your choice of an ML algorithm. Your choice shouldn’t depend on how state-of-the-art an algorithm is, but rather on your data set.

So how important is data?

The importance of data cannot be overstated. Here’s an example.

Energy Disaggregation uses ML to find the kind of electrical devices you might have in your home. It uses features like meter data, weather, locality etc.

When I was working on a project to do the this, my Neural Network couldn’t predict the electrical devices. No matter how wide or deep a network I made, I could hardly get an accuracy above 55%.

It took me a long time to realize that it wasn’t a problem with my model, but rather a problem with my data.

To do this project, I was using a dataset that I found on the Internet. The data was neither properly labeled, nor was it good. There were some very erratic power values that did not correspond to the weather data at the same time.

All this meant that no matter how good my model was, it could have never done its job as the data itself was bad!

What is good data?

It’s very hard to answer a question like that. I’ve learned not use data unless it’s from a verifiable source.

A verifiable source could be from open data sources like Kaggle or KDNuggets. Also, open source datasets from a company or a country. You can also use datasets that researchers have used in their work.

Sometimes getting the right data can be the hardest part of a project. This is especially true if you are trying to do something new.

For most of my ML projects, I had to get data from Ph.D. scholars and researchers. Do not hesitate to ask experts for data!

Even with verified data, you still need to check if it is good or not. Visualization is handy for this.

Visualization and Data

Data visualization lets you see what the data looks like. You can see if the features correspond to the output. It also helps you find the kind of data correlation you have.

Software packages like NumPy, Pandas and Matplotlib are great for visualization.

For example, the two images below show power usages of two homes for a 24 hour period.

The rise and fall of the curve in the first image shows the increase in power usage in the mornings and evenings. You can also see there is less power usage during the night.

Image 1: A good Electricity Power Consumption Graph

The second image has an erratic curve and “noisy” or irregular data.

Image 2: Irregular Electricity Power Consumption Data

If I were training a model to predict power usage, I’d use the data for the first image.

Data visualization helps you understand if the data you are using will help your purpose or not.

Data Correlation

Earlier in this post, I mentioned that you can’t use linear regression to model a nonlinear dataset.

The opposite is also true. If you have a linear correlated dataset you need a simple model like linear regression. Even the best CNN will give you a poor result.

Data correlation is the way in which one set of data may correspond to another set. In ML, think of how your features correspond with your output.

For example, the image below visualizes a dataset of brain size versus body size. Notice that as the body size increases, so does the brain size. This is known as a linear correlation. In a simple definition of linear correlation, the data follows a straight line.

Brain Weight Vs. Body Weight

Not all data is linearly correlated. The following image shows a curve of ice cream sales versus temperature. It has an inverted U shaped graph. Why?

It could mean that if it’s hot enough, people might not want to leave their homes to go buy ice cream. Or there could be another reason. So using Linear Regression doesn’t make sense for this dataset.

Ice Cream Sales vs. Temperature

It becomes very hard to figure out how data correlates if you have more than two features. Data visualization can help find how individual features may correlate with the output.

Filtering data

Not all data is relevant to your project.

A good example for this is the Kaggle Titanic competition. Kaggle is an online community of data scientists at all levels. They host ongoing competitions to help learn and practice data science.

In the Kaggle Titanic competition, you are asked to analyze which people are likely survivers. There are a lot of features in this challenge. You can also make many features by combining the features that are already present.

You can see the correlation of sex with survival, and age with survival. From these visualizations it becomes very obvious how sex and age played a very big role in deciding who might have survived aboard the Titanic.

Visualizations can also help you filter out useless features. For instance, there is no correlation between embarked port and survival. In this case, we leave it out.

Getting to the Math

Visualisation is great. But if you are someone who’d rather see numbers and stats, then there are other ways to find out how data correlates.

Pearson’s Correlation Coefficient helps you find out the relationship between two quantities. It gives you the measure of the strength of association between two variables. The value of Pearson’s Correlation Coefficient can be between -1 to +1.

1 means that they are highly correlated and 0 means no correlation. -1 means that there is a negative correlation. Think of it as an inverse proportion.

The t-test is a correlation coefficient testing for any correlation between two values.

Other popular correlation coefficients include

Spearman rank order correlation

Pearson’s Rank Correlation.

All these coefficients have advantages and drawbacks. It’s important to know when to use them.

Note that if you have a large dataset and if you get a small coefficient, say 0.4, then it’s not necessarily bad. The dataset might have a large statistically significant correlation.

Also note that correlation may not mean causation.

Because two variables are related, does not mean that one directly caused the other.

People aboard the Titanic did not die because they were male and aged 28. Rather, a large number of them died because officers were saving “Women and Children First”.

The importance of data correlation has an effect when you have a dataset with many features. It’s tempting to think that a larger number of features will help a model make better predictions. But that’s incorrect.

If you try to train a model on a set of features with no or very little correlation, you will get inaccurate results.

The age of the person who wrote a handwritten digit in MNIST could be a feature. But it wouldn’t help make better predictions.

When dealing with multidimensional datasets, it’s important to filter out non-correlated features. Instead, it’s better to use fewer highly correlated features to train a model.

Datasets with more features or higher dimensions is a recent problem. These days, data collection and storage has never been easier. More often than not, many datasets have features with similar information. This acts as noise in the system and increases complexity.

Some features also have very little variance. If your output has a lot of variance, then do you think a feature with a low variance will improve your model? NO! To figure out the importance of each feature in a dataset, we use rank correlation.

Rank correlation

Rank correlation is a technique that compares which features correlate with the output.

One popular rank correlation method in ML is the Principal Component Analysis. It’s a technique to find patterns in high dimensional data. Basically, it “will simplify data of higher dimensions into lower dimensions.” You can read more about it here.

With fewer dimensions, data visualisation also becomes easier.

Other powerful tools to find data correlation are Random Forests and Decision Trees. They work by finding out the statistical usages of each feature. This makes it easier to figure out what the most important features are.

How to figure out which algorithm to use?

Data correlation and visualisation can help you decide which ML algorithm to use.

Take a look at the brain vs body size data again. Both a neural network and linear regression will be able to fit this data. But, linear regression is less computationally expensive and will train faster than a neural network.

If your data does not have a linear correlation, you could consider using polynomial regression, SVMs or Random Forests.

But, on large datasets, these might be more computationally expensive to train than a small neural network.

When working on Image Recognition problems, it’s always better to use Convolutional Neural Networks (CNN). But, NLP and time series problems are better modeled by Recurrent Neural Networks and LSTMs (Long Short Term Memory).

Choosing an algorithm also depends on whether you are doing regression or classification.

Neural networks are amazing at classification tasks but not for regression. A simple SVM might perform better.

That’s all Folks!

If you liked this article please follow me and give me a clap…or two…or 40. If you didn’t then I would love to hear your feedback! You can follow me on twitter (@csoham358) also read some of my other articles here

Sources:

The brain size vs body size image was taken from Siraj Raval’s tutorial on implementing linear regression from scratch.

The Titanic Graphs were sourced from Ahmed Besbes blog on How to score 0.8134 in Kaggle Titanic Challenge.

Other diagram were taken from an article on data correlation from mathsisfun.com