斯皮尔曼等级相关性-Spearman Rank Correlation

一、简介

在机器学习中,当要预测不同的机器学习算法在同一个学习任务上的性能时,需要使用序相关系数对真实的性能排序与预测的性能排序进行比较,本文介绍了其中一种秩相关系数——斯皮尔曼等级相关性。

公式:

其中:di=xi-yi表示两个排序之间的差值;

            n:表示样本的大小,即机器学习算法的数量;

二、代码实现

1.计算斯皮尔曼等级相关性

[java]  view plain  copy
  1. package recommendation.featureExtraction;  
  2.   
  3. import java.math.BigDecimal;  
  4.   
  5. /** 
  6.  * spearman rank correlation 
  7.  * refer to soruce from http://en.wikipedia.org/wiki/Spearman_rank_correlation 
  8.  * @author LiuKai 2014.07.06 
  9.  */  
  10. public class SpearmanRankCorrelation {  
  11.   
  12.     /* 
  13.      * rang between [-1, 1]; 
  14.      * Inappropriate static <<Clean code: A Handbook of Agile Software Craftsmanship>>. 
  15.      * "There is almonst no chance that we'd want Math.max to be polymorphic" 
  16.      */  
  17.     public static double correlation(int[] base, int[] other) {  
  18.           
  19.         if (base.length != other.length) {  
  20.             System.err.println("The length of array base and other must be equal!");  
  21.             return 0.0;  
  22.         }  
  23.         if (base.length <= 1) {  
  24.             System.err.println("The length of both of the arrays must be equal!");  
  25.             System.exit(-1);  
  26.         }  
  27.         BigDecimal bg = new BigDecimal(1-(6.0*sumOfSquareDiff(base, other)/(power(base.length, 3)-base.length)));  
  28.         return bg.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();  
  29.     }  
  30.       
  31.     private static double sumOfSquareDiff(int[] a, int[] b) {  
  32.           
  33.         double sum = 0.0;  
  34.         for (int i = 0; i < a.length; i++)   
  35.             sum += power((a[i]-b[i]), 2);  
  36.         return sum;  
  37.     }  
  38.       
  39.     /* 
  40.      * Method should do one thing  
  41.      * <<Clean code: A handbook of Agile software craftsmanship>> 
  42.      */  
  43.     private static double power(double base, int exp) {  
  44.           
  45.         if (base == 0)  
  46.             return 0.0;  
  47.         double result = 1.0;  
  48.         for (int i = 0; i < exp; i++)  
  49.             result *= base;  
  50.         return result;  
  51.     }  
  52. }  
2.测试斯皮尔曼等级相关系数

[java]  view plain  copy
  1. package recommendation.featureExtraction;  
  2.   
  3. /** 
  4.  * Test SpearmanRankCorrelation.class 
  5.  * @author Administrator 
  6.  * 
  7.  */  
  8. public class TestSpearman {  
  9.       
  10.     public static void main(String[] args) {  
  11.         int[] base = {12345};  
  12.         int[] other1 = {12345};  
  13.         int[] other2 = FisherYatesShuffle.shuffle(base.clone());  
  14.         int[] other3 = {54321};  
  15.         System.out.println(SpearmanRankCorrelation.correlation(base, other1));    
  16.         System.out.println(SpearmanRankCorrelation.correlation(base, other2));  
  17.         System.out.println(SpearmanRankCorrelation.correlation(base, other3));  
  18.     }  
  19. }  
3.辅助类费雪耶兹随机置乱算法

具体见另一篇博客:http://blog.csdn.net/lhkaikai/article/details/25627161

[java]  view plain  copy
  1. package recommendation.featureExtraction;  
  2.   
  3. import java.util.Random;  
  4.   
  5. /* 
  6.  * Fisher-Yates shuffle, also known as the Knuth shuffle, is an algorithm for generating 
  7.  * a random permutation of a finite set-in plain terms, for randomly shuffling the set. 
  8.  */  
  9. public class FisherYatesShuffle {  
  10.       
  11.     public static int[] shuffle(int[] array) {  
  12.         for (int i = array.length - 1; i > 0; i--) {  
  13.             int rand = (new Random()).nextInt(i+1);  
  14.             int temp = array[i];  
  15.             array[i] = array[rand];  
  16.             array[rand] = temp;  
  17.         }  
  18.         return array;  
  19.     }  
  20.       
  21.     public static void main(String[] args) {  
  22.           
  23.         int[] array1= {12345};  
  24.         int[] array2 = shuffle(array1);  
  25.         for(int elem: array2)  
  26.             System.out.print(elem + " ");  
  27.         System.out.println();  
  28.     }  
  29. }  


三、实验测试结果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值