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

标签 python arrays numpy arcpy

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

我的问题如下......

示例:

在不由列数和行数定义的表格的基础上,您需要计算如图所示的表达式...

    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 矩阵。

关于python - 如何计算矩阵中每个单元格占列总和的比例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28993806/

相关文章:

java - 搜索二维数组

java - C++ 和 Java 中的数组

java - 如何在 GUI JPanel 中打印对象数组?

python - 关闭 Pygame alpha

python - 我需要导航网页及其子页面链接中的每个链接

python - numpy 数组的 bool /非零索引循环的替代方案

python - 如何将一系列索引/类别转换为分类数组

python - 当 N 不是 2 的幂时,Numpy(Python)中的 FFT

python - Matplotlib 在绘图中写入 '±'

python - 通过 serial1 将 Arduno Yun pin 信息发送到 linino python 脚本,通过 UDP 发送到 Max