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
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
# Load the dataset
df = pd.read_csv("rental_info.csv")
# Convert 'rental_date' and 'return_date' to datetime
df['rental_date'] = pd.to_datetime(df['rental_date'])
df['return_date'] = pd.to_datetime(df['return_date'])
# Calculate the rental length in days
df['rental_length_days'] = (df['return_date'] - df['rental_date']).dt.days
# Check unique values in 'special_features'
print(df['special_features'].unique())
# Create dummy variables for 'special_features'
df['deleted_scenes'] = df['special_features'].apply(lambda x: 1 if 'Deleted Scenes' in str(x) else 0)
df['behind_the_scenes'] = df['special_features'].apply(lambda x: 1 if 'Behind the Scenes' in str(x) else 0)
# Verify the number of ones in 'deleted_scenes'
print(f"Number of 'Deleted Scenes': {df['deleted_scenes'].sum()}")
# Prepare features and target
features = ['amount', 'amount_2', 'rental_rate', 'rental_rate_2', 'release_year', 'length', 'length_2', 'replacement_cost', 'deleted_scenes', 'behind_the_scenes',
'NC-17', 'PG', 'PG-13', 'R']
X = df[features]
y = df['rental_length_days']
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=9)
# Initialize models
models = {
'Linear Regression': LinearRegression(),
'Random Forest': RandomForestRegressor(random_state=9)
}
# Train and evaluate each model
best_model = None
best_mse = float('inf')
for name, model in models.items():
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print(f'{name} MSE: {mse}')
if mse < best_mse:
best_mse = mse
best_model = model
# Output the best model and its MSE
print(f'Best Model: {best_model.__class__.__name__}')
print(f'Best MSE: {best_mse}')
A explanation of each part of the code:
Import Required Libraries
import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from sklearn.ensemble import RandomForestRegressor from sklearn.metrics import mean_squared_error
- pandas: Used for data manipulation and analysis.
- train_test_split: Function to split data into training and test sets.
- LinearRegression: Linear regression model from scikit-learn.
- RandomForestRegressor: Random forest regression model from scikit-learn.
- mean_squared_error: Metric to evaluate the performance of regression models.
Load the Dataset
df = pd.read_csv("rental_info.csv")
- pd.read_csv("rental_info.csv"): Reads the CSV file into a DataFrame named
df.
Convert Date Columns to Datetime
df['rental_date'] = pd.to_datetime(df['rental_date']) df['return_date'] = pd.to_datetime(df['return_date'])
- pd.to_datetime(): Converts the 'rental_date' and 'return_date' columns to datetime objects for date calculations.
Calculate Rental Length in Days
df['rental_length_days'] = (df['return_date'] - df['rental_date']).dt.days
- df['return_date'] - df['rental_date']: Calculates the difference between the return and rental dates.
- .dt.days: Converts the time difference to the number of days.
Check Unique Values in special_features
special_featuresprint(df['special_features'].unique())
- df['special_features'].unique(): Prints unique values in the 'special_features' column to understand its contents before creating dummy variables.
Create Dummy Variables for special_features
special_featuresdf['deleted_scenes'] = df['special_features'].apply(lambda x: 1 if 'Deleted Scenes' in str(x) else 0) df['behind_the_scenes'] = df['special_features'].apply(lambda x: 1 if 'Behind the Scenes' in str(x) else 0)
- df['special_features'].apply(lambda x: ...): Applies a lambda function to each value in 'special_features' to create binary columns.
- 'Deleted Scenes' in str(x): Checks if 'Deleted Scenes' is part of the string representation of the feature value.
- 'Behind the Scenes' in str(x): Checks if 'Behind the Scenes' is part of the string representation of the feature value.
Verify the Number of Ones in deleted_scenes
deleted_scenesprint(f"Number of 'Deleted Scenes': {df['deleted_scenes'].sum()}")
- df['deleted_scenes'].sum(): Sums the values in the 'deleted_scenes' column to verify the number of entries with 'Deleted Scenes'.
Prepare Features and Target
features = ['amount', 'amount_2', 'rental_rate', 'rental_rate_2', 'release_year', 'length', 'length_2', 'replacement_cost', 'deleted_scenes', 'behind_the_scenes', 'NC-17', 'PG', 'PG-13', 'R'] X = df[features] y = df['rental_length_days']
- features: List of column names used as features for the regression model.
- X = df[features]: Creates a DataFrame
Xwith selected features. - y = df['rental_length_days']: Series
ycontains the target variable.
Split the Data into Training and Test Sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=9)
- train_test_split(): Splits the dataset into training and test sets.
- test_size=0.2: Uses 20% of the data for testing.
- random_state=9: Ensures reproducibility by setting a seed for the random number generator.
Initialize Models
models = { 'Linear Regression': LinearRegression(), 'Random Forest': RandomForestRegressor(random_state=9) }
- models: Dictionary with model names as keys and initialized models as values.
Train and Evaluate Each Model
best_model = None best_mse = float('inf') for name, model in models.items(): model.fit(X_train, y_train) y_pred = model.predict(X_test) mse = mean_squared_error(y_test, y_pred) print(f'{name} MSE: {mse}') if mse < best_mse: best_mse = mse best_model = model
- best_model: Variable to keep track of the best-performing model.
- best_mse: Variable to keep track of the best mean squared error.
- model.fit(X_train, y_train): Trains the model using the training data.
- model.predict(X_test): Predicts the target values for the test set.
- mean_squared_error(y_test, y_pred): Computes the mean squared error of the model's predictions.
- if mse < best_mse: Updates the best model and MSE if the current model performs better.
Output the Best Model and Its MSE
print(f'Best Model: {best_model.__class__.__name__}') print(f'Best MSE: {best_mse}')
- best_model.class.name: Prints the name of the best model's class.
- best_mse: Prints the best mean squared error achieved by the model.