Skip to content
Tutorials Center
My learning

Topics

Lesson 93 of 111 ยท Projects

Project: Box Office Revenue Prediction

Predict movie revenue with regression: tame a skewed target with logs, encode ratings and genres, compare models in dollars and percent, give forecast ranges.

How much will a film earn at the box office? Studios, distributors and cinema chains all want an estimate before release. As a machine learning problem this is a classic regression task with a messy target: a few blockbusters earn hundreds of times more than a typical film, so revenue is extremely right-skewed, and ordinary linear regression on the raw dollars behaves badly.

This project builds a revenue predictor on a synthetic table of 1,500 films (budget, runtime, genre, MPAA rating, release month, sequel flag and the number of famous actors in the cast), so the lesson runs anywhere. Real datasets (for example the public movie metadata collections) look the same, with more columns and more gaps. You will learn how to:

  1. explore a skewed target, spot outliers and see why logarithms are the standard fix,
  2. encode an ordered category (the rating) and unordered ones (the genre) correctly,
  3. compare models in units people understand (dollars and percent errors), and
  4. report a forecast range, not just a single number.

Related lessons: linear regression, regression metrics, categorical encoding, outliers and house prices project.

Step 1: Explore the data

For a regression project, the first plot (or, here, the first numbers) is always the distribution of the target. We also check how revenue differs between ratings and seasons, and how it relates to the budget.

Python Step 1: the shape of revenue

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The average film earns $181 million but the median film earns only $106 million, and the best earns $3 billion (we capped the synthetic maximum). The skewness of revenue is 4.55, a strongly right-skewed distribution; after taking the logarithm it is -0.07, which is essentially symmetric. The IQR rule from the outlier lesson flags 134 films (8.9%) on the raw scale, but only 8 on the log scale. That is the usual lesson: most of those "outliers" are not errors, they are simply big hits that look extreme only because the scale is wrong. Delete none of them.

Other patterns to remember: R-rated films have the lowest median revenue ($73 million against $126 to $132 million for PG and PG-13), summer and holiday releases earn about 55% more than releases in other months, and the budget is related to revenue (correlation 0.70 on raw dollars; on the log scale the relationship is cleaner, 0.73).

Step 2: Choose the target scale and the encoding

Four decisions shape the model:

  • Log the target. Revenue is multiplicative: a better genre or a summer release multiplies it by some factor, it does not add a fixed number of dollars. On the log scale these effects become additive, which is what a linear model assumes. We train on log(revenue) and convert the predictions back with exp (TransformedTargetRegressor does both steps).
  • Log the budget for the same reason.
  • Ordinal encoding for the rating. G, PG, PG-13 and R have a natural order, so one column with 0, 1, 2, 3 keeps it. The genre has no order, so it is one-hot encoded.
  • A "summer or holiday" flag from the release month, because the month number itself (12 is not "larger" than 1 in any useful way) is a poor linear feature.

We compare a baseline that predicts the mean revenue, a linear model on raw revenue, a linear model on the log scale and gradient boosting on the log scale. Metrics are in the user's units: the mean absolute error in millions of dollars, the median percentage error (how far off a typical prediction is), and the Rยฒ on the log scale.

Python Step 2: raw versus log target

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The model trained on raw dollars barely improves on the baseline: its Rยฒ on the log scale is negative (-0.19), which means that, measured on the log scale, it is worse than predicting the same typical value for every film. The log-log linear model is much better: the mean absolute error falls from $85 million to $75 million, and its typical prediction is off by 39% instead of 53%. Gradient boosting is no better here (40%), because the true relationship in this table is multiplicative and linear on the log scale. A flexible model cannot beat a model that matches the structure of the data, and a simple model with the right target transformation is often the best one.

Step 3: Interpret the model and report a range

The log-linear model has an attractive property: each coefficient b means "multiply revenue by e^b", or roughly "change revenue by 100ยทb percent" for small b. That makes the model something you can explain to a producer. We also turn the residuals (the model's errors on the log scale) into an 80% prediction range: multiply the point forecast by the 10th and 90th percentile of the training residuals.

Python Step 3: effects in percent and a forecast range

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The budget has an elasticity of 0.92: a 10% larger budget goes with about 9.2% more revenue, so revenue per dollar spent is nearly constant (in the real world, extra spending on a star or on marketing does not scale so cleanly, but the question is a good one to ask your data). Relative to dramas, action films and animation earn about twice as much, a sequel adds about 60%, an R rating costs about 35%, and a summer or holiday release adds about 29%. The final forecast is a range: for the example film, the median forecast is $546 million, with an 80% range of $261 million to $1.2 billion. Such a wide range is honest, since a typical error of 39% was seen in Step 2, and the check at the end confirms that the range is calibrated: 82% of the test films fall inside theirs. A single number such as "$546M" would sound more precise than it is.

Taking it further

  • Add real signals: the director's track record, the number of screens, marketing spend, pre-release social buzz. Beware of leakage: opening-weekend revenue is a great predictor of total revenue, but it is only known after release.
  • Handle high-cardinality fields (actor and director names) with target encoding fitted inside cross-validation (categorical encoding).
  • Regularize when you have many dummy columns (regularization).
  • Check residuals by budget level and genre for patterns; if the errors grow with the budget, model the variance too, or use quantile regression.
  • Respect time: train on older films and test on newer ones, since the market changes (cross-validation for special data).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is revenue usually modeled on a log scale?

    On the log scale, a genre or season effect becomes a constant shift, which is what linear regression expects, and giant hits stop dominating the fit.
  2. 2. The rating (G, PG, PG-13, R) is encoded as one ordinal column while the genre is one-hot encoded. Why?

    Use ordinal codes only when the order means something. For unordered categories they would invent a false ranking.
  3. 3. A coefficient of 0.47 on the log scale for 'sequel' means roughly what?

    On the log scale, adding b multiplies the predicted revenue by exp(b). exp(0.47) is about 1.6.
  4. 4. Why report an 80% range instead of one number?

    A point forecast hides uncertainty. A calibrated range (here, 82% of test films fell inside their 80% ranges) shows how much to trust it.

Summary

  • Revenue is right-skewed and multiplicative: model its logarithm and convert the predictions back, and keep the "outliers", which are mostly real hits.
  • Use ordinal encoding for ordered categories (ratings), one-hot for unordered ones (genres) and derived flags (a summer release) for awkward numbers such as the month.
  • Judge models in understandable units (dollars and percent error) next to Rยฒ, and compare against a mean baseline.
  • A log-linear model is easy to explain: each coefficient is a percentage effect. Residual quantiles turn it into a forecast range.
  • Avoid leakage from post-release information such as opening-weekend sales.

Back to the project list, or try a classification problem on tabular health data: Heart Disease and Breast Cancer Diagnosis.