Skip to content
Project: Predicting Movie Rental Durations
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 required packages
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
# Load the dataset
rental_df = pd.read_csv("rental_info.csv")
# Create target column: rental_length_days
rental_df['rental_date'] = pd.to_datetime(rental_df['rental_date'])
rental_df['return_date'] = pd.to_datetime(rental_df['return_date'])
rental_df['rental_length_days'] = (rental_df['return_date'] - rental_df['rental_date']).dt.days
# Create dummy variables for special_features
rental_df['deleted_scenes'] = rental_df['special_features'].str.contains('Deleted Scenes', na=False).astype(int)
rental_df['behind_the_scenes'] = rental_df['special_features'].str.contains('Behind the Scenes', na=False).astype(int)
# Select features (avoid leaking target info)
feature_cols = [
'amount', 'amount_2', 'rental_rate', 'rental_rate_2',
'release_year', 'length', 'length_2', 'replacement_cost',
'NC-17', 'PG', 'PG-13', 'R',
'deleted_scenes', 'behind_the_scenes'
]
X = rental_df[feature_cols]
y = rental_df['rental_length_days']
# Split data into train and test sets (20% test)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=9
)
# Train a few regression models
# 1. Linear Regression
lr = LinearRegression()
lr.fit(X_train, y_train)
y_pred_lr = lr.predict(X_test)
mse_lr = mean_squared_error(y_test, y_pred_lr)
print("Linear Regression MSE:", mse_lr)
# 2. Random Forest Regressor
rf = RandomForestRegressor(n_estimators=200, random_state=9)
rf.fit(X_train, y_train)
y_pred_rf = rf.predict(X_test)
mse_rf = mean_squared_error(y_test, y_pred_rf)
print("Random Forest MSE:", mse_rf)
# 3. Gradient Boosting Regressor
gbr = GradientBoostingRegressor(n_estimators=200, max_depth=4, random_state=9)
gbr.fit(X_train, y_train)
y_pred_gbr = gbr.predict(X_test)
mse_gbr = mean_squared_error(y_test, y_pred_gbr)
print("Gradient Boosting MSE:", mse_gbr)
# Choose the best model (lowest MSE)
mse_list = [mse_lr, mse_rf, mse_gbr]
models_list = [lr, rf, gbr]
best_idx = np.argmin(mse_list)
best_model = models_list[best_idx]
best_mse = mse_list[best_idx]
print("\nBest model:", type(best_model).__name__)
print("Best test MSE:", best_mse)