gpt4 book ai didi

python - 布里渊多样性指数

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:13 27 4
gpt4 key购买 nike

我正在尝试使用 pandas 和 numpy 根据“a”列在 Python 中按组计算布里渊多样性指数。但是出了点问题。

import pandas as pd
import numpy as np
def Brillouin_Index(x):
for i in range(len(x)):
x["Brillouin_Index"] = (np.log10(np.math.factorial(np.sum(x))) - np.sum(np.log10(np.math.factorial(x[i])))) / np.sum(x)
return x
a = list("ABCDEADECS")
b = [12,23,12,12,32,34,21,2,10,5]
c = {"a":a,"b":b}
data = pd.DataFrame(c)
data
data.groupby("a").apply(Brillouin_Index)

我执行了上面的代码,有两个错误。

TypeError: cannot convert the series to <class 'int'>
AttributeError: 'int' object has no attribute 'log10'

具体公式见以下链接Brillouin’s Diversity Index

我用其他软件计算了每组的值

  1. H_A = 0.2965
  2. H_B = 0
  3. H_C = 0.264
  4. H_D = 0.259
  5. H_E = 0.08085
  6. H_S = 0

非常感谢!

最佳答案

我用R分组计算布里渊多样性指数,代码如下:

Brillouin_Diversity_Index <- function(x)
{ N <- sum(x)

(log10(factorial(N)) - sum(log10(factorial(x)))) / N

}

dt <- data.table(x = c("A","B","C","D","E","A","D","E","C","S"),
y = c(12,23,12,12,32,34,21,2,10,5))
dt[,Brillouin_Diversity_Index(y),by = .(x)]
  1. x V1
  2. A 0.23021887
  3. B 0.00000000
  4. C 0.26412121
  5. D 0.25909105
  6. E 0.08085185
  7. 0.00000000

关于python - 布里渊多样性指数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50828138/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com