Back to projects

Classifying whether a car's gas mileage is above or below the median

LDA / QDA Naive Bayes Logistic regression KNN

Project description

The objective of this project is to test different classification models on their ability to classify whether a car's gas mileage is above or below the median value of the dataset. Gas mileage is measured in miles per gallon (mpg) based on the following car attributes: cylinders, displacement, horsepower, weight, acceleration, model year, and origin. The data — city-cycle fuel consumption in miles per gallon — was retrieved from the UCI Auto MPG dataset.

Exploratory data analysis

There are 392 observations across 8 variables. Below are the minimum, median, mean, and max values for cylinders, displacement, horsepower, weight, acceleration, and model year.

Methods

I split the data into approximately 70% training and 30% testing by randomly sampling the dataset. This is a standard split — using 70% or more of the data for training and 30% or less for testing — and was applied consistently across all five models.

Results

LDA

Testing error 0.1186441

52.19% of the training data had an mpg value above the dataset median, and 47.81% had an mpg value below it.

LDA model confusion matrix for classifying gas mileage above or below the median

QDA

Testing error 0.1186441

52.19% of the training data had an mpg value above the dataset median, and 47.81% had an mpg value below it.

QDA model confusion matrix for classifying gas mileage above or below the median

Naive Bayes

Testing error 0.1017

57 data points were correctly classified as below the median mpg, and 49 were correctly classified as above the median mpg.

Naive Bayes model confusion matrix for classifying gas mileage above or below the median

Logistic regression

Testing error 0.1186

54 data points were correctly classified as below the median mpg, and 50 were correctly classified as above the median mpg.

Logistic regression model confusion matrix for classifying gas mileage above or below the median

KNN

Testing error 0.1355932 (k = 4)

The k value with the lowest testing error was k = 4. 55 data points were correctly classified as below the median mpg, and 48 were correctly classified as above the median mpg.

KNN model confusion matrix for classifying gas mileage above or below the median

Conclusion

Naive Bayes had the lowest testing error and performed the best, followed by logistic regression. KNN performed the worst of the five models. LDA and QDA produced the same testing error.