0

Can someone help me sort a group of floats into bins no larger than a value of 10? I have two problems in that the program is reading a set of files and creating individual arrays for each value. Second I'm unsure as to what is going on with the numpy binning.

for filename in glob.iglob('*.html'):
    with open(filename) as f:
        soup = BeautifulSoup(f)
        results = []

        weight = soup.find('b', text='Shipping Weight:').next_sibling
        title = soup.find("span", id="btAsinTitle")

        results = weight
        import re
        import scipy 
        import numpy as np

        result_str = re.findall(r"[-+]?\d*\.\d+|\d+", results)
        result = float(result_str[0])
        #print result

        array_weight = [result]

        print array_weight

        x = np.array(array_weight)

        bins = np.array([10.0])

        inds = np.digitize(x, bins)

        print inds
2
  • Can you simplify this code a little? We don't need to see all the beautiful soup or re stuff to help you with sorting/binning floats. Start with your array of floats and go from there, otherwise I am a bit lost. Commented Apr 27, 2014 at 3:12
  • Small note, bring your import statements outside your loops! Otherwise you incur an overhead at each iteration of that for loop. Commented Apr 27, 2014 at 3:44

1 Answer 1

1

Answering the titled question and ignore the others (please read the FAQ!), a minimal working example for numpy.digitize is

import numpy as np

# Five bins, spaced from 0, 10
bins = np.linspace(0,10,5)

# Some random test data, from 0,10
data = np.random.random(size=20)*10
print data

# Binned data
print np.digitize(data,bins)

Which returns

[ 9.29893458  0.88322852  4.9592157   7.33677397  0.20901007  5.77875637
  2.49152666  3.55982666  5.33997896  3.76318862  0.35513614  7.12985682
  2.57747437  4.62240375  8.02503782  5.43143368  6.29290487  2.79342587
  3.11806151  5.79996645]
[4 1 2 3 1 3 1 2 3 2 1 3 2 2 4 3 3 2 2 3]

It is useful to read over the docs, they tell you the proper input and output!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.