Creating a User Input for Percentile Age Analysis with NumPy

Create an input box to accept a percentile from the user, convert it to an integer, and calculate the corresponding age using NumPy's percentile function.

Create an interactive percentile calculator using Python and NumPy. Learn how to input data and retrieve meaningful insights about age distributions.

Key Insights

  • Utilize Python's built-in input() function to create an interactive prompt, allowing users to enter a desired percentile for analysis.
  • Convert user input into an integer data type to effectively integrate it with NumPy's percentile function for accurate computation.
  • Apply NumPy's percentile function to the age data set, enabling the calculation of precise age thresholds, such as determining that 90% of people are younger than age 61.

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.

Let’s take a look at one possible solution to this challenge. We’ll create an input box for entering a percentile, which will return the age below which that percentage of people fall. So let’s define a user input.

`user_input` is a fine name for this variable. We’ll say it’s the result of the input prompt: “What percentile do you want to look at in the data?” For example, if we input 90, we’re asking for the 90th percentile.

If we run this and print `user_input`, the behavior depends on your interface. In Google Colab, you get a small input box that displays the question you asked.

Let’s say we enter 90. The value returned is `'90'`, which is a string. We need to convert the result to an integer using `int()`.

Now that we’ve got the integer value from the user input, we can pass that to NumPy’s `percentile()` function. The result will be the output of NumPy’s `percentile()` function, which we’ve already used.

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.

We’ll pass in the `ages` list. The second argument is the percentile value from the user input—75,90, or whatever was entered. Now that we have the age result, we can print it out.

Then we’ll use an f-string to print: `"{user_input}% of people are younger than {age}"`. Let’s give that a try and see if it works as expected.

If we input 90, the output might be: “90% of people are younger than 61.” We might consider rounding this.

We don’t need to include the decimal point. We can also test it with another value—for instance, the 40th percentile. If we enter 40, we’d expect the result to be: “40% of people are younger than 27.”

And that’s one possible solution to this challenge.

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