机器学习各种评价指标

1.adjusted Rand index (ARI):调整兰德系数[-1,1]

兰德指数用来衡量两个分布的吻合程度,取值范围[-1,1],数值越接近于1越好。

from sklearn.metrics import adjusted_rand_score  # 导入函数
labels_true = [0, 0, 1, 1, 0, 1]  # 真正的集合
labels_pred = [0, 0, 1, 1, 1, 2]  # 预测的集合
ari = adjusted_rand_score(labels_true, labels_pred)  # 采用adjusted_rand_score函数
print('兰德系数为:%f'%(ari))
#兰德系数为:0.117647

 2.adjusted mutual information (AMI):互信息

互信息也是用来衡量两个分布的吻合程度,取值范围[-1,1],值越大聚类效果与真实情况越吻合。

from sklearn.metrics import adjusted_mutual_info_score  # 导入函数
labels_true = [0, 0, 1, 1, 0, 1]  # 真正的集合
labels_pred = [0, 0, 1, 1, 1, 2]  # 预测的集合
ami=adjusted_mutual_info_score(labels_true, labels_pred)   # 采用adjusted_mutual_info_score函数
print('互信息为:%f'%(ami))
#互信息为:0.225042

3.Shannon entropy: 香农熵信息熵

import math
# 函数说明:创建数据集
def createDataSet():
    dataSet = [[u'青年', u'否', u'否', u'一般', u'拒绝'],
               [u'青年', u'否', u'否', u'好', u'同意'],
               [u'青年', u'是', u'否', u'好', u'拒绝'],
               [u'青年', u'是', u'是', u'好', u'同意'],
               [u'青年', u'是', u'是', u'一般', u'同意'],
               [u'青年', u'是', u'否', u'一般', u'同意'],
               [u'中年', u'否', u'否', u'一般', u'同意'],
               [u'中年', u'否', u'否', u'好', u'拒绝'],
               [u'中年', u'是', u'否', u'好', u'拒绝'],
               [u'中年', u'是', u'是', u'好', u'拒绝'],
               [u'中年', u'是', u'是', u'一般', u'拒绝'],
               [u'中年', u'是', u'否', u'一般', u'拒绝'],
               [u'老年', u'否', u'否', u'一般', u'同意'],
               [u'老年', u'否', u'否', u'好', u'拒绝'],
               [u'老年', u'是', u'否', u'好', u'拒绝'],
               [u'老年', u'是', u'是', u'好', u'拒绝'],
               [u'老年', u'是', u'是', u'一般', u'同意'],
               [u'老年', u'是', u'否', u'一般', u'拒绝'],
               ]
    labelSet = [u'年龄', u'有工作', u'有房子', u'信贷情况']
    # 返回数据集和标签
    return dataSet, labelSet
# 函数说明:计算给定数据集的经验熵(香农熵)
def cacShannonEnt(dataSet):
    """
    计算训练数据集Y随机变量的香农熵
    :param dataSet:
    :return:
    """
    numEntries = len(dataSet) # 返回数据集的行数
    labelCounts = {} # 保存每个标签(Label)出现次数的字典
    for featvec in dataSet:  # 对每组特征向量进行统计
        currentLbel = featvec[-1]  # 提取标签(Label)信息
        if currentLbel not in labelCounts.keys(): # 如果标签(Label)没有放入统计次数的字典,添加进去
            labelCounts[currentLbel] = 0
        labelCounts[currentLbel] += 1 # Label计数
    shannonEnt = 0.0  # 经验熵(香农熵)
    for key in labelCounts:    # 计算香农熵
        prob = float(labelCounts[key])/numEntries # 选择该标签(Label)的概率
        shannonEnt -= prob * math.log(prob, 2) # log base 2  # 利用公式计算
    return shannonEnt # 返回经验熵(香农熵)

if __name__ == '__main__':
    dataSet, labelSet = createDataSet()
    print(cacShannonEnt(dataSet))

4.Fowlkes-Mallows Index(FMI):FMI是对聚类结果和真实值计算得到的召回率和精确率,进行几何平均的结果,取值范围为 [0,1],越接近1越好。

from sklearn.metrics import fowlkes_mallows_score   # 导入函数
labels_true = [0, 0, 0, 1, 1, 1]   # 真正的集合
labels_pred = [0, 0, 1, 1, 2, 2]   # 预测的集合
fmi=fowlkes_mallows_score(labels_true, labels_pred)   # 采用fowlkes_mallows_score函数
print('FMI为:%f'%(fmi)) 
#FMI为:0.471405

4.normalized mutual information (NMI):

