Python微信订餐小程序课程视频

https://edu.csdn.net/course/detail/36074

Python实战量化交易理财系统

https://edu.csdn.net/course/detail/35475
最近在做眼底图像的无监督分类,使用的数据集辣子kaggle的Diabetic Retinopathy,简称DR,中文称糖尿病型眼底疾病。

最后的评估方法是二次加权kappa。以前没接触过,网上也没有具体的介绍,在这里简单谈谈我的理解,如有错误欢迎指出。

简介

Kappa指数用来衡量两个模型对同一张图片进行判断时,判断结果一致的程度,结果范围从0~1,1表示评价完全相同,0表示评价完全相反。

一般用模型获得相同评价的数量与基于可能性的期望是否有差别来分析,当两个模型相同评价的数量和基于可能性期望的数量基本一样时,kappa的值就接近于1。

举个栗子,模型A和基准的kappa:

kappa = (p0-pe) / (n-pe)

其中,P0 = 对角线单元中观测值的总和;pe = 对角线单元中期望值的总和。

根据kappa的计算方法分为简单kappa(simple kappa)和加权kappa(weighted kappa),加权kappa又分为linear weighted kappaquadratic weighted kappa。

weighted kappa

关于linear还是quadratic weighted kappa的选择,取决于你的数据集中不同class之间差异的意义。比如对于眼底图像识别的数据,class=0为健康,class=4为疾病晚期非常严重,所以对于把class=0预测成4的行为所造成的惩罚应该远远大于把class=0预测成class=1的行为,使用quadratic的话0->4所造成的惩罚就等于16倍的0->1的惩罚。如下图是一个四分类的两个计算方法的比较。

Python实现

参考:https://github.com/benhamner/Metrics/blob/master/Python/ml_metrics/quadratic_weighted_kappa.py

