Box Office Revenue Prediction Using Linear Regression in ML
Last Updated : 23 Jul, 2025
The objective of this project is to develop a machine learning model using Linear Regression to accurately predict the box office revenue of movies based on various available features. The model will be trained on a dataset containing historical movie data and will aim to identify key factors that impact revenue. By implementing data preprocessing, feature engineering, visualization and model evaluation techniques, this project seeks to:
Build a predictive model that can estimate the expected revenue of a movie prior to its release.
Provide insights into which features most influence box office success.
Compare linear regression performance with more advanced models (e.g., XGBoost) to assess predictive accuracy.
Here we can observe an unusual discrepancy in the dtype column the columns which should be in the number format are also in the object type. This means we need to clean the data before moving any further.
3. Exploring the Dataset
We take a quick look at statistical metrics (like mean, min, max) for each numeric column to understand the data distribution.
df.describe() gives a summary of the numeric columns (count, mean, standard deviation, min, max, etc.).
.T transposes the output for better readability ( rows become columns and vice versa ).
We clean the data by removing or filling missing values appropriately.
We drop the budget column entirely, likely due to too many missing values.
Fill missing values in MPAA and genres columns using their most frequent values (mode).
Remove any remaining rows with missing values.
Finally, check if any null values remain; the result should be 0.
Output:
0
4.1 Cleaning Numeric Columns Stored as Strings
Some numeric columns might be stored as strings with special characters (like $ or ,). We need to remove these characters and convert the columns back to numeric format.
Remove the first character from 'domestic_revenue' (likely a $ sign).
Remove commas from numeric values (e.g., 1,000 to 1000).
Ensure the columns are properly converted to float types.
Use pd.to_numeric to handle any remaining non-numeric values gracefully to turn them into NaNs.
5. Visualizing MPAA Rating Distribution
We want to see how many movies fall under each MPAA rating category like PG, R, PG-13, etc. We will create a horizontal bar chart showing the count of movies in each MPAA rating.
plt.figure(figsize=(10, 5)) sets the size of the plot.
sb.countplot() from Seaborn automatically counts and plots the frequency of each category in the 'MPAA' column.
Understanding these distributions is important before modeling, as it affects how the model interprets the data.
7. Detecting Outliers Using Boxplots
We use boxplots to visually check for outliers in key numeric features. Boxplots show the spread of data and highlight any outliers (points outside the whiskers).
We create three boxplots side by side, one for each feature (domestic_revenue, opening_theaters, release_days).
This helps us identify unusual values that might affect the model.
We transform the text data in the genres column into separate numeric features using one-hot encoding.
We use CountVectorizer to convert each genre like βActionβ, βComedyβ into a binary feature i.e 1 if the movie belongs to that genre, else 0.
Then drop the original genres text column since itβs now represented numerically.
But there will be certain genres that are not that frequent which will lead to increases in the complexity of the model unnecessarily. So we will remove those genres which are very rare.
9.1 Removing Rare Genre Columns with Mostly Zero Values
We will check for columns between 'action' and 'western' in the DataFrame and drop columns where over 95% of values are zero meaning that genre is rare.
Output:
0 (2694, 12)
This helps simplify the model by focusing on genres that actually appear frequently.
10. Encoding Categorical Columns into Numbers
We use LabelEncoderto replace each unique category with a number like βPGβ to 0, βRβ to 1. This is necessary because machine learning models work better with numbers than text labels.
11. Visualizing Strong Correlations Between Numeric Features
As all the categorical features have been labeled encoded let's check if there are highly correlated features in the dataset.
We will calculate the correlation matrix for all numeric columns.
The plot a heatmap, highlighting pairs of features with correlation greater than 0.8 (very strong correlation).
This helps us identify redundant features that may need to be removed or handled before modeling.
12. Preparing Data for Model Training and Validation
Now we will separate the features and target variables and split them into training and the testing data by using which we will select the model which is performing best on the validation data.
We will remove the title and target column domestic_revenue from the features and set domestic_revenue as the target variable.
We split the data into 90% training and 10% validation sets to evaluate model performance.
Output:
((2424, 10), (270, 10))
12.1 Normalizing Features for Better Model Training
We scale the features to have a mean of 0 and a standard deviation of 1, which helps models learn more effectively.
fit_transform learns scaling parameters from training data and applies scaling.
transform applies the same scaling to validation data without changing the scaler.
This standardization helps the model converge faster and improves stability during training.
13. Training the XGBoost Regression Model
XGBoost library models help to achieve state-of-the-art results most of the time so, we will also train this model to get better results.
Then train the model on the normalized training data (X_train) and target values (Y_train).
14. Evaluating Model Performance on Training and Validation Sets
We use Mean Absolute Error (MAE) to check how well the model predicts revenue on both training and validation data.
We predict revenue for the training data and calculate MAE to measure training error.
Also we predict revenue for the validation data and calculate MAE to measure how well the model generalizes.
Note: Lower MAE means better predictions, it helps identify if the model is overfitting or underfitting.
Output:
Training Error: 0.2104541861999253 Validation Error: 0.6358190127903746
We can observe that :
Training Error (0.21) is low: The model fits the training data quite well.
Validation Error (0.63) is significantly higher than training: This gap suggests the model might be overfitting, meaning it performs well on training data but not as well on unseen (validation) data.