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
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error as MSE
from sklearn.linear_model import Lasso
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestRegressor
# Read the dataset
df_rental = pd.read_csv('rental_info.csv')
df_rental.info()
df_rental.head(5)
# Convert into datetime format
df_rental['return_date'] = pd.to_datetime(df_rental['return_date'])
df_rental['rental_date'] = pd.to_datetime(df_rental['rental_date'])
# Create column named rental_length_days then get the number of days.
df_rental['rental_length'] = df_rental['return_date'] - df_rental['rental_date']
df_rental['rental_length_days'] = df_rental['rental_length'].dt.days
# Create two columns of dummy variables from "special features" column using np.where()
df_rental['deleted_scenes'] = np.where(df_rental['special_features'].str.contains('Deleted Scenes'), 1, 0)
df_rental['behind_the_scenes'] = np.where(df_rental['special_features'].str.contains('Behind the Scenes'), 1, 0)
# Choose columns to drop then split the dataset into feature and target sets
cols_to_drop = ["special_features", "rental_length", "rental_length_days", "rental_date", "return_date"]
X = df_rental.drop(cols_to_drop, axis=1)
y = df_rental['rental_length_days']
# Execute train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=9)
# Instantiate the lasso model
lasso = Lasso(alpha=0.3, random_state=9)
lasso.fit(X_train, y_train)
lasso_coef = lasso.coef_ # access the coefficients
# Perform feature selection on columns that has positive coefficients
X_lasso_train, X_lasso_test = X_train.iloc[:, lasso_coef > 0], X_test.iloc[:, lasso_coef > 0]
# Instantiate Linear Regression Model
lr = LinearRegression()
lr.fit(X_lasso_train, y_train)
y_test_pred = lr.predict(X_lasso_test)
mse_linreg_lasso = MSE(y_test, y_test_pred) # this is the MSE Score for Linear Regression Model
# Random Forest Hyperparameters
param_dist = {'n_estimators': np.arange(1,101,1),
'max_depth':np.arange(1,11,1)}
# Instantiate Random Forest Model
rf = RandomForestRegressor()
# Use random search to find the best hyperparameters
random_search = RandomizedSearchCV(rf,
param_distributions=param_dist,
cv=5,
random_state=9)
random_search.fit(X_train, y_train)
hyper_params = random_search.best_params_
# Run the RandomForestRegressor on the selected hyperparameterrs
rf = RandomForestRegressor(n_estimators=hyper_params['n_estimators'],
max_depth=hyper_params['max_depth'],
random_state=9)
rf.fit(X_train, y_train)
rf_pred = rf.predict(X_test)
mse_random_forest = MSE(y_test, rf_pred) # this is the MSE Score for Random Forest Model
# Recommend and save best model
if mse_random_forest < 3:
best_model = rf
best_mse = mse_random_forest
print("Best model: Random Forest Regressor")
print(f"Best MSE: {best_mse:.3f}")
elif mse_linreg_lasso < 3:
best_model = lr
best_mse = mse_linreg_lasso
print("Best model: Linear Regression (Lasso-selected features)")
print(f"Best MSE: {best_mse:.3f}")
else:
best_model = None
best_mse = None
print("No model achieved an MSE below 3.")
# Predict using the best model
y_pred = best_model.predict(X_test)
residuals = y_test - y_pred
# Create a DataFrame for Plot
df_plot = pd.DataFrame({'actual': y_test, 'predicted': y_pred, 'residual': residuals})
# Create subplots
fig, axes = plt.subplots(1, 2, figsize=(14, 6))
# Plot 1: Actual vs. Predicted
sns.scatterplot(x=df_plot['actual'], y=df_plot['predicted'], ax=axes[0])
axes[0].plot([df_plot['actual'].min(), df_plot['actual'].max()],
[df_plot['actual'].min(), df_plot['actual'].max()],
color='red', linestyle='--', label='Perfect Prediction')
axes[0].set_title("Actual vs. Predicted Rental Duration")
axes[0].set_xlabel("Actual Rental Length (days)")
axes[0].set_ylabel("Predicted Rental Length (days)")
axes[0].legend()
axes[0].grid(True)
# Plot 2: Residuals vs. Predicted
sns.scatterplot(x=df_plot['predicted'], y=df_plot['residual'], ax=axes[1])
axes[1].axhline(0, color='red', linestyle='--')
axes[1].set_title("Residuals vs. Predicted Values")
axes[1].set_xlabel("Predicted Rental Length (days)")
axes[1].set_ylabel("Residuals (Actual - Predicted)")
axes[1].grid(True)
plt.tight_layout()
plt.show()