Predicting Used Car Prices with Machine Learning (Python scikit-learn Regression Edition)

Posted by


In this tutorial, we will learn how to predict the price of used cars using machine learning techniques, specifically regression using Python’s scikit-learn library. Predicting the price of used cars can be a valuable tool for both buyers and sellers in the automotive market. By analyzing various features of a car, we can build a model that can predict the price based on these attributes.

To begin, make sure you have Python and scikit-learn installed on your system. If you haven’t installed them yet, I recommend using the Anaconda distribution which comes with both installed by default.

First, we need a dataset to work with. For this tutorial, we will use the famous ‘Used Cars Dataset’ available on Kaggle. You can download the dataset from the following link: https://www.kaggle.com/austinreese/craigslist-carstrucks-data/

Once you have downloaded the dataset, you can start by loading it into a pandas DataFrame:

import pandas as pd

data = pd.read_csv('used_cars_dataset.csv')

Next, we need to preprocess the data by handling missing values, converting categorical variables into numerical format, and selecting the relevant features that will be used for training our model:

# Check for missing values
data.isnull().sum()

# Handle missing values
data['odometer'].fillna(data['odometer'].mean(), inplace=True)
data['manufacturer'].fillna('unknown', inplace=True)
data['model'].fillna('unknown', inplace=True)

# Convert categorical variables into numerical format
data = pd.get_dummies(data, columns=['manufacturer', 'model', 'condition', 'fuel', 'title_status', 'transmission', 'drive', 'size', 'type', 'paint_color'])

# Select relevant features
X = data.drop(['price'], axis=1)
y = data['price']

After preprocessing the data, we can split it into a training and testing set:

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Now, we can build a regression model using scikit-learn. In this tutorial, we will use the Random Forest Regressor as our model:

from sklearn.ensemble import RandomForestRegressor

model = RandomForestRegressor()
model.fit(X_train, y_train)

Once the model is trained, we can make predictions on the test set and evaluate its performance:

predictions = model.predict(X_test)

from sklearn.metrics import mean_squared_error, r2_score

mse = mean_squared_error(y_test, predictions)
r2 = r2_score(y_test, predictions)

print('Mean Squared Error:', mse)
print('R^2 Score:', r2)

After evaluating the model, you can further fine-tune it by tweaking hyperparameters or trying different regression algorithms. Remember that machine learning is an iterative process, so don’t be afraid to experiment with different approaches.

To make predictions on new data, you can simply pass the features of the car you want to predict the price for to the model’s predict method:

new_data = pd.DataFrame({
    'odometer': [50000],
    'year': [2015],
    'manufacturer': ['Toyota'],
    'model': ['Camry'],
    'condition': ['excellent'],
    'fuel': ['gas'],
    'title_status': ['clean'],
    'transmission': ['automatic'],
    'drive': ['fwd'],
    'size': ['mid-size'],
    'type': ['sedan'],
    'paint_color': ['blue']
})

new_data = pd.get_dummies(new_data, columns=['manufacturer', 'model', 'condition', 'fuel', 'title_status', 'transmission', 'drive', 'size', 'type', 'paint_color'])

prediction = model.predict(new_data)
print('Predicted Price:', prediction)

That’s it! You have now successfully built and trained a regression model to predict the price of used cars using machine learning. Remember that the accuracy of the model may vary depending on the features and the amount of data available. Experiment with different approaches to improve the performance of your model. Happy coding!