Python 创建通用函数
我有一个函数定义- 现在我必须创建一个这样的函数-Python 创建通用函数,python,numpy,Python,Numpy,我有一个函数定义- 现在我必须创建一个这样的函数- feature file_frequency_M file_frequency_B abc 2 5 问题在于,由于(t,c)(其中t是特征,c是类)有4种组合,因此可能出现(t,c),(t',c),(t,c'),(t',c')。因此,根据t,c的值,函数的定义也会改变。 除了计算a、b、c、d四次,然后对函数值求和外,还有其他方法吗 数据集如下所示- feature file_
feature file_frequency_M file_frequency_B
abc 2 5
问题在于,由于(t,c)(其中t是特征,c是类)有4种组合,因此可能出现(t,c),(t',c),(t,c'),(t',c')。因此,根据t,c的值,函数的定义也会改变。
除了计算a、b、c、d四次,然后对函数值求和外,还有其他方法吗
数据集如下所示-
feature file_frequency_M file_frequency_B
abc 2 5
我的尝试-
dataset = pd.read_csv('.csv')
score = []
###list =[(t,c) ,(t,c0),(t0,c),(t0,c0)] ##representation of the combination of (t,c)
l=152+1394
for index, row in dataset.iterrows():
a = row['file_frequency_M']
b = row['file_frequency_B']
c = 152 - a
d = 1394 - b
temp_score = 0
tmp1 = 0
tmp2 = 0
tmp3 = 0
tmp4 = 0
for i in range(4):
if i == 0:
if a == 0:
tmp1 = 0
else:
tmp1 = log10(((a * l) / (a + c) * (a + b)))
temp_score += tmp1
if i == 1:
if b == 0:
tmp2 = 0
else:
tmp2 = log10(((b * l) / (b + d) * (b + a)))
temp_score += tmp2
if i == 2:
if c == 0:
tmp3 = 0
else:
tmp3 = log10(((c * l) / (c + a) * (c + d)))
temp_score += tmp3
if i == 3:
if d == 0:
tmp4 = 0
else:
tmp4 = log10(((d * l) / (d + b) * (d + c)))
temp_score += tmp4
score.append(temp_score)
np.savetxt("m.csv", score, delimiter=",")
通过创建
I(t,c)
的函数表示,可以节省大量代码重复:
注意:您的代码似乎有错误,根据函数定义的图像,应该是:
log10((a * n) / ((a + c) * (a + b)))
不是
(注意括号的位置)。您当前的解决方案是什么?如果您向我展示您当前的解决方案,我可能会对其进行改进,但我不会在没有正确描述的情况下尝试从头开始解决您的问题。@Scotty1这是一个非常基本的解决方案。但无论如何,我会发布它
log10(((a * l) / (a + c) * (a + b)))