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

from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.neighbors import KNeighborsRegressor as KNN

from sklearn.ensemble import VotingRegressor

SEED = 9

# Importing the csv file into action
rental_info = pd.read_csv('rental_info.csv')

rental_info["special_features"] = (
    rental_info["special_features"]
    .str.strip("{}")
    .str.replace('"', '', regex=False)
)

rental_info
# ------------------- Getting things ready -------------------
rental_info['rental_length_days'] = (pd.to_datetime(rental_info['return_date']) - pd.to_datetime(rental_info['rental_date'])).dt.days

special_features_dummies = rental_info["special_features"].str.get_dummies(sep=",")

# Keep only the two columns we need and rename
rental_info = rental_info.join(
    special_features_dummies[["Deleted Scenes", "Behind the Scenes"]].rename(
        columns={
            "Deleted Scenes": "deleted_scenes",
            "Behind the Scenes": "behind_the_scenes"
        }
    )
)

print(rental_info.columns)
# ------------------- Splitting data -------------------
X = rental_info[['amount', 'release_year', 'rental_rate',
       'length', 'replacement_cost', 'NC-17', 'PG', 'PG-13', 'R', 'amount_2', 
        'length_2', 'rental_rate_2', 'deleted_scenes', 'behind_the_scenes']]

y = rental_info['rental_length_days']

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

linreg = LinearRegression()
knn = KNN()
dt = DecisionTreeRegressor(random_state=SEED)

regressors = [
    ('Linear Regression', linreg),
    ('KNN', knn),
    ('Decision Tree', dt)
]

vr = VotingRegressor(estimators=regressors)

vr.fit(X_train, y_train)
y_pred = vr.predict(X_test)

mse_vr = MSE(y_test, y_pred)

best_model = vr
best_mse = mse_vr