How to write python expression and code block for Geometric Mean

669
10
Jump to solution
10-09-2018 12:48 AM
ParthBansal
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"

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
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

Calculate Value—Tools | ArcGIS Desktop 

View solution in original post

10 Replies
DanPatterson_Retired
MVP Esteemed Contributor

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.])

# ---- number of polygons from a fishnet
100

gm  # ---- geometric mean
1000000.0
ParthBansal
New Contributor

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

0 Kudos
DanPatterson_Retired
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

Calculate Value—Tools | ArcGIS Desktop 

View solution in original post

DanPatterson_Retired
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 table

gm(fc)
999999.9999999978
XanderBakker
Esri Esteemed Contributor

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

DanPatterson_Retired
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

XanderBakker
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.

ParthBansal
New Contributor

I meant geometric mean as in statistical value. Thanks!

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

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

0 Kudos