bigfunctions > quantize_into_bins_with_labels
quantize_into_bins_with_labels¶
Call or Deploy quantize_into_bins_with_labels
?
✅ You can call this quantize_into_bins_with_labels
bigfunction directly from your Google Cloud Project (no install required).
- This
quantize_into_bins_with_labels
function is deployed inbigfunctions
GCP project in 39 datasets for all of the 39 BigQuery regions. You need to use the dataset in the same region as your datasets (otherwise you may have a function not found error). - Function is public, so it can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
- You may prefer to deploy the BigFunction in your own project if you want 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). Discover the framework
Public BigFunctions Datasets:
Region | Dataset |
---|---|
eu |
bigfunctions.eu |
us |
bigfunctions.us |
europe-west1 |
bigfunctions.europe_west1 |
asia-east1 |
bigfunctions.asia_east1 |
... | ... |
Description¶
Signature
quantize_into_bins_with_labels(value, bin_bounds, labels)
Description
Get the label
of the bin in which belongs value
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 |
+---------------------+
Need help using quantize_into_bins_with_labels
?
The community can help! Engage the conversation on Slack
For professional suppport, don't hesitate to chat with us.
Found a bug using 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.
For professional suppport, don't hesitate to chat with us.
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.
Spread the word¶
BigFunctions is fully open-source. Help make it a success by spreading the word!