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 pandas as pd
import numpy as np

from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error as MSE

# Import any additional modules and start coding below
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor

from sklearn.model_selection import GridSearchCV
rental_info_df = pd.read_csv("rental_info.csv", parse_dates=['rental_date', 'return_date'])
rental_info_df
rental_info_df.info()
rental_info_df['rental_length_days'] = (rental_info_df['return_date'] - rental_info_df['rental_date']).dt.days
rental_info_df
rental_info_df['deleted_scenes'] = np.where(rental_info_df['special_features'].str.contains("Deleted Scenes"), 1,0)
rental_info_df['behind_the_scenes'] = np.where(rental_info_df['special_features'].str.contains("Behind the Scenes"), 1, 0)
rental_info_df
# split - train | test
X = rental_info_df.drop(['rental_length_days', 'rental_date', 'return_date', 'special_features'], axis=1)
y = rental_info_df['rental_length_days']
# y
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=9)
lr_model = LinearRegression()
rf_model = RandomForestRegressor(random_state=9)
gb_model = GradientBoostingRegressor(random_state=9)

models = [
    ('LinearRegression', lr_model),
    ('RandomForest', rf_model),
    ('GradientBoosting', gb_model)
]

best_model = None
best_mse = 3 # value assumed for the requirement - "(MSE) less than 3 on the test set"

for model_name, model in models:
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    mse_val = MSE(y_test, y_pred)
    print(f"{model_name} MSE: {mse_val:.2f}")

    if mse_val < best_mse:
        best_model = model
        best_mse = mse_val
best_model
rf_params = {
    'n_estimators': [100, 200],
    'max_depth': [None, 10, 20],
    'max_features': ['auto', 'sqrt']
}

rf_grid = GridSearchCV(rf_model,
                       param_grid=rf_params,
                       cv=5,
                       scoring='neg_mean_squared_error',
                       n_jobs=-1)

rf_grid.fit(X_train, y_train)
rf_best_model = rf_grid.best_estimator_
rf_best_mse = MSE(y_test, rf_best_model.predict(X_test))
print(f"RandomForest (tuned) MSE: {rf_best_mse:.2f}")
gb_params = {
    'n_estimators': [100, 200],
    'learning_rate': [0.05, 0.1],
    'max_depth': [3, 5],
    'subsample': [0.8, 1.0]
}

gb_grid = GridSearchCV(gb_model,
                       param_grid=gb_params,
                       cv=5,
                       scoring='neg_mean_squared_error',
                       n_jobs=-1)

gb_grid.fit(X_train, y_train)
gb_best_model = gb_grid.best_estimator_
gb_best_mse = MSE(y_test, gb_best_model.predict(X_test))
print(f"GradientBoosting (tuned) MSE: {gb_best_mse:.2f}")
for tuned_model, tuned_mse in [(rf_best_model, rf_best_mse), (gb_best_model, gb_best_mse)]:
    if tuned_mse < best_mse:
        best_mse = tuned_mse
        best_model = tuned_model

print(f"Final Best Model: {type(best_model).__name__} with MSE: {best_mse:.2f}")