Skip to content

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 libraries
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

# Import rental_info dataset
rental_info=pd.read_csv('rental_info.csv')
rental_info_backup=rental_info.copy()

check and clean data


#change date objects to datetime
rental_info['rental_date']=pd.to_datetime(rental_info['rental_date'])
rental_info['return_date']=pd.to_datetime(rental_info['return_date'])

#get the number of days 
rental_info['rental_length']=rental_info['return_date']-rental_info['rental_date']
rental_info['rental_length_days']=rental_info['rental_length'].dt.days
rental_info=rental_info.drop('rental_length',axis=1)

#display the cleaned file and info
display(rental_info.head())
display(rental_info.info())
display(rental_info.describe())

dummy variables

#Convert special features into two dummy columns
rental_info['deleted_scenes']=np.where(rental_info['special_features'].str.contains('Deleted Scenes'),1,0)

#extract behind the scenes clause as 0 or 1 
rental_info["behind_the_scenes"] = np.where(rental_info["special_features"].str.contains("Behind the Scenes"), 1, 0)

train test split


#drop the irrelevant columns and the target column and set the features
X = rental_info.drop(columns=['rental_date', 'return_date', 'rental_length_days','special_features'],axis=1)

#now set the target
y = rental_info['rental_length_days']

train test split

#split the data into train and test sets
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=9)

Lasso regression

from sklearn.linear_model import Lasso
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

#perform a lasso regression analysis
lasso = Lasso(alpha=0.3, random_state=9)
lasso.fit(X_train, y_train)
lasso_coef = lasso.coef_
print(lasso.coef_)

# Perform feature selection by choosing columns with positive coefficients
X_lasso_train, X_lasso_test = X_train.iloc[:, lasso_coef > 0], X_test.iloc[:, lasso_coef > 0]
lasso_plus = lasso_coef[lasso_coef > 0]

#plot bars of positive lasso coefficients
plt.bar(X_lasso_train.columns, lasso_plus)
plt.xticks(rotation=90) 
plt.show()

linear regression

#import libraries
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error as MSE
from sklearn.model_selection import cross_val_score

#perform a linear regression and calculate rmse and score
reg=LinearRegression()
reg.fit(X_train, y_train)
y_pred=reg.predict(X_test)
rmse=MSE(y_test, y_pred)**(1/2)
score=reg.score(X_test, y_test)
print(f'rmse {rmse} ')
print(f'score {score}')

#print cv scores 
cv_scores=cross_val_score(reg, X_test, y_test, cv=5)
print(cv_scores)

decision tree regressor