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
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
import matplotlib.pyplot as plt
# Import any additional modules and start coding below#criando dataframe
dvd=pd.read_csv('rental_info.csv')
#modelo nao pode ter missing, verificando se tem missing
print(dvd.head(),dvd.isna().sum())
#as variaveis precisam ser numericas, entao transformo strings em datas
dvd['return_date']=pd.to_datetime(dvd['return_date'])
dvd['rental_date']=pd.to_datetime(dvd['rental_date'])
#adicionando a coluna rental_lenght_days que descobre quantos dias foi alugado
dvd['rental_length_days']=dvd['return_date']-dvd['rental_date']
dvd['rental_length_days'] = dvd['rental_length_days'].dt.days
#a coluna special_features é objeto, preciso transformar em dummies numéricas 0-1 se for deleted scenes ou behind the scenes
dvd['deleted_scenes']=dvd['special_features'].apply(lambda x: 1 if "Deleted Scenes" in x else 0)
dvd['behind_the_scenes']=dvd['special_features'].apply(lambda x: 1 if "Behind the Scenes" in x else 0)
#verificando se foi certo a criação da dummie
print(dvd['deleted_scenes'].value_counts())
print(dvd['behind_the_scenes'].value_counts())
#separando os features em X
X=dvd.drop(['rental_length_days','special_features','rental_date','return_date'],axis=1)
y=dvd['rental_length_days']
#separando os valores em teste e treino
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2,random_state=9)
#treinando o 1o modelo: regressao linear
best_model=LinearRegression()
best_model.fit(X_train,y_train)
y_pred1=best_model.predict(X_test)
best_mse=mean_squared_error(y_test,y_pred1,squared=False)
print(mse1)
#treinando o 2o modelo, só usando as features importantes
#separando os valores em teste e treino
X2=X[['amount','length_2']]
print(X2)
X_train, X_test, y_train, y_test = train_test_split(X2,y,test_size=0.2,random_state=9)
reg2=LinearRegression()
reg2.fit(X_train,y_train)
y_pred2=reg2.predict(X_test)
mse2=mean_squared_error(y_test,y_pred2,squared=False)
print(mse2)
lasso = Lasso(alpha=0.1)
lasso_coef = lasso.fit(X, y).coef_
plt.bar(X.columns, lasso_coef)
plt.xticks(rotation=45)
plt.show()