#! /usr/bin/env python2.7import numpy as npdef confusion\_matrix(rater\_a, rater\_b, min\_rating=None, max\_rating=None):"""Returns the confusion matrix between rater's ratings"""assert(len(rater_a) == len(rater_b))if min_rating is None:min_rating = min(rater_a + rater_b)if max_rating is None:max_rating = max(rater_a + rater_b)num_ratings = int(max_rating - min_rating + 1)conf_mat = [[0 for i in range(num_ratings)]for j in range(num_ratings)]for a, b in zip(rater_a, rater_b):conf_mat[a - min_rating][b - min_rating] += 1return conf_matdef histogram(ratings, min\_rating=None, max\_rating=None):"""Returns the counts of each type of rating that a rater made"""if min_rating is None:min_rating = min(ratings)if max_rating is None:max_rating = max(ratings)num_ratings = int(max_rating - min_rating + 1)hist_ratings = [0 for x in range(num_ratings)]for r in ratings:hist_ratings[r - min_rating] += 1return hist_ratingsdef quadratic\_weighted\_kappa(rater\_a, rater\_b, min\_rating=None, max\_rating=None):"""Calculates the quadratic weighted kappaquadratic\_weighted\_kappa calculates the quadratic weighted kappavalue, which is a measure of inter-rater agreement between two ratersthat provide discrete numeric ratings. Potential values range from -1(representing complete disagreement) to 1 (representing completeagreement). A kappa value of 0 is expected if all agreement is due tochance.quadratic\_weighted\_kappa(rater\_a, rater\_b), where rater\_a and rater\_beach correspond to a list of integer ratings. These lists must have thesame length.The ratings should be integers, and it is assumed that they containthe complete range of possible ratings.quadratic\_weighted\_kappa(X, min\_rating, max\_rating), where min\_ratingis the minimum possible rating, and max\_rating is the maximum possiblerating"""rater_a = np.array(rater_a, dtype=int)rater_b = np.array(rater_b, dtype=int)assert(len(rater_a) == len(rater_b))if min_rating is None:min_rating = min(min(rater_a), min(rater_b))if max_rating is None:max_rating = max(max(rater_a), max(rater_b))conf_mat = confusion_matrix(rater_a, rater_b,min_rating, max_rating)num_ratings = len(conf_mat)num_scored_items = float(len(rater_a))hist_rater_a = histogram(rater_a, min_rating, max_rating)hist_rater_b = histogram(rater_b, min_rating, max_rating)numerator = 0.0denominator = 0.0for i in range(num_ratings):for j in range(num_ratings):expected_count = (hist_rater_a[i] * hist_rater_b[j]/ num_scored_items)d = pow(i - j, 2.0) / pow(num_ratings - 1, 2.0)numerator += d * conf_mat[i][j] / num_scored_itemsdenominator += d * expected_count / num_scored_itemsreturn 1.0 - numerator / denominatordef linear\_weighted\_kappa(rater\_a, rater\_b, min\_rating=None, max\_rating=None):"""Calculates the linear weighted kappalinear\_weighted\_kappa calculates the linear weighted kappavalue, which is a measure of inter-rater agreement between two ratersthat provide discrete numeric ratings. Potential values range from -1(representing complete disagreement) to 1 (representing completeagreement). A kappa value of 0 is expected if all agreement is due tochance.linear\_weighted\_kappa(rater\_a, rater\_b), where rater\_a and rater\_beach correspond to a list of integer ratings. These lists must have thesame length.The ratings should be integers, and it is assumed that they containthe complete range of possible ratings.linear\_weighted\_kappa(X, min\_rating, max\_rating), where min\_ratingis the minimum possible rating, and max\_rating is the maximum possiblerating"""assert(len(rater_a) == len(rater_b))if min_rating is None:min_rating = min(rater_a + rater_b)if max_rating is None:max_rating = max(rater_a + rater_b)conf_mat = confusion_matrix(rater_a, rater_b,min_rating, max_rating)num_ratings = len(conf_mat)num_scored_items = float(len(rater_a))hist_rater_a = histogram(rater_a, min_rating, max_rating)hist_rater_b = histogram(rater_b, min_rating, max_rating)numerator = 0.0denominator = 0.0for i in range(num_ratings):for j in range(num_ratings):expected_count = (hist_rater_a[i] * hist_rater_b[j]/ num_scored_items)d = abs(i - j) / float(num_ratings - 1)numerator += d * conf_mat[i][j] / num_scored_itemsdenominator += d * expected_count / num_scored_itemsreturn 1.0 - numerator / denominatordef kappa(rater\_a, rater\_b, min\_rating=None, max\_rating=None):"""Calculates the kappakappa calculates the kappavalue, which is a measure of inter-rater agreement between two ratersthat provide discrete numeric ratings. Potential values range from -1(representing complete disagreement) to 1 (representing completeagreement). A kappa value of 0 is expected if all agreement is due tochance.kappa(rater\_a, rater\_b), where rater\_a and rater\_beach correspond to a list of integer ratings. These lists must have thesame length.The ratings should be integers, and it is assumed that they containthe complete range of possible ratings.kappa(X, min\_rating, max\_rating), where min\_ratingis the minimum possible rating, and max\_rating is the maximum possiblerating"""assert(len(rater_a) == len(rater_b))if min_rating is None:min_rating = min(rater_a + rater_b)if max_rating is None:max_rating = max(rater_a + rater_b)conf_mat = confusion_matrix(rater_a, rater_b,min_rating, max_rating)num_ratings = len(conf_mat)num_scored_items = float(len(rater_a))hist_rater_a = histogram(rater_a, min_rating, max_rating)hist_rater_b = histogram(rater_b, min_rating, max_rating)numerator = 0.0denominator = 0.0for i in range(num_ratings):for j in range(num_ratings):expected_count = (hist_rater_a[i] * hist_rater_b[j]/ num_scored_items)if i == j:d = 0.0else:d = 1.0numerator += d * conf_mat[i][j] / num_scored_itemsdenominator += d * expected_count / num_scored_itemsreturn 1.0 - numerator / denominatordef mean\_quadratic\_weighted\_kappa(kappas, weights=None):"""Calculates the mean of the quadraticweighted kappas after applying Fisher's r-to-z transform, which isapproximately a variance-stabilizing transformation. Thistransformation is undefined if one of the kappas is 1.0, so all kappavalues are capped in the range (-0.999, 0.999). The reversetransformation is then applied before returning the result.mean\_quadratic\_weighted\_kappa(kappas), where kappas is a vector ofkappa valuesmean\_quadratic\_weighted\_kappa(kappas, weights), where weights is a vectorof weights that is the same size as kappas. Weights are applied in thez-space"""kappas = np.array(kappas, dtype=float)if weights is None:weights = np.ones(np.shape(kappas))else:weights = weights / np.mean(weights)# ensure that kappas are in the range [-.999, .999]kappas = np.array([min(x, .999) for x in kappas])kappas = np.array([max(x, -.999) for x in kappas])z = 0.5 * np.log((1 + kappas) / (1 - kappas)) * weightsz = np.mean(z)return (np.exp(2 * z) - 1) / (np.exp(2 * z) + 1)def weighted\_mean\_quadratic\_weighted\_kappa(solution, submission):predicted_score = submission[submission.columns[-1]].copy()predicted_score.name = "predicted\_score"if predicted_score.index[0] == 0:predicted_score = predicted_score[:len(solution)]predicted_score.index = solution.indexcombined = solution.join(predicted_score, how="left")groups = combined.groupby(by="essay\_set")kappas = [quadratic_weighted_kappa(group[1]["essay\_score"], group[1]["predicted\_score"]) for group in groups]weights = [group[1]["essay\_weight"].irow(0) for group in groups]return mean_quadratic_weighted_kappa(kappas, weights=weights)

