我正在尝试使用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
我使用其他软件来计算每组的值
非常感谢!
答案 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)]