布里渊的多样性指数

时间:2018-06-13 02:03:48

标签: python indexing

我正在尝试使用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
  7. 非常感谢!

1 个答案:

答案 0 :(得分:1)

我用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. S 0.00000000