0

My goal is

  1. to identify tracts that are above the 99 percentile of a variable (xvar),
  2. to dissolve contiguous tracts by summing a variable (yvar), and
  3. to get the coordinates of the centroids (longitude and latitude) of the merged polygons.

At the very first stage for picking the above-99th-percentile tracts, I have got some errors. The following is my current work:

import arcpy
import numpy as np

input = r'D:\7362.shp'
myarray = arcpy.da.FeatureClassToNumPyArray(input, ('xvar'))

print myarray
# Printed output:
# [(5.204770523,) (8.6471839055,) (2.1095837756,) ..., (1.4788840302,
# (6.24183499,) (3.6710648163,)]

p99 = np.percentile(myarray, 99)

# use cursor to create the indicator field
# Do I have to create the abovep99 field in the first place???
with arcpy.da.UpdateCursor(input, ['xvar','abovep99']) as cursor:
    for row in cursor:
        if row[0] > p99:
            row[1] = 1
        else:
            row[1] = 0
        cursor.updateRow(row)

The error messages in the percentile calculation p99 = np.percentile(myarray, 99) are following:

Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
p99 = np.percentile(myarray, 99)
File "C:\Python27\ArcGIS10.3\lib\site-packages\numpy\lib\function_base.py", line 3096, in percentile
return _compute_qth_percentile(sorted, q, axis, out)
File "C:\Python27\ArcGIS10.3\lib\site-packages\numpy\lib\function_base.py", line 3132, in _compute_qth_percentile
return add.reduce(sorted[indexer]*weights, axis=axis, out=out)/sumval
TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and 'numpy.ndarray'

How do I fix this problem for the percentile calculation?

1

The numpy array that you are using is composed of uni-dimensional tuple items. Unlike my original answer, introducing axis argument will yield the same error. As suggested by OP, numpy array should be planarised before aplying percentile function, such as myarray_1 = np.array([arr[0] for arr in myarray]).

For dissolving the features based on a variable, please have a look at ArcGIS Dissolve tool. You need to specify yvar as dissolve field and also statistics field with SUM operator.

Extracting the centroids is pretty straightforward and you can find various workarounds for this, such as this and that.

  • @faith_dur: Thanks for the links. I converted the tuple items to the list format using the following code: thelist = [myarray[j][i] for i in range(len(myarray[0])) for j in range(len(myarray))] – Bill TP Aug 21 '15 at 2:42
  • I was just curious if you have tried adding axis argument to np.percentile as in the answer and if so what happened? Since you are using only one attribute field, I do not think you need to reconfigure your array. – fatih_dur Aug 21 '15 at 3:05
  • Adding the axis argument did not solve the problem. – Bill TP Aug 21 '15 at 3:54
  • Sorry, in fact I applied my suggestion to a planarised sample numpy array (as you did) and answer it assuming I am using tuples-array. I will edit my answer for others who may have the same problem. – fatih_dur Aug 21 '15 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.