This community-built FAQ covers the “Bins and Count I” exercise from the lesson “Histograms”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Learn Statistics With Python
FAQs on the exercise Bins and Count I
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Need broader help or resources? Head here.
Looking for motivation to keep learning? Join our wider discussions.
Learn more about how to use this guide.
Found a bug? Report it!
Have a question about your account or billing? Reach out to our customer support team!
None of the above? Find out where to ask other questions here!
Can someone please clarify on why we are adding 1 for calculating the binwidth calculation.
I’d hazard a guess it’s just to make the number an integer so that the values can be binned from-
rather than 2.666…
I don’t see, how this answers the above question. Just go through some examples.
min_value = 3
max_value = 9
range = max_value - min_value = 6
If have three bins of the same size 3 and 9 their width will be 2 (from 6/3).
But (9 - 3 + 1) = 7. If we divide this into the number of bins we get a width of 7/3 =2.333
TH eplus one seems to be inaccurate in the lesson. Specially if you look firther in the lesson when coding histograms with numpy and matplotlib.
In both you give as an input the range and number of bins. And the width of the bin is (“range” / “number of bins”).
I’m not sure I’d actually done this lesson at the time I answered and I assumed it was for plotting and all they wanted was integer bin widths; I’m not suggesting you add 1 to any old range . You don’t have to use a fixed number of bins or even fixed bin sizes ; notably you may even choose bins that have different widths if it’s more useful for a particular data set. Binning a set of discrete values but plotting with edges at
8 / 3 and similar is a little confusing and I think they all they’ve done is alter the bin widths to avoid that. If I’ve found the right lesson it probably would’ve been better to stick with 8 bins for the 8 values and if it must be compressed then perhaps 4 bins so the edges still make sense. Whilst there are methods of trying to automatically choose bins sometimes you may wish to fix your own if it better represents the data.
Sort of off topic:
The link at the top of this FAQ doesn’t even go to the correct part in the course.
It should be:
As it is now, it just goes to the syllabus. This happens often as these FAQs aren’t really maintained/updated with the course content.
I think a lot what you mentioned (wanting integer bins, adapting bin width for plotting edges at choice) can be resumed to the following Statement:
When choosing the bins (number and width) for your purpose, it is suitable if all your bins taken together encompass more values than your range, as long as the range of the data is within the bins.
What caught my interest in your comment was the following statement:
Do you have an example for this? That would be awesome
Investigating a datasets with different bins (width and number of bins) to understand it better is known to me. I just have no example for the what you mentioned.
There’s a fairly simple but good example on the Wikipedia page about historgrams-
More details in main discussion https://en.wikipedia.org/wiki/Histogram#Examples. There are also examples of e.g. normalising and using something other than frequency on the y axis which might also be useful when changing bin sizes.