I am building a model to calculate geometric mean for each shape file in model builder. How to do write Expression and Code Block which would:
Take in shapefile "City_1" and use all the values in Field "Area" and calculate n√a*b*c*d. ..... where n are number of values in field "Area" and a,b,c,d are all the value in field "Area"
Solved! Go to Solution.
the first example is a code block (def gm(a) ), just substitute your field name in the expression call …. gm(!YourFieldName!) … using a python parser.
The last one can be turned into a code block
import scipy.stats as st
def gm(a):
"""scipy version"""
areas = st.gmean(areas)
return areas
gm(!YourFieldName!)
This is the link to Calculate values to show you where to put stuff
an old one
https://community.esri.com/message/434687?commentID=434687#comment-434687
and it isn't done in the field calculator
fc = r"C:\GIS\A_Tools_scripts\aprx_info\aprx_info.gdb\SamplingGrids"
a = arcpy.da.TableToNumPyArray(fc, "Shape_Area")
a = a.view('float')
N = len(a)
loga = np.log10(a)
_sum = np.cumsum(loga)
gm = 10.0**((_sum[-1])/N)
# ---- results
a
array([1000000., 1000000., 1000000., 1000000., 1000000., 1000000., 1000000.,
1000000., 1000000., 1000000., 1000000., 1000000., 1000000., 1000000.,
1000000., 1000000., 1000000., 1000000., 1000000., 1000000., 1000000.,
1000000., 1000000., 1000000., 1000000., 1000000., 1000000., 1000000.,
1000000., 1000000., 1000000., 1000000., 1000000., 1000000., 1000000.,
1000000., 1000000., 1000000., 1000000., 1000000., 1000000., 1000000.,
1000000., 1000000., 1000000., 1000000., 1000000., 1000000., 1000000.,
1000000., 1000000., 1000000., 1000000., 1000000., 1000000., 1000000.,
1000000., 1000000., 1000000., 1000000., 1000000., 1000000., 1000000.,
1000000., 1000000., 1000000., 1000000., 1000000., 1000000., 1000000.,
1000000., 1000000., 1000000., 1000000., 1000000., 1000000., 1000000.,
1000000., 1000000., 1000000., 1000000., 1000000., 1000000., 1000000.,
1000000., 1000000., 1000000., 1000000., 1000000., 1000000., 1000000.,
1000000., 1000000., 1000000., 1000000., 1000000., 1000000., 1000000.,
1000000., 1000000.])
N # ---- number of polygons from a fishnet
100
gm # ---- geometric mean
1000000.0
Thank you. And when I said 'expression and code block' I was thinking about using 'calculate value' in model builder.
the first example is a code block (def gm(a) ), just substitute your field name in the expression call …. gm(!YourFieldName!) … using a python parser.
The last one can be turned into a code block
import scipy.stats as st
def gm(a):
"""scipy version"""
areas = st.gmean(areas)
return areas
gm(!YourFieldName!)
This is the link to Calculate values to show you where to put stuff
But as a code piece
fc = 'C:\\GIS\\A_Tools_scripts\\aprx_info\\aprx_info.gdb\\SamplingGrids'
def gm(fc):
import scipy.stats as st
a = arcpy.da.TableToNumPyArray(fc, "Shape_Area")
a = a.view('float')
return st.gmean(a)
# --- now feed it the featureclass or fc table
gm(fc)
999999.9999999978
Or just use the standard tool Mean Center—Help | ArcGIS Desktop
Xander Bakker do you suspect that the OP meant "geographic center" and not the geometric mean as in the mathematical definition? They are two different measures
A very good question Dan_Patterson , we'll see what the OP will respond, but your suggestions so far have been very helpful.
I meant geometric mean as in statistical value. Thanks!
I will mark as 'Assumed Answered' since you didn't close the thread