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 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
rental = pd.read_csv("rental_info.csv")
#EDA
print(rental.head())#Getting the number of rental days.
#Convert to pd.datetime() format
column =["return_date","rental_date"]
rental[column]= rental[column].apply(pd.to_datetime)
# Calculation of rental period in days
rental['rental_length_days'] = rental.apply(lambda row: (row['return_date'] - row['rental_date']).days, axis=1)
#drop date column
rental = rental.drop(column,axis=1)
print(rental.head())#Add dummy variable
rental["deleted_scenes"] = np.where(rental["special_features"].str.contains("Deleted Scenes"), 1,0)
rental["behind_the_scenes"] = np.where(rental["special_features"].str.contains("Behind the Scenes"), 1,0)
#drop "special feature column"
rental = rental.drop(["special_features"],axis=1)
print(rental.info())#Execute a train-test split
#definir variable explicative et variable cible
X = rental.drop(columns=["rental_length_days"])
y= rental ["rental_length_days"]
#X feature matrix, y target variable
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=9)#Feature Selection
from sklearn.linear_model import Lasso
#instantiate lasso model
lasso_model = Lasso(alpha=0.1,random_state=9)
#fit to data
lasso_model.fit(X_train,y_train)
#feature impotance
print(lasso_model.coef_)
#set condition for non-null
non_zero_coeff = lasso_model.coef_ != 0
#subset feature
X_train = X_train.iloc[:,non_zero_coeff]
X_test = X_test.iloc[:,non_zero_coeff]
print(X_train)
print(X_test)
#Choosing model and perform hyperparameter tuning
#import module and functiun
from sklearn.metrics import mean_squared_error as MSE
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
#define a functiun to evaluate model
def evaluate_model (model,X_train,y_train,X_test, y_test):
model.fit(X_train,y_train)
y_pred = model.predict(X_test)
mse = MSE(y_test,y_pred)
return mse
#dictionary of the models
models = {
'Linear Regression': LinearRegression(),
'Decision Tree': DecisionTreeRegressor(random_state=9),
'Random Forest': RandomForestRegressor(random_state=9)
}
#empty dictionary for mse
condition = 3
filtered_models_mse = {}
# Loop to evaluate each model and store the MSE in the results dictionary
for name, model in models.items():
mse = evaluate_model(model, X_train, y_train, X_test, y_test)
if mse < condition:
filtered_models_mse[name] = mse
# Get the best model and its MSE in one go
best_model_name, best_mse = min(filtered_models_mse.items(), key=lambda item: item[1])
# Get the actual scikit-learn model object
best_model = models[best_model_name]
print(f"The best model is: {best_model_name} with MSE: {best_mse}")