Machine Learning
Predicting Car Fuel Economy Dataset: Auto MPG, 398 cars from 1970–82 Tools: Python, pandas, scikit-learn, matplotlib
0.892 test R², polynomial model
0.806 test R², linear baseline
398 cars, 80 held back for testing
The dataset and why I chose it
This dataset has information on car models from the 70's and 80's. Older vehicles are an interest of mine, so being able to visualize data about them is something I really wanted to do. It also has a variety of datatypes to work with, which gave me a lot more to work with. The goal is to predict a car's fuel economy from its other features. I thought with how simple cars were back then, that it would be easier to estimate fuel economy.
Cleaning and feature engineering
Missing values. Horsepower was stored as text because six rows had a "?" in them, so those had to be changed into proper null values before I could work with them. I filled them in with the median, which is 93.5.
Balance and outliers. It seems that the origin column is imbalanced, with a vast majority of the cars being American made, 249 of 398 cars. Odd numbered cylinder engines are also incredibly uncommon, and four cylinders is the most common at 204 cars. Since fuel economy is a number, there aren't any classes to balance. I kept all of the rows because there were no absurd values that didn't seem accurate for a car from this era.
Encoding. There are 305 unique car names out of 398 rows, so most are unique and the model wouldn't have much to learn from if I one hot encoded them. I pulled out the brand, which is the first word of the name, instead and fixed the typos and duplicates, like "chevy" and "chevrolet". Origin was one hot encoded since the numbers are just labels and not sequential.
Correlated columns. Fuel economy has the strongest correlation with weight, and it is negative, so the heavier a car is, the worse its fuel efficiency is. Weight, displacement, horsepower, and cylinders are also all correlated with each other, which makes sense since the larger a vehicle is, the more powerful it seemingly needs to be. They were too highly correlated, so I removed cylinders and displacement so they don't harm the model.
0 10 20 30 40 50 60 70 9 15 21 27 33 39 45 fuel economy (mpg) number of cars Most cars fall between about 13 and 30 mpg, with a smaller group of really efficient ones.
mpg cyl disp hp weight accel year mpg 1.00 -0.78 -0.80 -0.77 -0.83 0.42 0.58 cyl -0.78 1.00 0.95 0.84 0.90 -0.51 -0.35 disp -0.80 0.95 1.00 0.90 0.93 -0.54 -0.37 hp -0.77 0.84 0.90 1.00 0.86 -0.69 -0.41 weight -0.83 0.90 0.93 0.86 1.00 -0.42 -0.31 accel 0.42 -0.51 -0.54 -0.69 -0.42 1.00 0.29 year 0.58 -0.35 -0.37 -0.41 -0.31 0.29 1.00
Correlation between columns. Red means they go up together and blue means one goes up as the other goes down. Weight has the strongest link to fuel economy, at -0.83.
Finding clusters without using mpg
I left out fuel economy, used only the features, and ran K-means on them. The elbow is at k=2 because that's where the most pronounced bend in the chart is. It split the cars into two groups: 301 lighter, lower horsepower cars that average about 2,592 lb and 86 hp and 97 much heavier and more powerful ones that average about 4,144 lb and 162 hp. The fuel economy between the two groups varies drastically, with the more efficient group at about 26.4 mpg and the more powerful, less efficient group at only a little over 14.5.
0 600 1200 1800 1 2 3 4 5 6 7 8 number of clusters (k) inertia elbow at k = 2 Elbow plot. The bend is at 2 clusters.
50 100 150 200 250 2,000 3,000 4,000 5,000 weight (lb) horsepower lighter, lower horsepower heavier, more powerful
Train/test split and the models
I held out 20% of the cars, which is 80, to test on and trained on the other 318. The model can't see the test cars while it trains, otherwise it would memorize them and make the scores look better than they actually are. Based on the shape of the fuel economy vs weight and horsepower plots there's a bit of a curve, so a polynomial regression made the most sense using degree 2 on horsepower, weight, acceleration, and model year. I trained a plain linear regression too, since knowing how it acts can show where it might be more accurate.
Results
R² is basically how much of the variation in fuel economy the model explains. On cars it hadn't seen before, the polynomial model explained about 89.2% of it and the linear model explained about 80.6%, so my initial prediction was correct. They are both somewhat accurate, but the polynomial version does have that slight edge, and on the graph it gets slightly tighter when the fuel economy is higher. The training and test scores were close for both, so neither seems badly overfit.
Polynomial
0.892 train 0.896
10 20 30 40 50 10 20 30 40 50 actual fuel economy (mpg) predicted fuel economy (mpg) Predicted fuel economy against actual fuel economy for the 80 test cars. The closer a dot is to the dashed line, the better the prediction.
What I learned
Fuel economy is driven by how big and powerful a vehicle is. Weight, displacement, horsepower, and cylinders were all negatively correlated with fuel economy, and letting the model curve got it about nine points of R² higher than a straight line. The data is small and outdated, so I don't expect it to be incredibly accurate for newer vehicles, considering the advancements made in technology and efficiency. There also aren't enough 3 and 5 cylinder cars for the model to learn much from.
I got a lot more comfortable with pandas and scikit-learn. pandas made the cleaning pretty easy, like turning the bad horsepower values into nulls with to_numeric and one hot encoding the brands with get_dummies. scikit-learn covered everything else, like SimpleImputer for the median, KMeans for the clusters, and a pipeline that scales the columns and adds the polynomial terms, so the exact same steps get applied to the test cars. The biggest thing I learned is to split the data before fitting anything and to score the model on cars it hasn't seen, since that is what shows whether it really works. I also refined my matplotlib skills.