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.
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.