Skip to content Skip to sidebar Skip to footer

Bin One Column And Sum The Other Of (2,n) Array

Question: I have a dataset like the following: import numpy as np x = np.arange(0,10000,0.5) y = np.arange(x.size)/x.size Plotting in log-log space, it looks like this: import

Solution 1:

I will answer two of your several questions: How to create bins alternatively and generate an arbitrary n points per order of magnitude instead of 10?

You can make use of np.logspace and np.outer to create your bins for any arbitrary n value as following. The default base in logspace is 10. It generates logarithmically spaced points similar to linspace which generates linearly spaced mesh.

For n=10

n = 10bins = np.unique(np.outer(np.logspace(0, 3, 4), np.arange(0, n+1)))
# array([0.e+00, 1.e+00, 2.e+00, 3.e+00, 4.e+00, 5.e+00, 6.e+00, 7.e+00,#        8.e+00, 9.e+00, 1.e+01, 2.e+01, 3.e+01, 4.e+01, 5.e+01, 6.e+01,#        7.e+01, 8.e+01, 9.e+01, 1.e+02, 2.e+02, 3.e+02, 4.e+02, 5.e+02,#        6.e+02, 7.e+02, 8.e+02, 9.e+02, 1.e+03, 2.e+03, 3.e+03, 4.e+03,#        5.e+03, 6.e+03, 7.e+03, 8.e+03, 9.e+03, 1.e+04])

For n=20

n = 20bins = np.unique(np.outer(np.logspace(0, 3, 4), np.arange(0, n+1)))
# array([0.0e+00, 1.0e+00, 2.0e+00, 3.0e+00, 4.0e+00, 5.0e+00, 6.0e+00, 7.0e+00, 8.0e+00, 9.0e+00, 1.0e+01, 1.1e+01, 1.2e+01, 1.3e+01, 1.4e+01, 1.5e+01, 1.6e+01, 1.7e+01, 1.8e+01, 1.9e+01, 2.0e+01, 3.0e+01, 4.0e+01, 5.0e+01, 6.0e+01, 7.0e+01, 8.0e+01, 9.0e+01, 1.0e+02, 1.1e+02, 1.2e+02, 1.3e+02, 1.4e+02, 1.5e+02, 1.6e+02, 1.7e+02, 1.8e+02, 1.9e+02, 2.0e+02, 3.0e+02, 4.0e+02, 5.0e+02, 6.0e+02, 7.0e+02, 8.0e+02, 9.0e+02, 1.0e+03, 1.1e+03, 1.2e+03, 1.3e+03, 1.4e+03, 1.5e+03, 1.6e+03, 1.7e+03, 1.8e+03, 1.9e+03, 2.0e+03, 3.0e+03, 4.0e+03, 5.0e+03, 6.0e+03, 7.0e+03, 8.0e+03, 9.0e+03, 1.0e+04, 1.1e+04, 1.2e+04, 1.3e+04, 1.4e+04, 1.5e+04, 1.6e+04, 1.7e+04, 1.8e+04, 1.9e+04, 2.0e+04])

EDIT

If you want 0, 10, 20, 30...90, 100, 200, 300... you can do the following

n = 10bins = np.unique(np.outer(np.logspace(1, 3, 3), np.arange(0, n+1)))
# array([    0.,    10.,    20.,    30.,    40.,    50.,    60.,    70.,#           80.,    90.,   100.,   200.,   300.,   400.,   500.,   600.,#          700.,   800.,   900.,  1000.,  2000.,  3000.,  4000.,  5000.,#         6000.,  7000.,  8000.,  9000., 10000.])

Post a Comment for "Bin One Column And Sum The Other Of (2,n) Array"