I want to calculate scores in one field based on the percentile (in steps of 10) the row falls into in another field. I found a solution here (Calculating Percentiles in ArcMap?) that I tried out, but I now get the error:
unsupported operand type(s) for *: 'numpy.ndarray' and 'numpy.ndarray'
My code looks as follows:
input = "Buffer"
arr = arcpy.da.FeatureClassToNumPyArray(input, ('Shape_Area'))
p1 = np.percentile(arr, 10)
p2 = np.percentile(arr, 20)
p3 = np.percentile(arr, 30)
p4 = np.percentile(arr, 40)
p5 = np.percentile(arr, 50)
p6 = np.percentile(arr, 60)
p7 = np.percentile(arr, 70)
p8 = np.percentile(arr, 80)
p9 = np.percentile(arr, 90)
p10 = np.percentile(arr, 100)
#use cursor to update the new rank field
with arcpy.da.UpdateCursor(input , ['Shape_Area','ConS']) as cursor:
for row in cursor:
if row[0] < p1:
row[1] = 0.1 #rank 0
elif p1 <= row[0] and row[0] < p2:
row[1] = 0.2
elif p2 <= row[0] and row[0] < p3:
row[1] = 0.3
elif p3 <= row[0] and row[0] < p4:
row[1] = 0.4
elif p4 <= row[0] and row[0] < p5:
row[1] = 0.5
elif p5 <= row[0] and row[0] < p6:
row[1] = 0.6
elif p6 <= row[0] and row[0] < p7:
row[1] = 0.7
elif p7 <= row[0] and row[0] < p8:
row[1] = 0.8
elif p9 <= row[0] and row[0] < p10:
row[1] = 0.9
else:
row[1] = 1
cursor.updateRow(row)
When searching on the internet I found that the problem behind this error often lies in the values being strings rather than floats or integers so I edited my code to read:
p1 = np.percentile(arr, 10, np.float)
p2 = np.percentile(arr, 20, np.float)
p3 = np.percentile(arr, 30, np.float)
And now I got the error message:
'type' object is not iterable
What can I do to solve this issue, or is there maybe another way to calculate the scores?
numpy.percentile
here. You are providing a type (np.float
) as a parameter where the parameter needs to be the axis along which the percentiles are computed.arr
?[(39710.930139345284,) (32032.62901900672,) (13822.309180032586,) ..., (33956.1676006977,) (35127.24443114165,) (23696.81216139415,)]
numpy.percentile
, for examplep1
, is a single value and that all of the records in your feature class have a value for'Shape_Area'
?