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 belowrental_info = pd.read_csv('rental_info.csv')
rental_info#formatting data
rental_info.dtypes#getting rental_length_days
rental_info['rental_length_days'] =(pd.to_datetime(rental_info['return_date']) - pd.to_datetime(rental_info['rental_date'])).dt.days
rental_info[['rental_date','return_date','rental_length_days']].head()#adding deleted scenes and behind the scenes
rental_info['deleted_scenes'] = np.where(rental_info['special_features'].str.contains('Deleted Scenes'), 1, 0)
rental_info['behind_the_scenes'] = np.where(rental_info['special_features'].str.contains('Behind the Scenes'), 1, 0)
rental_info.head()#splitting data for predictions'
X = rental_info.drop(['rental_length_days', 'rental_date', 'return_date', 'special_features'], axis=1)
y = rental_info['rental_length_days']
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
from sklearn.model_selection import GridSearchCV
import matplotlib.pyplot as plt
lasso_model = Lasso(alpha=0.0014677992676220691, random_state=9)
lasso_model.fit(X_train, y_train)
# param_alpha = {'alpha':np.logspace(-4, 0, 25)}
# grid_search = GridSearchCV(estimator=lasso_model, param_grid=param_alpha, cv=5, scoring='neg_mean_squared_error')
# grid_search.fit(X_train, y_train)
# best_params = grid_search.best_params_
# best_params
lasso_coeff = lasso_model.coef_
print(lasso_coeff)
plt.bar(X.columns, lasso_coeff)
plt.xticks(rotation=90)
plt.show()#linear regression
from sklearn.linear_model import LinearRegression
X_lasso_train, X_lasso_test = X_train.iloc[:, lasso_coeff > 0], X_test.iloc[:, lasso_coeff > 0]
li_model = LinearRegression()
li_model.fit(X_lasso_train, y_train)
y_pred = li_model.predict(X_lasso_test)
mse_li_model_lasso = mean_squared_error(y_test, y_pred)
print(mse_li_model_lasso)
#Random Forest
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import RandomizedSearchCV
rf_model = RandomForestRegressor(random_state=9)
rf_model.get_params()
param_rf = {'n_estimators': np.arange(1, 101, 1),
'max_depth': np.arange(1, 11, 1)}
grid_search = RandomizedSearchCV(rf_model, param_distributions=param_rf, cv=5, scoring='neg_mean_squared_error')
grid_search.fit(X_train, y_train)
best_params = grid_search.best_params_
best_paramsrf_model = RandomForestRegressor(n_estimators=best_params['n_estimators'], max_depth=best_params['max_depth'], random_state=9)
rf_model.fit(X_train, y_train)
y_pred = rf_model.predict(X_test)
mse_rf_model = mean_squared_error(y_test, y_pred)
print(mse_rf_model)best_model = rf_model
best_mse = mse_rf_model