Select to view content in your preferred language

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

2213
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 Emeritus

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 Emeritus

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)

# ---- 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‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````
New Contributor

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

MVP Emeritus

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 Emeritus

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‍‍‍‍‍‍‍‍‍‍‍‍‍``````
Esri Esteemed Contributor

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

MVP Emeritus

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 Emeritus

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