如何计算矩阵中每个单元格占列总和的比例?

How to calculate the proportion of each cell in the matrix to the sum of the column?

我是使用 python 的绝对初学者。

我的问题如下....

示例:

在没有定义列数和行数的table的基础上,需要计算表达式如图...

    A  B  C  D  E .. n
 a  1  3  5  3  5 .. n
 b  4  3  4  2  6 .. n
 .  .  .  .  .  . .. n
 .
 m  .  .  .  .  . .. n
    X1 X2 X3 X4 X5.. Xn

X1 = (Aa / SUM A)^2 + (Ab / SUM A)^2+ .. +(Am / SUM A)^2
X2 = (Ba / SUM B)^2 + (Bb / SUM B)^2+ .. +(Bm / SUM B)^2
.
.
Xn ....

有没有人知道如何创建解决图中所示问题的表达式?

我应该是给已有的代码插入定义了一个表达式....

  values = [] # store the sum values here.
  fields = arcpy.ListFields(fc, "*")

  # get the OID/FID field name to skip
  desc = arcpy.Describe(fc)
  if desc.hasOID:
      OIDname = desc.OIDFieldName.upper()
  else:
      OIDname = ""

  for field in fields:
      if field.name.upper() != OIDname: # skip the OID/FID field.
         if field.type in ("Double", "Integer", "Single"):
              # sum each suitable field, but not the NULL ones - they would be bad
              with arcpy.da.SearchCursor(fc,field.name,field.name + " is not NULL") as sCur:
                  thisValue = 0
                  for row in sCur:
                      thisValue = ???????? # expression -- (A1 / SUM A)^2+...
                  values.append(thisValue) # this will be the inserted row
              fieldNameList.append(field.name) 

  with arcpy.da.InsertCursor(fc, fieldNameList) as cur:
      cur.insertRow(values)

在代码中我使用了 arcpy 和 numpy ...

提前致谢

使用 NumPy 的广播规则,您可以避免循环执行如下操作:

s = a.sum(axis=0)
x = ((a/s)**2).sum(axis=0)

其中 a 是您的 m X n 矩阵。

问题已解决....

感谢@Saullo Castro

with arcpy.da.SearchCursor(fc,(fieldNameList[:-1])) as sCur: 

    for row in sCur:

            iteracija_kolona = [row[i] for i in range(len(fieldNameList[:-1]))]
            values.append(iteracija_kolona) 

    matrica = np.array(values) 
    s = matrica.sum(axis=0) 
    x = ((matrica/s)**2).sum(axis=0).tolist() 
    del row
    del sCur


    with arcpy.da.InsertCursor(fc, fieldNameList[:-1]) as cur: 
        cur.insertRow(x)