TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Binning

Feature EngineeringData Processing
Easy

Binning (also called discretization) converts continuous numeric features into discrete categories by dividing the value range into equal-width intervals. Each value is assigned to its corresponding bin. This technique can reduce noise, handle outliers, and make features compatible with algorithms that work better with categorical data.

Given a list of numeric values and a number of bins, assign each value to a bin index using equal-width binning.

Algorithm

  1. Compute the bin width from the data range:
w=max⁡(x)−min⁡(x)num_binsw = \frac{\max(x) - \min(x)}{\text{num\_bins}}w=num_binsmax(x)−min(x)​
  1. Assign each value to a bin:
bin(xi)=min⁡(⌊xi−min⁡(x)w⌋,  num_bins−1)\text{bin}(x_i) = \min\left(\left\lfloor \frac{x_i - \min(x)}{w} \right\rfloor, \; \text{num\_bins} - 1\right)bin(xi​)=min(⌊wxi​−min(x)​⌋,num_bins−1)

The maximum value is clamped to the last bin. If all values are equal, all are assigned to bin 0.

Return one integer from zero through num_bins minus one for each value.

Loading visualization...

Examples

Input: values = [0, 25, 50, 75, 100], num_bins = 4

Output: [0, 1, 2, 3, 3]

Explanation: The width is 25, and the maximum is clamped into the final bin.

Input: values = [1, 2, 3, 4, 5, 6], num_bins = 2

Output: [0, 0, 0, 1, 1, 1]

Hint 1

Compute bin width from the observed minimum and maximum.

Hint 2

Convert each offset into an integer bin and clamp the maximum to the final index.

Requirements

  • Divide the range [min, max] into num_bins equal-width intervals
  • Assign each value a 0-indexed bin number
  • Clamp the maximum value to the last bin (num_bins - 1)
  • If all values are identical, assign all to bin 0
  • Return a list of integers

Constraints

  • values has at least 1 element
  • num_bins >= 1
  • Return a list of integers in [0, num_bins - 1]
  • Time limit: 300 ms
Try Similar Problems
Rank TransformZscore StandardizationPolynomial FeaturesInteraction FeaturesLog Transform

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Binning

Feature EngineeringData Processing
Easy

Binning (also called discretization) converts continuous numeric features into discrete categories by dividing the value range into equal-width intervals. Each value is assigned to its corresponding bin. This technique can reduce noise, handle outliers, and make features compatible with algorithms that work better with categorical data.

Given a list of numeric values and a number of bins, assign each value to a bin index using equal-width binning.

Algorithm

  1. Compute the bin width from the data range:
w=max⁡(x)−min⁡(x)num_binsw = \frac{\max(x) - \min(x)}{\text{num\_bins}}w=num_binsmax(x)−min(x)​
  1. Assign each value to a bin:
bin(xi)=min⁡(⌊xi−min⁡(x)w⌋,  num_bins−1)\text{bin}(x_i) = \min\left(\left\lfloor \frac{x_i - \min(x)}{w} \right\rfloor, \; \text{num\_bins} - 1\right)bin(xi​)=min(⌊wxi​−min(x)​⌋,num_bins−1)

The maximum value is clamped to the last bin. If all values are equal, all are assigned to bin 0.

Return one integer from zero through num_bins minus one for each value.

Loading visualization...

Examples

Input: values = [0, 25, 50, 75, 100], num_bins = 4

Output: [0, 1, 2, 3, 3]

Explanation: The width is 25, and the maximum is clamped into the final bin.

Input: values = [1, 2, 3, 4, 5, 6], num_bins = 2

Output: [0, 0, 0, 1, 1, 1]

Hint 1

Compute bin width from the observed minimum and maximum.

Hint 2

Convert each offset into an integer bin and clamp the maximum to the final index.

Requirements

  • Divide the range [min, max] into num_bins equal-width intervals
  • Assign each value a 0-indexed bin number
  • Clamp the maximum value to the last bin (num_bins - 1)
  • If all values are identical, assign all to bin 0
  • Return a list of integers

Constraints

  • values has at least 1 element
  • num_bins >= 1
  • Return a list of integers in [0, num_bins - 1]
  • Time limit: 300 ms
Try Similar Problems
Rank TransformZscore StandardizationPolynomial FeaturesInteraction FeaturesLog Transform

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.