Selecting features using sum of attribute values

06-29-2016 12:13 AM
New Contributor

Hello, I am attempting to create a tool that allows for the clustering of polygon features into different groups based  the sum of values (Housing_Unit field) of the polygon features. (Example: Using a maximum of 600 housing units to divide the features into separate groups). The tool is designed such that if the sum of values is more than the specified value parameter (Example: 200 < 500 (parameter)), the tool will stop. However the tool only stops after the sum of values violates the specified value, thus the features grouped includes the feature that caused the entire group to violate the parameter. I would like it such that the tool does not consider the features that caused the sum of value to violate the parameter, but I am unsure of the Python expression to do so, as I am still new to coding (and python). My expression to set the constraint is

" %sum_of_housing_units% < %housing_units_parameter% " My arcmap is advanced licensed.

0 Kudos
1 Reply
MVP Esteemed Contributor

I don't know what your code looks like so I will give a demonstration.

In effect you could do a cumulative sum of the list of values you have, then get an index of that where the current sum is less than a threshold.  Once you have the indices ( a 0,1 boolean), you slice the original array to get the values

upper = 10

>>> vals = np.random.randint(5,size=10)

>>> vals

array([3, 0, 4, 4, 4, 4, 2, 0, 4, 0])

>>> s = np.cumsum(vals) <= 10

>>> s

array([1, 1, 1, 0, 0, 0, 0, 0, 0, 0], dtype=bool)

>>> vals

array([3, 0, 4])                  # first group <= 10

new_vals = vals[np.where(s==0)]

>>> new_vals

array([4, 4, 4, 2, 0, 4, 0])

So the slicing or looping you need to implement this depends on the code you are using... there is no need to use numpy since you can replicate this using pure python

0 Kudos