Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 569233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:16:24+00:00 2026-05-13T13:16:24+00:00

What is a good way to bin numerical values into a certain range? For

  • 0

What is a good way to bin numerical values into a certain range? For example, suppose I have a list of values and I want to bin them into N bins by their range. Right now, I do something like this:

from scipy import *
num_bins = 3 # number of bins to use
values = # some array of integers...
min_val = min(values) - 1
max_val = max(values) + 1
my_bins = linspace(min_val, max_val, num_bins)
# assign point to my bins
for v in values:
  best_bin = min_index(abs(my_bins - v))

where min_index returns the index of the minimum value. The idea is that you can find the bin the point falls into by seeing what bin it has the smallest difference with.

But I think this has weird edge cases. What I am looking for is a good representation of bins, ideally ones that are half closed half open (so that there is no way of assigning one point to two bins), i.e.

bin1 = [x1, x2)
bin2 = [x2, x3)
bin3 = [x3, x4)
etc...

what is a good way to do this in Python, using numpy/scipy? I am only concerned here with binning integer values.

thanks very much for your help.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:16:24+00:00Added an answer on May 13, 2026 at 1:16 pm

    numpy.histogram() does exactly what you want.

    The function signature is:

    numpy.histogram(a, bins=10, range=None, normed=False, weights=None, new=None)
    

    We’re mostly interested in a and bins. a is the input data that needs to be binned. bins can be a number of bins (your num_bins), or it can be a sequence of scalars, which denote bin edges (half open).

    import numpy
    values = numpy.arange(10, dtype=int)
    bins = numpy.arange(-1, 11)
    freq, bins = numpy.histogram(values, bins)
    # freq is now [0 1 1 1 1 1 1 1 1 1 1]
    # bins is unchanged
    

    To quote the documentation:

    All but the last (righthand-most) bin is half-open. In other words, if bins is:

    [1, 2, 3, 4]
    

    then the first bin is [1, 2) (including 1, but excluding 2) and the second [2, 3). The last bin, however, is [3, 4], which includes 4.

    Edit: You want to know the index in your bins of each element. For this, you can use numpy.digitize(). If your bins are going to be integral, you can use numpy.bincount() as well.

    >>> values = numpy.random.randint(0, 20, 10)
    >>> values
    array([17, 14,  9,  7,  6,  9, 19,  4,  2, 19])
    >>> bins = numpy.linspace(-1, 21, 23)
    >>> bins
    array([ -1.,   0.,   1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,
            10.,  11.,  12.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,  20.,
            21.])
    >>> pos = numpy.digitize(values, bins)
    >>> pos
    array([19, 16, 11,  9,  8, 11, 21,  6,  4, 21])
    

    Since the interval is open on the upper limit, the indices are correct:

    >>> (bins[pos-1] == values).all()
    True
    >>> import sys
    >>> for n in range(len(values)):
    ...     sys.stdout.write("%g <= %g < %g\n"
    ...             %(bins[pos[n]-1], values[n], bins[pos[n]]))
    17 <= 17 < 18
    14 <= 14 < 15
    9 <= 9 < 10
    7 <= 7 < 8
    6 <= 6 < 7
    9 <= 9 < 10
    19 <= 19 < 20
    4 <= 4 < 5
    2 <= 2 < 3
    19 <= 19 < 20
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 395k
  • Answers 395k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Apparently you can just assign the permissions in the FBML… May 15, 2026 at 2:44 am
  • Editorial Team
    Editorial Team added an answer Have you tried using ActiveRecord's delete_all method? Here's an excerpt… May 15, 2026 at 2:44 am
  • Editorial Team
    Editorial Team added an answer Settings | Editor | General > Other | Strip trailing… May 15, 2026 at 2:44 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.