TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
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.

Loading visualization...

Examples

Input:

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

Output:

[0, 1, 2, 3, 3]

Range is 100, width = 25. Bins: [0-25), [25-50), [50-75), [75-100]. Value 100 is clamped to bin 3.

Input:

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

Output:

[0, 0, 0, 1, 1, 1]

Range is 5, width = 2.5. Values 1-3 fall in bin 0 [1, 3.5), values 4-6 fall in bin 1 [3.5, 6].

Hint 1

Compute min_val and max_val. If they are equal, return all zeros. Otherwise compute bin_width = (max_val - min_val) / num_bins. For each value, compute int((v - min_val) / bin_width) and clamp to num_bins - 1.

Hint 2

The clamping with min(bin_idx, num_bins - 1) is needed because the maximum value exactly equals max_val, and (max_val - min_val) / bin_width = num_bins, which would be out of range.

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.

Loading visualization...

Examples

Input:

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

Output:

[0, 1, 2, 3, 3]

Range is 100, width = 25. Bins: [0-25), [25-50), [50-75), [75-100]. Value 100 is clamped to bin 3.

Input:

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

Output:

[0, 0, 0, 1, 1, 1]

Range is 5, width = 2.5. Values 1-3 fall in bin 0 [1, 3.5), values 4-6 fall in bin 1 [3.5, 6].

Hint 1

Compute min_val and max_val. If they are equal, return all zeros. Otherwise compute bin_width = (max_val - min_val) / num_bins. For each value, compute int((v - min_val) / bin_width) and clamp to num_bins - 1.

Hint 2

The clamping with min(bin_idx, num_bins - 1) is needed because the maximum value exactly equals max_val, and (max_val - min_val) / bin_width = num_bins, which would be out of range.

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.