Streamlining Revenue Predictions with Python Functions

Create a Python function to predict concession revenue from attendance using linear regression.

Learn how to simplify and reuse concession revenue predictions by encapsulating calculations within Python functions. Enhance readability and maintainability in your code through strategic function implementation.

Key Insights

  • Implementing a Python function named predict_concessions streamlines concession revenue predictions, making it easy to estimate, for example, approximately $70,000 for an attendance of 25,000 fans.
  • Using descriptive function names like predict_concessions enhances readability, clearly indicating the function's purpose and improving code clarity.
  • Encapsulating calculations within functions allows centralized modifications; changing the rounding method to integers in just one location automatically updates all predictions moving forward.

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

This calculation up here is the calculation we want, but we ultimately are going to want that to be something we reuse. So to do that, we're going to put it into a Python function. And this is a very good idea, and you'll see why for a number of reasons.

We're going to define a function called `predict_concessions`, and it takes in attendance. Since that's already a variable, to avoid ambiguity, we'll call this one `days_attendance`, like so. Okay.

To predict concessions from one day's attendance, we're going to return our m value times one day's attendance plus b. Okay. Let's see if that works. If we have 25,000 fans—if the ballpark comes to us and says, “Hey, DataWiz, we're expecting 25,000 fans today, how much concessions revenue can we expect?”—we say, “Okay, I've got a function for that.”

I'm going to run `predict_concessions(2,000)`. What we get back is—oh, about 70,000. And yeah, that looks about right.

So what it’s returning is: given that X, where on the line is it going to be? On average, what can we expect the concessions to be? And we can expect it to be right around there. That's our estimate.

Data Analytics Certificate: Live & Hands-on, In NYC or Online, 0% Financing, 1-on-1 Mentoring, Free Retake, Job Prep. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

That's our prediction. What about for another value? I’m going to make this into an official print statement. Print: “Concessions revenue for 25,000.”

Is that? Yeah. Great. Let's try another number.

Concessions revenue for—what was the other big gap here? We haven't seen a number around 28,000. We’ll run `predict_concessions(2,000)`.

Get that value. All right. So here are some of the reasons to make a little function like this.

Even though it is a fairly simple calculation as math formulas go. And the reason is twofold.

One, we can use a more human-readable name. Hey, if I run `predict_concessions` and give it an attendance, it will predict the concessions for me. It's very clear when we write this code what it is we're doing.

The other thing is that we can now work on this in one place. If we want to change how we predict concessions, it’s encapsulated in one area, as the term goes. If we look at this—this is ugly. That is an ugly amount.

Not only does the ballpark probably not care how many tens of thousands or hundreds of thousands of a cent we can expect, but they probably don't care about cents at all. In fact, we might even consider rounding it to a whole number.

But let's at least make it an integer. Let's round it to the nearest integer. And we'll do that by, before we return this value, this calculation, we'll run it through the `int()` function.

Then return what that returns. Now we’ve changed it in one place. And anytime we run `predict_concessions`, it will give it to us rounded to the nearest dollar.

So as we continue to use `predict_concessions` in this lesson, we'll see the advantage of having made that change in one place—not having to continue to run an increasingly customized calculation as we change it and having to update it in countless places. Right now what we have is a linear regression-based prediction of how much to expect in concessions given an attendance.

Colin Jaffe

Colin Jaffe is a programmer, writer, and teacher with a passion for creative code, customizable computing environments, and simple puns. He loves teaching code, from the fundamentals of algorithmic thinking to the business logic and user flow of application building—he particularly enjoys teaching JavaScript, Python, API design, and front-end frameworks.

Colin has taught code to a diverse group of students since learning to code himself, including young men of color at All-Star Code, elementary school kids at The Coding Space, and marginalized groups at Pursuit. He also works as an instructor for Noble Desktop, where he teaches classes in the Full-Stack Web Development Certificate and the Data Science & AI Certificate.

Colin lives in Brooklyn with his wife, two kids, and many intricate board games.

More articles by Colin Jaffe

How to Learn Machine Learning

Master machine learning with hands-on training. Use Python to make, modify, and test your own machine learning models.

Yelp Facebook LinkedIn YouTube Twitter Instagram