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 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 below
# 1. Load the data, parsing both date columns
df = pd.read_csv('rental_info.csv', parse_dates=['rental_date', 'return_date'])

# 2. Preview the data
print(df.head())

# 3. Check data types and missing values
print(df.info())
# 1. Subtracting datetime columns:
df['rental_length_days'] = (df['return_date'] - df['rental_date']).dt.days

# 2. Quick sanity-check by viewing the first few rows:
print(df[['rental_date','return_date','rental_length_days']].head())
df['deleted_scenes'] = df['special_features'].str.contains("Deleted Scenes").astype(int)
df['behind_the_scenes'] = df['special_features'].str.contains("Behind the Scenes").astype(int)

# Let's check a few rows to be sure
print(df[['special_features', 'deleted_scenes', 'behind_the_scenes']].head(100))
#  y (target)
y = df['rental_length_days']

#  drop unnecessary columns for X 
X = df.drop(columns=['rental_date', 'return_date', 'rental_length_days', 'special_features'])
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=9)
# Quick check
print("Train shape:", X_train.shape)
print("Test shape:", X_test.shape)
from sklearn.linear_model import LassoCV

# Step 1: Create and fit the model
lasso = LassoCV(cv=5, random_state=9)
lasso.fit(X_train, y_train)

# Step 2: Check which features Lasso kept (non-zero coefficients)
lasso_coefs = pd.Series(lasso.coef_, index=X_train.columns)

# Step 3: Filter out zero-coefficient features
selected_features = lasso_coefs[lasso_coefs != 0].index.tolist()

# Show what features were kept
print("Selected features by Lasso:", selected_features)
# Keeping  Lasso selected features
X_train_selected = X_train[selected_features]
X_test_selected = X_test[selected_features]
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor

models = {
    "LinearRegression": LinearRegression(),
    "DecisionTree": DecisionTreeRegressor(random_state=9),
    "RandomForest": RandomForestRegressor(random_state=9)
}
from sklearn.model_selection import RandomizedSearchCV
from sklearn.metrics import mean_squared_error

param_dist = {
    'model__n_estimators': [50, 100, 200],
    'model__max_depth': [None, 5, 10, 20],
    'model__min_samples_split': [2, 5, 10]
}

results = {}
for name, model in models.items():
    pipe = Pipeline([('model', model)])
    
    if name == "RandomForest":
        search = RandomizedSearchCV(pipe, param_distributions=param_dist, cv=5, random_state=9)
        search.fit(X_train_selected , y_train)
        best_model = search.best_estimator_
    else:
        best_model = pipe.fit(X_train_selected , y_train)
    
    y_pred = best_model.predict(X_test_selected )
    mse = mean_squared_error(y_test, y_pred)
    results[name] = {'model': best_model, 'mse': mse}
best_model_name = min(results, key=lambda k: results[k]['mse'])
best_model = results[best_model_name]['model']
best_mse = results[best_model_name]['mse']

print("Best model:", best_model_name)
print("Best MSE:", best_mse)
# Start your coding from below
import pandas as pd
import numpy as np

from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

# For lasso
from sklearn.linear_model import Lasso
#from sklearn.preprocessing import StandardScaler

# Run OLS
from sklearn.linear_model import LinearRegression

# Random forest
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import RandomizedSearchCV

# Read in data
df_rental = pd.read_csv("rental_info.csv")

# Add information on rental duration
df_rental["rental_length"] = pd.to_datetime(df_rental["return_date"]) - pd.to_datetime(df_rental["rental_date"])
df_rental["rental_length_days"] = df_rental["rental_length"].dt.days

### Add dummy variables
# Add dummy for deleted scenes
df_rental["deleted_scenes"] =  np.where(df_rental["special_features"].str.contains("Deleted Scenes"), 1, 0)
# Add dummy for behind the scenes
df_rental["behind_the_scenes"] =  np.where(df_rental["special_features"].str.contains("Behind the Scenes"), 1, 0)

# Choose columns to drop
cols_to_drop = ["special_features", "rental_length", "rental_length_days", "rental_date", "return_date"]

# Split into feature and target sets
X = df_rental.drop(cols_to_drop, axis=1)
y = df_rental["rental_length_days"]

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

# Create the Lasso model
lasso = Lasso(alpha=0.3, random_state=9) 

# Train the model and access the coefficients
lasso.fit(X_train, y_train)
lasso_coef = 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]

# Run OLS models on lasso chosen regression
ols = LinearRegression()
ols = ols.fit(X_lasso_train, y_train)
y_test_pred = ols.predict(X_lasso_test)
mse_lin_reg_lasso = mean_squared_error(y_test, y_test_pred)

# Random forest hyperparameter space
param_dist = {'n_estimators': np.arange(1,101,10),  # Reduced the range and step size
              'max_depth':np.arange(1,11,2)}  # Reduced the range and step size

# Create a random forest regressor
rf = RandomForestRegressor()

# Use random search to find the best hyperparameters
rand_search = RandomizedSearchCV(rf, 
                                 param_distributions=param_dist, 
                                 cv=5, 
                                 random_state=9,
                                 n_iter=10)  # Limit the number of iterations

# Fit the random search object to the data
rand_search.fit(X_train, y_train)

# Create a variable for the best hyper param
hyper_params = rand_search.best_params_

# Run the random forest on the chosen hyper parameters
rf = RandomForestRegressor(n_estimators=hyper_params["n_estimators"], 
                           max_depth=hyper_params["max_depth"], 
                           random_state=9)
rf.fit(X_train,y_train)
rf_pred = rf.predict(X_test)
mse_random_forest= mean_squared_error(y_test, rf_pred)

# Random forest gives lowest MSE so:
best_model = rf
best_mse = mse_random_forest