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, KFold, cross_val_score, GridSearchCV
from sklearn.metrics import mean_squared_error
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline, Pipeline
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.svm import SVR
import matplotlib.pyplot as plt
from sklearn.model_selection import GridSearchCV
# Import any additional modules and start coding belowdf_rental = pd.read_csv("rental_info.csv")
df_rentaldf_rental.info()df_rental.describe()df_rental["special_features"].unique()Preprocessed
Create a new columns called "rental_length_days"
#Extract columns and convert them to date time
col_date1 = pd.to_datetime(df_rental["rental_date"])
col_date2 = pd.to_datetime(df_rental["return_date"])# Compute the difference between the two days
diff_date = col_date2-col_date1
col_rental_length_days = []
for idx, row in diff_date.items():
col_rental_length_days.append( row.days )# Create new column 'rental_length_days'
df_prep = df_rental.drop( ["rental_date", "return_date"], axis=1 )
df_prep["rental_length_days"] = col_rental_length_daysCreate two columns of dummy variables from "special_features"
# Find specific scenes and return -1 on failure
dlt_scenes_find = df_prep["special_features"].str.find('Deleted Scenes')
bhd_scenes_find = df_prep["special_features"].str.find('Behind the Scenes')# Create two new columns for special features
df_prep = df_prep.drop( "special_features", axis=1 )
df_prep["deleted_scenes"] = (dlt_scenes_find != -1).astype(int)
df_prep["behind_the_scenes"] = (bhd_scenes_find != -1).astype(int)Feature selection using randomforest