一致性检验评价方法kappa相关推荐

  1. GIS 地质灾害评价——易发性评价方法的选择

    20世纪六十年代,人们开始了对地质灾害易发性评价的研究,九十年代后伴随着计算科学和信息量论以及模糊数学论等众多学科被引入到地质灾害的研究领域,地质灾害易发性评价由最初的定性发展逐步向定量发展转换,即以 ...

  2. 对《基于机器学习的区域滑坡危险性评价方法综述》阅读的总结

    对<基于机器学习的区域滑坡危险性评价方法综述>阅读的总结 1.摘要 ​ 这篇综述主要系统阐述了:作者通过阅读文献,总结了基于机器学习技术解决滑坡危险性评价方法:可以分为 1.评价因子选择 ...

  3. 层次分析法在gis方面评价方法

    ##层次分析法在gis方面评价方法 一.评价方法 层次分析法进行建模,大致分为以下四步: 1.分析系统中各因素之间的关系,建立系统的递阶层次结构. 2.对于同一层次的个元素关于上一层次中某一准则的重要 ...

  4. MPB:东林牛犇组玉米根系简化细菌群落的定量与其生物防治效果的评价方法(视频)...

    为进一步提高<微生物组实验手册>稿件质量,本项目新增大众评审环节.文章在通过同行评审后,采用公众号推送方式分享全文,任何人均可在线提交修改意见.公众号格式显示略有问题,建议电脑端点击文末阅 ...

  5. 音视频技术:视频质量评价方法简介

    视频质量评估(VQA)一直是个很活跃的研究领域,原因其一是业内一直缺少一种统一且准确的评估标准,其二是影响视频质量的因素过多,且包含很多主观因素,难以客观.定量地评价.经过这么多年的研究,已经诞生了非 ...

  6. 音视频技术:视频质量评价方法简介 1

    视频质量评估(VQA)一直是个很活跃的研究领域,原因其一是业内一直缺少一种统一且准确的评估标准,其二是影响视频质量的因素过多,且包含很多主观因素,难以客观.定量地评价.经过这么多年的研究,已经诞生了非 ...

  7. 机器学习第10天:模型评价方法及代码实现

    文章目录 一.分类评价指标 1.精确率(Precision) 2.召回率(Recall) 3.准确率(Accuracy) 4.F1_score 二.回归评价指标 1.平方根误差(RMSE) 2.均方误 ...

  8. 模式识别的评价方法:ROC曲线, DET曲线, FPPW, FPPI

    转载自:http://blog.csdn.net/pb09013037/article/details/48949037 因个人在做模式识别相关的工作,模式识别算法最终的性能评价是关键.但苦于网上很难 ...

  9. 视频质量评价方法简介

    周鑫 2017年毕业于浙江大学,读研期间主要进行视频编码相关研究,目前在通信与视频部门进行转码引擎相关研发. 作者简介 ●●● 引言 视频质量评估(VQA)一直是个很活跃的研究领域,原因其一是业内一直 ...

  10. 直播 | 清华大学关健:利用自监督学习的开放端故事生成评价方法

    「AI Drive」是由 PaperWeekly 和 biendata 共同发起的学术直播间,旨在帮助更多的青年学者宣传其最新科研成果.我们一直认为,单向地输出知识并不是一个最好的方式,而有效地反馈和 ...

最新文章

  1. 高温保护_连续4天高温预警!高温作业,这些劳动保护知识一定要懂!
  2. Android SystemServer分析
  3. 为什么手机游戏手柄没有流行起来?
  4. NOIP2021:游记
  5. oracle arp绑定mac地址,使用ARP命令来绑定IP和MAC地址
  6. Windows Live Messenger 新功能预览
  7. MICROSOFT REPORT VIEWER 2012之无法加载相关的dll
  8. 从Oracle到PostgreSQL:一文掌握Checkpoint重要概念
  9. 日更第8期-2015-3-23-如何科学地使用因特网-第三讲-为什么要用Git Bash?咱们用Github for Windows吧!(上)...
  10. 【zz】Matlab 二值图像形态学函数 bwmorph
  11. UVA10929 You can say 11【大数模除】
  12. js生成1~100个随机不重复数
  13. 安装破解版的edraw max
  14. java项目:基于ssm的高校后勤管理系统(spring+spring mvc+mybatis+easyui+jquery)1004
  15. HttpClient使用详解
  16. 武汉大学计算机学院 毕业答辩,【2017年整理】毕业与答辩ppt模板武汉大学.ppt
  17. php redis统计在线人数,每天活跃度
  18. 小米市场魅力所在?你读懂小米了么?
  19. 教培机构如何深耕种子用户从0到1-线上线下教学的有效融合
  20. 朴素贝叶斯--新浪新闻分类实例

热门文章

  1. 灵感爆发:An/flash 影片剪辑动画播放一遍后,摆脱paly()的控制
  2. CST2018/2020安装注意事项
  3. Oracle动态性能视图学习之v$session_longops
  4. 发射功率dBm 换算表
  5. 老人与老浏览器-李开复与成熟度最高的VRML浏览器SGI Cosmo
  6. adobe dreamweaver cs6 css,Adobe Dreamweaver CS6
  7. Ps钢笔工具及其附属工具的用法
  8. unity 获取设备的GPS信息
  9. 解决No instances available for XXX
  10. 山地车中轴进水表现_4种自行车中轴的拆卸和保养方法