Grouping values in an array
<p>I have a numpy array of size a*2. (Typical size of a is 100). In the first column are values between x_smallest and x_largest. In the second column are the corresponding y values. Now almost all x values are unique, so I want to group them. Like the first group goes from values x_smallest to x_1. The second group from x_1 to x_2. (x_smallest &lt; x_1 &lt; ... x_largest). This should be adjustable, so that I can find a useful size. I should mention that the x values are Not integers, but the y values are integers. (The y values are between 1 and N) Now I would like to know for each group the proportion of "n>1" y values against the "1" y values. Here is a small fraction of an example array:</p>

<pre><code>2.750000000000000000e+00,2.000000000000000000e+00
3.100000000000000089e+00,5.000000000000000000e+00
2.649999999999999911e+00,2.000000000000000000e+00
2.500000000000000000e+00,2.000000000000000000e+00
3.100000000000000089e+00,2.000000000000000000e+00
2.799999999999999822e+00,5.000000000000000000e+00
3.450000000000000178e+00,4.000000000000000000e+00
3.200000000000000178e+00,5.000000000000000000e+00
3.200000000000000178e+00,3.000000000000000000e+00
2.399999999999999911e+00,1.000000000000000000e+00 
</code></pre>

<p>The output array could look like this:</p>

<pre><code>1.5, 0
2.5, 0.2
3.5, 0.5
</code></pre>

<p>(Here the x_values are the midpoint of the area of x_i and x_i+1. )The output example here clearly doesn't fit to the example array. Do you have any ideas how this can be done easily. I could only think about making many specific if else commands which wouldn't be very helpful for general cases.</p>

<p>Okay, I think I solved it by myself. Here is the solution in case someone has a similiar problem and find this question:</p>

<pre><code>numgroup = 5  # Number of Groups
dmimax = numpy.amax(dmivsstasta[:, 0])  # Gets x_largest
dmimin = numpy.amin(dmivsstasta[:, 0])  # Gets x_smallest
stamax = numpy.amax(dmivsstasta[:, 1])  # Gets y_largest
stepsize = (dmimax-dmimin)/5.0  # Determines size of a group

grouparray = numpy.zeros((5, stamax+1)) # Creates array in which everything is saved


for x in range(numgroup):
    grouparray[x, 0] = dmimin+stepsize/2.0+x*stepsize  # Saves midpositon of 
                                                         each group at first column
print(grouparray)  # Just to check values
print(dmimin)
print(dmimax)
print(stepsize)

for x1 in range(numgroup):  # Iterates over all values
    for x2 in range(rd):
        if (grouparray[x1, 0]-stepsize/2.0) &lt;= dmivsstasta[x2, 0] 
        &lt; (grouparray[x1, 0]+stepsize/2.0):
            grouparray[x1, dmivsstasta[x2, 1]] += 1

print(grouparray)
</code></pre>

<p>The only missing part is to calculate the proportion, which is now easy to do with the grouparray </p>

python arrays numpy