https://faroukibrahim-fii.github.io/reading-notes/
Generally speaking, we can break down applied machine learning into the following chunks:
We will cover exploratory analysis, data cleaning, feature engineering, algorithm selection, and model training.
The purpose of exploratory analysis is to “get to know” the dataset. Doing so upfront will make the rest of the project much smoother, in 3 main ways:
tx_price | beds | baths | sqft | year_built | lot_size | property_type | exterior_walls | roof | basement | restaurants | groceries | nightlife | cafes | shopping | arts_entertainment | beauty_spas | active_life | median_age | married | college_grad | property_tax | insurance | median_school | num_schools | tx_year | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 295850 | 1 | 1 | 584 | 2013 | 0 | Apartment / Condo / Townhouse | Wood Siding | NaN | NaN | 107 | 9 | 30 | 19 | 89 | 6 | 47 | 58 | 33.0 | 65.0 | 84.0 | 234.0 | 81.0 | 9.0 | 3.0 | 2013 |
1 | 216500 | 1 | 1 | 612 | 1965 | 0 | Apartment / Condo / Townhouse | Brick | Composition Shingle | 1.0 | 105 | 15 | 6 | 13 | 87 | 2 | 26 | 14 | 39.0 | 73.0 | 69.0 | 169.0 | 51.0 | 3.0 | 3.0 | 2006 |
2 | 279900 | 1 | 1 | 615 | 1963 | 0 | Apartment / Condo / Townhouse | Wood Siding | NaN | NaN | 183 | 13 | 31 | 30 | 101 | 10 | 74 | 62 | 28.0 | 15.0 | 86.0 | 216.0 | 74.0 | 8.0 | 3.0 | 2012 |
3 | 379900 | 1 | 1 | 618 | 2000 | 33541 | Apartment / Condo / Townhouse | Wood Siding | NaN | NaN | 198 | 9 | 38 | 25 | 127 | 11 | 72 | 83 | 36.0 | 25.0 | 91.0 | 265.0 | 92.0 | 9.0 | 3.0 | 2005 |
4 | 340000 | 1 | 1 | 634 | 1992 | 0 | Apartment / Condo / Townhouse | Brick | NaN | NaN | 149 | 7 | 22 | 20 | 83 | 10 | 50 | 73 | 37.0 | 20.0 | 75.0 | 88.0 | 30.0 | 9.0 | 3.0 | 2002 |
Next, it can be very enlightening to plot the distributions of your numeric features.
Often, a quick and dirty grid of histograms is enough to understand the distributions.
Categorical features cannot be visualized through histograms. Instead, you can use bar plots.
In particular, you’ll want to look out for sparse classes, which are classes that have a very small number of observations.
Segmentations are powerful ways to observe the relationship between categorical features and numeric features.
Box plots allow you to do so.
Finally, correlations allow you to look at the relationships between numeric features and other numeric features.
Correlation is a value between -1 and 1 that represents how closely two features move in unison.
Data cleaning is one those things that everyone does but no one really talks about. Sure, it’s not the “sexiest” part of machine learning. And no, there aren’t hidden tricks and secrets to uncover.
However, proper data cleaning can make or break your project. Professional data scientists usually spend a very large portion of their time on this step.
Better data beats fancier algorithms.
In other words… garbage in gets you garbage out. Even if you forget everything else from this course, please remember this point.
The first step to data cleaning is removing unwanted observations from your dataset.
This includes duplicate or irrelevant observations.
Duplicate observations
Duplicate observations most frequently arise during data collection, such as when you:
The next bucket under data cleaning involves fixing structural errors.
Structural errors are those that arise during measurement, data transfer, or other types of “poor housekeeping.”
As you can see:
Outliers can cause problems with certain types of models. For example, linear regression models are less robust to outliers than decision tree models.
In general, if you have a legitimate reason to remove an outlier, it will help your model’s performance.
Missing data is a deceptively tricky issue in applied machine learning.
First, just to be clear, you cannot simply ignore missing values in your dataset. You must handle them in some way for the very practical reason that most algorithms do not accept missing values.
Unfortunately, from our experience, the 2 most commonly recommended ways of dealing with missing data actually suck.
They are:
Dropping missing values is sub-optimal because when you drop observations, you drop information.
Imputing missing values is sub-optimal because the value was originally missing but you filled it in, which always leads to a loss in information, no matter how sophisticated your imputation method is.
Feature engineering is about creating new input features from your existing ones.
In general, you can think of data cleaning as a process of subtraction and feature engineering as a process of addition.
You can often engineer informative features by tapping into your (or others’) expertise about the domain.
Try to think of specific information you might want to isolate. Here, you have a lot of “creative freedom.”
The first of these heuristics is checking to see if you can create any interaction features that make sense. These are combinations of two or more features.
By the way, in some contexts, “interaction terms” must be products between two variables. In our context, interaction features can be products, sums, or differences between two features.
The next heuristic we’ll consider is grouping sparse classes.
Sparse classes (in categorical features) are those that have very few total observations. They can be problematic for certain machine learning algorithms, causing models to be overfit.
Most machine learning algorithms cannot directly handle categorical features. Specifically, they cannot handle text values.
Therefore, we need to create dummy variables for our categorical features.
Dummy variables are a set of binary (0 or 1) variables that each represent a single class from a categorical feature.
Finally, remove unused or redundant features from the dataset.
Unused features are those that don’t make sense to pass into our machine learning algorithms.
In this lesson, we’ll introduce 5 very effective machine learning algorithms for regression tasks. They each have classification counterparts as well.
And yes, just 5 for now. Instead of giving you a long list of algorithms, our goal is to explain a few essential concepts (e.g. regularization, ensembling, automatic feature selection) that will teach you why some algorithms tend to perform better than others.
To introduce the reasoning for some of the advanced algorithms, let’s start by discussing basic linear regression. Linear regression models are very common, yet deeply flawed.
This is the first “advanced” tactic for improving model performance. It’s considered pretty “advanced” in many ML courses, but it’s really pretty easy to understand and implement.
The first flaw of linear models is that they are prone to be overfit with many input features.
There are 3 common types of regularized linear regression algorithms.
Lasso, or LASSO, stands for Least Absolute Shrinkage and Selection Operator.
Ridge stands Really Intense Dangerous Grapefruit Eating (just kidding… it’s just ridge).
Elastic-Net is a compromise between Lasso and Ridge.
Awesome, we’ve just seen 3 algorithms that can protect linear regression from overfitting. But if you remember, linear regression suffers from two main flaws:
Ensembles are machine learning methods for combining predictions from multiple separate models. There are a few different methods for ensembling, but the two most common are:
Bagging attempts to reduce the chance overfitting complex models.
Boosting attempts to improve the predictive flexibility of simple models.
Random forests train a large number of “strong” decision trees and combine their predictions through bagging.
Boosted trees train a sequence of “weak”, constrained decision trees and combine their predictions through boosting.
How to Train ML Models
At last, it’s time to build our models!
It might seem like it took us a while to get here, but professional data scientists actually spend the bulk of their time on the steps leading up to this one:
Again, that’s because better data beats fancier algorithms.
Let’s start with a crucial but sometimes overlooked step: Spending your data.
Think of your data as a limited resource.
So far, we’ve been casually talking about “tuning” models, but now it’s time to treat the topic more formally.
When we talk of tuning models, we specifically mean tuning hyperparameters.
There are two types of parameters in machine learning algorithms.
Next, it’s time to introduce a concept that will help us tune our models: cross-validation.
Cross-validation is a method for getting a reliable estimate of model performance using only your training data.
Now that we’ve split our dataset into training and test sets, and we’ve learned about hyperparameters and cross-validation, we’re ready fit and tune our models.
Basically, all we need to do is perform the entire cross-validation loop detailed above on each set of hyperparameter values we’d like to try.
By now, you’ll have 1 “best” model for each algorithm that has been tuned through cross-validation. Most importantly, you’ve only used the training data so far.