Skip to main content
Colin Jaffe/2 min read

Creating a User Input for Percentile Age Analysis with NumPy

Machine Learning Essentials

Supervised vs Unsupervised

Labeled data vs unlabeled — different problem classes.

Classification vs Regression

Predict a class label vs a continuous number.

Train/Test Split

Always evaluate on data the model never saw during training.

Hyperparameter Tuning

Grid search and cross-validation to find the best settings.

Master Machine Learning at Noble Desktop

Noble Desktop's Python Machine Learning Bootcamp covers scikit-learn, Keras, neural networks, and applied ML.

This lesson is a preview from our Data Science & AI Certificate Online (includes software) and Python Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.

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.

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.