Skip to content

A DVD rental company needs your help! They want to figure out how many days a customer will rent a DVD for based on some features and has approached you for help. They want you to try out some regression models which will help predict the number of days a customer will rent a DVD for. The company wants a model which yeilds a MSE of 3 or less on a test set. The model you make will help the company become more efficient inventory planning.

The data they provided is in the csv file rental_info.csv. It has the following features:

  • "rental_date": The date (and time) the customer rents the DVD.
  • "return_date": The date (and time) the customer returns the DVD.
  • "amount": The amount paid by the customer for renting the DVD.
  • "amount_2": The square of "amount".
  • "rental_rate": The rate at which the DVD is rented for.
  • "rental_rate_2": The square of "rental_rate".
  • "release_year": The year the movie being rented was released.
  • "length": Lenght of the movie being rented, in minuites.
  • "length_2": The square of "length".
  • "replacement_cost": The amount it will cost the company to replace the DVD.
  • "special_features": Any special features, for example trailers/deleted scenes that the DVD also has.
  • "NC-17", "PG", "PG-13", "R": These columns are dummy variables of the rating of the movie. It takes the value 1 if the move is rated as the column name and 0 otherwise. For your convinience, the reference dummy has already been dropped.
# Import necessary packages
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split

# Load dataset
rent = pd.read_csv('rental_info.csv', parse_dates=['rental_date', 'return_date'])
rent.head()
## Pre-process data

# Add column with information on how many days a DVD has been rented
rent['rental_length_days'] = (rent['return_date'] - rent['rental_date']).dt.days

# Create dummy for 'Deleted Scenes'
rent['deleted_scenes'] = rent['special_features'].str.contains('Deleted Scenes', regex=False).astype(int)

# Create dummy for 'Behind the Scenes'
rent['behind_the_scenes'] = rent['special_features'].str.contains('Behind the Scenes', regex=False).astype(int)

# Drop the original special_features colums
rent = rent.drop('special_features', axis=1)

# Drop potential leakage features
rent = rent.drop(['rental_date', 'return_date'], axis=1)

# Define features and target
X = rent.drop('rental_length_days', axis=1)
y = rent['rental_length_days']

# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=9)
## Models selection and evaluation

# Import additional modules
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor

# Tran Linear Regression model
linreg = LinearRegression()
linreg.fit(X_train, y_train)

# Predict on test set
y_pred_linreg = linreg.predict(X_test)

# Calcilate MSE
mse_linreg = mean_squared_error(y_test, y_pred_linreg)
print(f'Linear Regression MSE is {mse_linreg:.3f}.')

# Train Random Forest Regressor
ranfor = RandomForestRegressor()
ranfor.fit(X_train, y_train)

# Predict on test set
y_pred_ranfor = ranfor.predict(X_test)

# Calcilate MSE
mse_ranfor = mean_squared_error(y_test, y_pred_ranfor)
print(f'Random Forest Regressor MSE is {mse_ranfor:.3f}.')

# Train Gradient Boosting Regressor model
gbr = GradientBoostingRegressor(random_state=9)
gbr.fit(X_train, y_train)

# Predict on test set
y_pred_gbr = gbr.predict(X_test)

# Calcilate MSE
mse_gbr = mean_squared_error(y_test, y_pred_gbr)
print(f'Gradient Boosting Regressor MSE is {mse_gbr:.3f}.')

# Store both models and their corresponding MSEs
models_dict = {
    'Linear Regression': (linreg, mse_linreg),
    'Random Forest Regressor': (ranfor, mse_ranfor),
    'Gradient Boosting Regressor': (gbr, mse_gbr)
}

# Find the model with the lowest MSE
best_model_name = min(models_dict, key=lambda model: models_dict[model][1])  # Get model name with lowest MSE
best_model, best_mse = models_dict[best_model_name]                         # Get the model object and its MSE

# Display the recommended model
print(f'\nRecommended regression model is {best_model_name} with MSE {best_mse:.3f}.')