# How to write python expression and code block for Geometric Mean

669
10
10-09-2018 12:48 AM
New Contributor

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"

1 Solution

Accepted Solutions
MVP Esteemed Contributor

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

10 Replies
MVP Esteemed Contributor

an old one

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)# ---- resultsaarray([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 fishnet100gm  # ---- geometric mean1000000.0‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``
New Contributor

Thank you. And when I said 'expression and code block' I was thinking about using 'calculate value' in model builder.

MVP Esteemed Contributor

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

MVP Esteemed Contributor

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 tablegm(fc)999999.9999999978‍‍‍‍‍‍‍‍‍‍‍‍‍``
Esri Esteemed Contributor

Or just use the standard tool Mean Center—Help | ArcGIS Desktop

MVP Esteemed Contributor

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

Esri Esteemed Contributor

A very good question Dan_Patterson , we'll see what the OP will respond, but your suggestions so far have been very helpful.

New Contributor

I meant geometric mean as in statistical value. Thanks!

MVP Esteemed Contributor

I will mark as 'Assumed Answered' since you didn't close the thread