F-Score(非模型评价打分,区别与 F1_score )是一种衡量特征在两类之间分辨能力的方法,通过此方法可以实现最有效的特征选择。最初是由台湾国立大学的Yi-Wei Chen提出的(参考《Combining SVMs with Various Feature Selection Strategies》),公式如下:

其中i代表第i个特征,即每一个特征都会有一个F-score。x拔是所有该特征值的平均数,而(+),(-)则分别代表所有阳性样本和阴性样本的特征值(的平均数)。代表k是对于具体第i个特征的每个实例,分母的两个sigma可以理解为阳性样本与阴性样本的特征值的方差。F-score越大说明该特征的辨别能力越强。

这里给出一个Python脚本,可以实现F-score的计算。当然,一般我们的输入格式是一个矩阵,包含所有样本的特征以及这些样本的分类,最终希望得到每个特征的F-score。于是我写的脚本就是这样以特征矩阵以及分类列表两个参数作为输入,以F-score列表作为输出。

谢娟英等人还改进了F-score,将它推广至多分类的情况(查看文献),公式如下:

不难发现,原版的F-score是改进版的一个特例,即l=2, 分别代表正类与负类(我们通常用0和1表示)
考虑到原Fscore可能更常用一点,这里仅分享原版的脚本。而改进版的由于我本人的研究需要也应该会写,有需要的可以留言~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def fscore_core(np,nn,xb,xbp,xbn,xkp,xkn):
'''
np: number of positive features
nn: number of negative features
xb: list of the average of each feature of the whole instances
xbp: list of the average of each feature of the positive instances
xbn: list of the average of each feature of the negative instances
xkp: list of each feature which is a list of each positive instance
xkn: list of each feature which is a list of each negatgive instance
reference: http://link.springer.com/chapter/10.1007/978-3-540-35488-8_13
'''

def sigmap (i,np,xbp,xkp):
return sum([(xkp[i][k]-xbp[i])**2 for k in range(np)])

def sigman (i,nn,xbn,xkn):
print sum([(xkn[i][k]-xbn[i])**2 for k in range(nn)])
return sum([(xkn[i][k]-xbn[i])**2 for k in range(nn)])

n_feature = len(xb)
fscores = []
for i in range(n_feature):
fscore_numerator = (xbp[i]-xb[i])**2 + (xbn[i]-xb[i])**2
fscore_denominator = (1/float(np-1))*(sigmap(i,np,xbp,xkp))+ \
(1/float(nn-1))*(sigman(i,nn,xbn,xkn))
fscores.append(fscore_numerator/fscore_denominator)

return fscores

def fscore(feature,classindex):
'''
feature: a matrix whose row indicates instances, col indicates features
classindex: 1 indicates positive and 0 indicates negative
'''
n_instance = len(feature)
n_feature = len(feature[0])
np = sum(classindex)
nn = n_instance - np
xkp =[];xkn =[];xbp =[];xbn =[];xb=[]
for i in range(n_feature):
xkp_i = [];xkn_i = []
for k in range(n_instance):
if classindex[k] == 1:
xkp_i.append(feature[k][i])
else:
xkn_i.append(feature[k][i])
xkp.append(xkp_i)
xkn.append(xkn_i)
sum_xkp_i = sum(xkp_i)
sum_xkn_i = sum(xkn_i)
xbp.append(sum_xkp_i/float(np))
xbn.append(sum_xkn_i/float(nn))
xb.append((sum_xkp_i+sum_xkn_i)/float(n_instance))
return fscore_core(np,nn,xb,xbp,xbn,xkp,xkn)