I have a dataset:

5, 10, 11, 13, 15, 35, 50 ,55, 72, 92, 204, 215

The formula for binning into equal-widths is this (as far as I know) $$width = (max - min) / N$$

I think N is a number that divides the length of the list nicely. So in this case it is 3. Therefore:

width = 70

How do I use that 70 to make the bins?

  • $\begingroup$ You can answer your own question (maybe not now, but tomorrow it will be possible). $\endgroup$ – Davide Giraudo Nov 7 '13 at 10:07
  • 1
    $\begingroup$ Check the list of related questions to the right -> and perhaps try a search. If you can't find another question with an answer or answers that deal with your question, please go ahead and answer it yourself. If there is another question/answer that does answer your question, it would be better to delete. $\endgroup$ – Glen_b Nov 7 '13 at 11:05
  • 1
    $\begingroup$ Two comments: I think that the number of bins is more typically not a factor of the sample size; there's no need for it to be. (Also, more typically, though not always, the first and last bin boundaries are placed at roundish numbers.) -- but the requirements of what you're doing may differ $\endgroup$ – Glen_b Nov 7 '13 at 18:05
  • 2
    $\begingroup$ Mike, you might appreciate this assessment of histograms offered by @Glen_b in response to a related question. $\endgroup$ – whuber Nov 7 '13 at 20:41
  • 2
    $\begingroup$ 3 appears out of mid-air and is never explained. The question is mystifying on several different levels. 0. Why 3 here? 1. Why bin at all? You can show all the values directly in a dot or strip plot or a quantile plot. 2. Even if you bin, there are grounds (aesthetic and other) for nicer numbers such as lower bin limits 0, 50, 100, .. 3. Who says that bins all need to be populated? I have no problem with some empty bins showing gaps in the data. They are less misleading than wider bins. $\endgroup$ – Nick Cox Feb 26 '18 at 10:24

I found the answer. I was somewhat close with the question. The trick is that width is not just width, it is width of each interval.


bin1: 5,10,11,13,15,35,50,55,72 I.e. all values between 5 and 75
bin2: 92 I.e. all values between 75 and 145    
bin3: 204,215 I.e. all values between 145 and 215

And for equal width binning you are given number of required bins and in this case it is 3.

  • 1
    $\begingroup$ I am still not getting this. How did you get N=3 and why not 4 or 2? Also, how did you divide them into 3 bins with those particular data points. I would highly appreciate any references as I also wanted to see how equal probabilities are done (in terms of chi-square ). $\endgroup$ – leviathan Dec 4 '13 at 0:03
  • 1
    $\begingroup$ The answer to 'I am still not getting this. How did you get N=3 and why not 4 or 2?' is that, the question itself asks for three bins with equal width. When the range of the sequence(max-min) is divided by the number of bins (N=3), the output yields the width of each bin(equal width of 70). Now that we have the width of the bin decided, we can easily divide the given sequence according to closed and open intervals to include every element of the sequence to be a member of some interval or the other.Hope this helps. $\endgroup$ – Indu May 7 '17 at 13:20
  • $\begingroup$ So, the question should be described as Binning By Equal-Width with 3 bins? $\endgroup$ – Nick Dong Feb 9 '19 at 8:09

So far, all the answers have proposed a representation of the histogram that is, in a sense, biased. Bias is not necessarily bad, but it is good to recognize it and to be able to control it.

The bias arises because these methods establish a set of bins in which the smallest data value is at the extreme left of the lowest bin and the largest data value is at the extreme right of the highest bin. The bin width is thus the smallest possible that could be used to stuff these data into a desired number of bins. (For some purposes that is ideal.) A more representative bin width would be one that looked as if the bins had not been chosen on the basis of the data. That's more useful for evaluating the histogram in any context where the data are viewed as a random sample. In such a circumstance there would, on average, be about a half a bin occupied beyond each of the data extremes.

For a less biased representation, divide the range of the data $(x_i)$ into $n-1$ pieces rather than $n$ and situate the bins so that the outer halves of the extreme bins are empty. More generally, if you would like to effect some compromise between these approaches, choose a positive number $a$ strictly between $0$ and $2$ and divide the range into $n-a$ pieces. This can immediately be done with the ceiling transformation

$$x \to \lceil \frac{x - m + \delta}{h} \rceil$$

where $m$ is the least value in the data, $M$ is the greatest, the bin width is

$$h = \frac{M-m}{n-a},$$


$$\delta = \frac{nh - (M-m)}{2h}$$

is the amount of extra space on either side of $m$ and $M$ in the histogram.

(BTW, if you wish to specify the bin width $h$ instead of the number of bins, you may independently set the value of $\delta$ (representing the empty space at the left of the leftmost bin) and simply apply the first formula. It will give an integer -- positive when $\delta$ is positive -- indicating the bin assignment.)

When $a=0,$ the minimum data values will be put into a nonexistent bin $0.$ It is conventional to assign them to bin $1.$ (An easy way to accomplish this without an extra programming step is to set $a$ to a very small value, such as $10^{-6}.$) As $a$ increases, the extreme bins become less and less occupied

To illustrate, I generated $50$ values from a uniform distribution and computed $n=4$ equally spaced bins for a range of values of $a.$