"""
利用Python实现NMI计算
"""
import math
import numpy as np
from sklearn import metrics
def NMI(A, B):
    # 样本点数
    total = len(A) # 计算A中元素个数
    A_ids = set(A) # 创建一个无序不重复元素集合Aid
    B_ids = set(B) # 创建一个无序不重复元素集合Bid
    # 互信息计算
    MI = 0
    eps = 1.4e-45
    for idA in A_ids:  # 在Aid集合遍历元素
        for idB in B_ids:  # 在Bid集合遍历元素
            idAOccur = np.where(A == idA)    # 输出A集合中等于ida元素值下标的数组
            idBOccur = np.where(B == idB)      # 输出B集合中等于idb元素的下标的数组
            idABOccur = np.intersect1d(idAOccur, idBOccur)   # 求两个数组的交集的数组
            px = 1.0*len(idAOccur[0])/total  # 计算p(x)的值 (A数组中aid元素的个数)/(A数组总长度)
            py = 1.0*len(idBOccur[0])/total  # 计算p(y)的值
            pxy = 1.0*len(idABOccur)/total   # 计算p(x,y)联合概率(AB数组中aid的位置和bid的位置相同的个数)/(A数组总长度)
            MI = MI + pxy*math.log(pxy/(px*py)+eps, 2)  # 计算MI值
    # 标准化互信息
    Hx = 0
    for idA in A_ids:  # 在Aid集合遍历元素
        idAOccurCount = 1.0*len(np.where(A==idA)[0])  # A=ida的元素个数
        Hx = Hx - (idAOccurCount/total)*math.log(idAOccurCount/total+eps, 2)  # 计算H(x)值
        Hy = 0
    for idB in B_ids:
        idBOccurCount = 1.0*len(np.where(B==idB)[0])  # B=idb的元素个数
        Hy = Hy - (idBOccurCount/total)*math.log(idBOccurCount/total+eps, 2)  # 计算H(y)值
    MIhat = 2.0*MI/(Hx+Hy)
    return MIhat


if __name__ == '__main__':
    A = np.array([1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3])
    B = np.array([1,2,1,1,1,1,1,2,2,2,2,3,1,1,3,3,3])
    print(NMI(A, B))
    print(metrics.normalized_mutual_info_score(A, B))     # 直接调用 sklearn 中的函数

5.Jaccard index:Jaccard 相似系数又称为Jaccard相似性度量(Jaccard系数,Jaccard 指数,Jaccard index)。用于比较有限样本集之间的相似性与差异性。Jaccard系数值越大,样本相似度越高。

from sklearn.metrics import jaccard_score
y_test = [1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3]
yhat = [1,2,1,1,1,1,1,2,2,2,2,3,1,1,3,3,3]
jac = jaccard_score(y_test, yhat, average='macro')
print("jac:%f" %jac)

6.receiver operating characteristic (ROC)

from sklearn import metrics
import matplotlib.pyplot as plt

y_labels = [1,1,0,1,1,0,0,0]
y_scores=[0.8,0.9,0.6,0.3,0.2,0.1,0.1,0.6]
y_preds = [1,1,1,0,1,0,0,1] # threshold=0.5
report = metrics.classification_report(y_labels, y_preds)
fpr, tpr, thresholds = metrics.roc_curve(y_labels, y_scores)
auc = metrics.auc(fpr, tpr)
plt.plot(fpr, tpr, '*-')
plt.ylabel('TPR')
plt.xlabel('FPR')
plt.title('ROC curve')
plt.show()
print(report)

7.recall/accuracy:召回率/准确率

true positive rates (TPRs, sensitivities)

false positive rates (FPRs)

F1 score

from sklearn import metrics # sklearn==0.24.2
import numpy as np

test_Y = np.array([0,0,1,0,0,1,0,1,1,0])  # 测试集的标签
predict_Y = np.array([1,0,1,0,0,0,0,1,0,0])  # 预测值

confusion_matrix = metrics.confusion_matrix(test_Y, predict_Y)       # 混淆矩阵
tn, fp, fn, tp = metrics.confusion_matrix(test_Y, predict_Y).ravel()  # 混淆矩阵各值
recall_score = metrics.recall_score(test_Y, predict_Y)               # 召回率
pre_score = metrics.precision_score(test_Y, predict_Y)               # 准确率
ACC = metrics.accuracy_score(test_Y, predict_Y)                      # 准确度ACC

print(confusion_matrix)
print(tn, fp, fn, tp)
print(recall_score)
print(pre_score)
print(ACC)
print(f1_score(test_Y, predict_Y, average='weighted'))
print(f1_score(test_Y, predict_Y, average='macro'))
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值