Skip to content

quantize_into_bins_with_labels

quantize_into_bins_with_labels(value, bin_bounds, labels)

Description

Get the label of the bin in which belongs value

Usage

Call or Deploy quantize_into_bins_with_labels ?
Call quantize_into_bins_with_labels directly

The easiest way to use bigfunctions

  • quantize_into_bins_with_labels function is deployed in 39 public datasets for all of the 39 BigQuery regions.
  • It can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
  • (You need to use the dataset in the same region as your datasets otherwise you may have a function not found error)

Public BigFunctions Datasets

Region Dataset
eu bigfunctions.eu
us bigfunctions.us
europe-west1 bigfunctions.europe_west1
asia-east1 bigfunctions.asia_east1
... ...
Deploy quantize_into_bins_with_labels in your project

Why deploy?

  • You may prefer to deploy quantize_into_bins_with_labels in your own project to build and manage your own catalog of functions.
  • This is particularly useful if you want to create private functions (for example calling your internal APIs).
  • Get started by reading the framework page

Deployment

quantize_into_bins_with_labels function can be deployed with:

pip install bigfunctions
bigfun get quantize_into_bins_with_labels
bigfun deploy quantize_into_bins_with_labels

Examples

1. 55 is between 50 and 60 so it is in second bin. --> Function returns Wait for result exam label.

select bigfunctions.eu.quantize_into_bins_with_labels(55, [0, 50, 60, 90, 100], ['Fail', 'Wait for result exam', 'Pass', 'Pass with mention'])
select bigfunctions.us.quantize_into_bins_with_labels(55, [0, 50, 60, 90, 100], ['Fail', 'Wait for result exam', 'Pass', 'Pass with mention'])
select bigfunctions.europe_west1.quantize_into_bins_with_labels(55, [0, 50, 60, 90, 100], ['Fail', 'Wait for result exam', 'Pass', 'Pass with mention'])
+----------------------+
| label                |
+----------------------+
| Wait for result exam |
+----------------------+

2. Lower bounds are inclusive. 50 is then also in second bin. --> Function returns Wait for result exam label.

select bigfunctions.eu.quantize_into_bins_with_labels(50, [0, 50, 60, 90, 100], ['Fail', 'Wait for result exam', 'Pass', 'Pass with mention'])
select bigfunctions.us.quantize_into_bins_with_labels(50, [0, 50, 60, 90, 100], ['Fail', 'Wait for result exam', 'Pass', 'Pass with mention'])
select bigfunctions.europe_west1.quantize_into_bins_with_labels(50, [0, 50, 60, 90, 100], ['Fail', 'Wait for result exam', 'Pass', 'Pass with mention'])
+----------------------+
| label                |
+----------------------+
| Wait for result exam |
+----------------------+

3. -10 is below the lowest bound --> Function returns UNDEFINED_INF. (It returns UNDEFINED_SUP is above the upper bound).

select bigfunctions.eu.quantize_into_bins_with_labels(-10, [0, 50, 60, 90, 100], ['Fail', 'Wait for result exam', 'Pass', 'Pass with mention'])
select bigfunctions.us.quantize_into_bins_with_labels(-10, [0, 50, 60, 90, 100], ['Fail', 'Wait for result exam', 'Pass', 'Pass with mention'])
select bigfunctions.europe_west1.quantize_into_bins_with_labels(-10, [0, 50, 60, 90, 100], ['Fail', 'Wait for result exam', 'Pass', 'Pass with mention'])
+---------------+
| label         |
+---------------+
| UNDEFINED_INF |
+---------------+

4. You can also pass n + 1 labels instead of n - 1 labels (when n is the number of bounds). In that case, values below the first bound will have this first label (instead of UNDEFINED_INF). -10 will then give Lower than very bad!.

select bigfunctions.eu.quantize_into_bins_with_labels(-10, [0, 50, 60, 90, 100], ['Lower than very bad!', 'Fail', 'Wait for result exam', 'Pass', 'Pass with mention', 'Genius!'])
select bigfunctions.us.quantize_into_bins_with_labels(-10, [0, 50, 60, 90, 100], ['Lower than very bad!', 'Fail', 'Wait for result exam', 'Pass', 'Pass with mention', 'Genius!'])
select bigfunctions.europe_west1.quantize_into_bins_with_labels(-10, [0, 50, 60, 90, 100], ['Lower than very bad!', 'Fail', 'Wait for result exam', 'Pass', 'Pass with mention', 'Genius!'])
+---------------------+
| label               |
+---------------------+
| Lower than very bad |
+---------------------+

Use cases

A common use case for the quantize_into_bins_with_labels function is assigning letter grades to students based on their numerical scores.

Imagine a grading system where:

  • 0-50: Fail
  • 50-60: Wait for result exam
  • 60-90: Pass
  • 90-100: Pass with mention

You have a table of student scores:

CREATE TEMP TABLE StudentScores AS
SELECT 'Alice' AS student, 75 AS score UNION ALL
SELECT 'Bob', 55 AS score UNION ALL
SELECT 'Charlie', 92 AS score UNION ALL
SELECT 'David', 45 AS score UNION ALL
SELECT 'Eve', 105 AS score;

You can use the quantize_into_bins_with_labels function to assign letter grades:

SELECT
    student,
    score,
    bigfunctions.us.quantize_into_bins_with_labels(score, [0, 50, 60, 90, 100], ['Fail', 'Wait for result exam', 'Pass', 'Pass with mention']) AS grade
FROM
    StudentScores;

This will return:

+---------+------+----------------------+
| student | score | grade                |
+---------+------+----------------------+
| Alice   |   75 | Pass                 |
| Bob     |   55 | Wait for result exam |
| Charlie |   92 | Pass with mention    |
| David   |   45 | Fail                 |
| Eve     |  105 | UNDEFINED_SUP        |
+---------+------+----------------------+

This clearly shows which grade each student receives based on their score. The UNDEFINED_SUP for Eve indicates her score is above the defined range. You could handle this by adding another bin (e.g., 100-110: Exceptional) or by using an n+1 label approach as shown in the documentation example 4. For example:

SELECT
    student,
    score,
    bigfunctions.us.quantize_into_bins_with_labels(score, [0, 50, 60, 90, 100], ['Lower than very bad!', 'Fail', 'Wait for result exam', 'Pass', 'Pass with mention', 'Genius!']) AS grade
FROM
    StudentScores;

Other use cases could include:

  • Categorizing customer spending: Assign labels like "Low Spender," "Medium Spender," "High Spender" based on purchase amounts.
  • Classifying product sales: Group products into "Low Sales," "Moderate Sales," "High Sales" categories based on units sold.
  • Defining age groups: Assign age ranges to individuals like "Child," "Teenager," "Adult," "Senior."
  • Bucketing sensor data: Categorize sensor readings into different levels (e.g., "Low," "Medium," "High") for easier analysis and alerts.

Essentially, anytime you need to categorize continuous numeric data into discrete labeled bins, quantize_into_bins_with_labels can be helpful.


Need help or Found a bug?
Get help using quantize_into_bins_with_labels

The community can help! Engage the conversation on Slack

We also provide professional suppport.

Report a bug about quantize_into_bins_with_labels

If the function does not work as expected, please

  • report a bug so that it can be improved.
  • or open the discussion with the community on Slack.

We also provide professional suppport.


Show your ❤ by adding a ⭐ on