The gray backgrounds show the extent of each bin while the heights of the horizontal segments (which are always at the same locations in each plot, since the data never change) depict the data values. They are colored according to their bin assignments. You can see how with $a=0$ the data fully fill the bins while with $a=2$ only the smallest and greatest data values occupy the extreme bins.

Here is the R code I used for assigning arbitrary values y to the bins determined by the data x.

bin <- function(y, x, n, a=1) {
  if (missing(x)) x <- y
  r <- range(x, na.rm=TRUE)
  h <- diff(r) / (n - a)
  delta <- (n * h - diff(r)) / 2
  ceiling((y - r[1] + delta) / h) 

\begin{align}\text{width}= \frac{\max-\min}{ \text{number of bins}} = \frac{215-5}3 =70. \end{align}

$$5, 10, 11, 13, 15, 35, 50 ,55, 72, 92, 204, 215$$

  1. $70+5=75$ (from $5$ to $75)=$Bin $1: 5,10,11,13,15,35,50,55,72$
  2. $70+75=145$ (from $75$ to $145)=$Bin $2:92$
  3. $70+145=215$ (from $145$ to $215)=$Bin $3: 204,215 $
  • $\begingroup$ I got two concerns. Firstly, the list should be sorted or equal width cannot apply but for equal depth, sorting is not required (I saw in lecture notes). Secondly, if 75 appears in the list, should it be put in Bin1 or Bin2? I believe it should be in Bin2 to make consistency. Correct me if I am wrong. $\endgroup$ – Shark Deng Aug 24 '20 at 5:07

Binning is a unsupervised technique of converting Numerical data to categorical data but it do not use the class information. There are two unsupervised technique.

1-Equal width. 2-Equal frequency.

In Equal width, we divide the data in equal widths. In order to calculate width we have the formula.


So from the available data in the problem we have 75 is the width.

We divide the data with three categories:

5 to 75 75 to 145 145 to 215.

Now we keep the numbers in the above ranges.Thus,

bin1: 5,10,11,13,15,35,50,55,72 bin2: 92 will hold only one value as we have only one value exist between 75 and 145
bin3: 204,215.

  • $\begingroup$ This doesn't seem to say anything that isn't in the accepted answer $\endgroup$ – Nick Cox Feb 26 '18 at 11:15
  • 1
    $\begingroup$ why 75 instead of 70? The answer should give an example of 2-Equal frequency, either, since you mentioned this concept here. $\endgroup$ – Nick Dong Feb 9 '19 at 8:06
  • 1
    $\begingroup$ Methods that use binning should always be under suspicion. $\endgroup$ – Frank Harrell Nov 13 '20 at 12:56

I also stumbled on the problem about how to exactly divide the bins, and could not find anything on the internet. So here is how I would formalize the problem to unambiguously assign the items to the $N$ desired bins.

Provided some multiset $L \in \mathbb{R}^n$, $n \in \mathbb{N}^+$ and the number of desired bins $N \in \mathbb{N}^+$.

Calculate the width $w \in \mathbb{R}$ like so (note that $w$ does not have to be a natural number):

\begin{equation*} w := \frac{1}{N} \cdot \bigl(\max(L)-\min(L)\bigr) \end{equation*}

Then calculate the intervals $I_1, \dots, I_N$:

\begin{equation*} I_i := \begin{cases} \Bigl[\min(L) + (i - 1) \cdot w,\ \min(L) + i \cdot w\Bigr),\ &i \in \{1, \dots, N-1\}\\ \Bigl[\min(L) + (N - 1) \cdot w,\ \min(L) + N \cdot w\Bigr], &i = N \end{cases} \end{equation*}

Alternatively, you can use: \begin{equation*} I_i := \begin{cases} \Bigl[\min(L),\ \min(L) + w\Bigr], &i = 1\\ \Bigl(\min(L) + (i - 1) \cdot w,\ \min(L) + i \cdot w\Bigr],\ &i \in \{2, \dots, N\} \end{cases} \end{equation*}

Then fill each bin $B_1, \dots, B_N$ like so: \begin{equation*} B_i := \bigl\{x \in L \mid x \in I_i\bigr\} = L \cap I_i,\ i \in \bigl\{1, \dots, N\bigr\} \end{equation*}

Pseudocode for inputs L[0, ..., n-1] and N desired bins, choosing the last interval as largest:

L := sorted(L)
width := (1/N) * (L[n-1] - L[0])
B[i] := [] for i in {0, ..., N-1}
i := 0
for x in L:
    if x >= L[0] + i * width and i != N-1:
        i := i + 1
    B[i] := B[i] + [x]

With your example $L := \{5, 10, 11, 13, 15, 35, 50 ,55, 72, 92, 204, 215\}$ and $N := 3$, we get the following: \begin{equation*} w = \frac{1}{N} \cdot \bigl(\max(L) - \min(L)\bigr) = \frac{1}{3} \cdot (215 - 5) = 70\\ \begin{aligned} I_1 &= \left[5, 75\right) \longrightarrow B_1 = \{5,10,11,13,15,35,50,55,72\}\\ I_2 &= \left[75, 145\right) \longrightarrow B_2 = \{92,204\}\\ I_3 &= \left[145, 215\right] \longrightarrow B_3 = \{215\} \end{aligned} \end{equation*}


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.