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.linear_model import Lasso
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestRegressor
# Import any additional modules and start coding below
#read csv
rental_df = pd.read_csv('rental_info.csv')
#convert columns to date time
rental_df["return_date"] = pd.to_datetime(rental_df["return_date"])
rental_df["rental_date"] = pd.to_datetime(rental_df["rental_date"])
#no. of rental days
rental_df['rental_length'] = rental_df["return_date"] - rental_df["rental_date"]
rental_df['rental_length_days'] = rental_df['rental_length'].dt.days
#create two columns of dummy variables
rental_df["deleted_scenes"] = np.where(rental_df["special_features"].str.contains("Deleted Scenes"), 1,0)
rental_df["behind_the_scenes"] = np.where(rental_df["special_features"].str.contains("Behind the Scenes"), 1,0)
# Drop the 'special_features' column
rental_df = rental_df.drop("special_features", axis=1)
X = rental_df.drop(["rental_length_days","rental_date", "return_date","rental_length"], axis=1)
y = rental_df["rental_length_days"]
#split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=9)
#instantiate the model
lasso = Lasso(random_state=9, alpha=0.01)
lasso.fit(X_train, y_train)
lasso_coef = lasso.coef_
# Subset training and test features based on non-zero coefficients
selected_features_train = X_train.iloc[:, lasso_coef > 0]
selected_features_test = X_test.iloc[:, lasso_coef > 0]
# Display the selected features
print("Selected Features for Training:")
print(selected_features_train)
print("\nSelected Features for Testing:")
print(selected_features_test)
#rental_df.dtypes
# Define the hyperparameter ranges for Random Forest
param_dist = {
'n_estimators': [10, 50, 100, 150, 200],
'max_features': ['auto', 'sqrt', 'log2'],
'max_depth': [None, 10, 20, 30, 40, 50],
'min_samples_split': [2, 5, 10, 15, 20],
'min_samples_leaf': [1, 5, 10, 15, 20],
}
#fit the model
best_model = RandomForestRegressor()
# Create a RandomizedSearchCV object
random_search = RandomizedSearchCV(
best_model,
param_distributions=param_dist,
n_iter=10,
cv=5,
random_state=9,
n_jobs=-1
)
random_search.fit(X_train, y_train)
# Print the best hyperparameters found
print("Best Hyperparameters:",random_search.best_params_)
# print estimators
model = random_search.best_estimator_
print("Best Estimators:",model)
#predictions
pred = model.predict(X_test)
#fit model
best_model.fit(X_train, y_train)
y_pred = best_model.predict(X_test)
best_mse = mean_squared_error(y_test, y_pred)
print("The mse is", best_mse)