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

# Import any additional modules and start coding below
import pandas as pd
import datetime as dt
import numpy as np
#Read rental_info.csv
df = pd.read_csv('rental_info.csv')
#datetime columns
df["rental_date"] = pd.to_datetime(df["rental_date"])
df["return_date"] = pd.to_datetime(df["return_date"])
#Column "rental_length_days"
df["rental_length_days"] = (df["return_date"] - df["rental_date"] ).dt.days
#columns of dummy variables
df["deleted_scenes"] = np.where(df["special_features"].str.contains("Deleted Scenes"), 1, 0)
df["behind_the_scenes"] = np.where(df["special_features"].str.contains("Behind the Scenes"), 1, 0)
#Get X and y features
X = df[["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']]
y = df['rental_length_days']
#Split data into train test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=9)
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint

# Define the models
models = {
    "LinearRegression": LinearRegression(),
    "DecisionTreeRegressor": DecisionTreeRegressor(),
    "RandomForestRegressor": RandomForestRegressor()
}

# Parameters for RandomizedSearchCV
param_distributions = {
    "LinearRegression": {},  # LinearRegression does not have hyperparameters to tune in this context
    "DecisionTreeRegressor": {
        "max_depth": randint(1, 20),
        "min_samples_leaf": randint(1, 20),
        "min_samples_split": randint(2, 20)
    },
    "RandomForestRegressor": {
        "n_estimators": randint(10, 200),
        "max_depth": randint(1, 20),
        "min_samples_leaf": randint(1, 20),
        "min_samples_split": randint(2, 20)
    }
}

# Results container
best_estimators = {}

# Perform Randomized Search
for model_name in models:
    random_search = RandomizedSearchCV(models[model_name], param_distributions=param_distributions[model_name], n_iter=10, cv=5, scoring='neg_mean_squared_error', random_state=9)
    random_search.fit(X_train, y_train)
    best_estimators[model_name] = random_search.best_estimator_
    print(f"Best parameters for {model_name}: {random_search.best_params_}")
    print(f"Best score for {model_name}: {random_search.best_score_}")
best_model = RandomForestRegressor(max_depth = 16, min_samples_leaf=6, min_samples_split=10, n_estimators = 142,random_state=9)
best_model.fit(X_train, y_train)
y_pred = best_model.predict(X_test)
best_mse = mean_squared_error(y_test, y_pred)
best_mse