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 as MSE
from sklearn.linear_model import LinearRegression,Lasso,Ridge
from sklearn.model_selection import GridSearchCV,RandomizedSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestRegressor
# Import any additional modules and start coding below
data=pd.read_csv("rental_info.csv")
data.head()data["rental_date"]=pd.to_datetime(data["rental_date"])
data["return_date"]=pd.to_datetime(data["return_date"])data["rental_length_days"]=(data["return_date"] - data["rental_date"]).dt.days
data["deleted_scenes"] = np.where(data["special_features"].str.contains("Deleted Scenes"), 1, 0)
# Add dummy for behind the scenes
data["behind_the_scenes"] = np.where(data["special_features"].str.contains("Behind the Scenes"), 1, 0)data.head()cols=["special_features","rental_date","return_date","release_year"]
X=data.drop(cols,axis=1)
y=data["rental_length_days"]X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=9)algorithms={
'linear regression':{
'model':LinearRegression(),
"params":{}
},
'Lasso':{
'model':Lasso(),
'params':{
"alpha":[0.1,0.2,0.3,0.5]
}
},
'random_forest_regressor':{
'model':RandomForestRegressor(),
'params':{
'n_estimators':[1,5,10,20,50],
'criterion':['absolute_error', 'poisson', 'friedman_mse', 'squared_error'],
'max_depth':list(range(1,11,1))
}
},
}Sc=StandardScaler()
X_train_scaled=Sc.fit_transform(X_train)
X_test_scaled=Sc.fit_transform(X_test)params={"alpha":np.arange(0,1,0.1).tolist()}
Las_reg=Lasso(random_state=9)
rscv=RandomizedSearchCV(Las_reg,params,cv=10)
rscv.fit(X_train_scaled,y_train)
best_model_l=rscv.best_estimator_
las_pred=best_model_l.predict(X_test_scaled)
mse_ls=MSE(y_test,las_pred)
rmse=mse_ls ** (1/2)
print(f"Root mean squared error:{rmse} \n rsvc best parameter:{rscv.best_params_} \n rsvc best score:{rscv.best_score_}")lasso=Lasso(alpha=0.0,random_state=9)
lasso.fit(X_train_scaled,y_train)
y_l_pred=lasso.predict(X_test_scaled)
lasso_coef=lasso.coef_
X_lasso_train, X_lasso_test = X_train_scaled[:, lasso_coef > 0], X_test_scaled[:, lasso_coef > 0]
lr=LinearRegression()
lr.fit(X_lasso_train,y_train)
lr_pred=lr.predict(X_lasso_test)
mse_lr=MSE(y_test,lr_pred)
rmse_lr=mse_lr ** (1/2)
print(rmse_lr)
parameters={
"n_estimators":list(range(1,101,1)),
"max_depth":list(range(1,11,1))
}
rf=RandomForestRegressor()
rscvs=RandomizedSearchCV(rf,parameters,cv=10)
rscvs.fit(X_train,y_train)
best_model=rscvs.best_estimator_
print(best_model,rscvs.best_score_)
rf_params=rscvs.best_params_
rf_final=RandomForestRegressor(n_estimators=rf_params["n_estimators"],max_depth=rf_params["max_depth"],random_state=9)
rf_final.fit(X_train,y_train)
pred_rf_f=rf_final.predict(X_test)
mse=MSE(pred_rf_f,y_test)
rmse_f=mse ** (1/2)
print(rmse_f)