python基于scipy模块实现统计学中三大相关系数的计算

    最近用到了相关性分析的内容,想起来之前用过的皮尔森系数了,今天拿过来使用的时候发现统计学中关于相关性分析的部分有三大相关性系数,这里简单抱着拿来主义的心理去实践一下,具体的相关性系数计算方法都不需要自己去写了,python的第三方模块scipy中已经内置了相应的计算函数了,关于三者的异同点和适用场合我就不多介绍了,网上有很多相关的介绍的,下面是具体的实践:

#!usr/bin/env python
#encoding:utf-8

'''
__Author__:沂水寒城
功能: 基于scipy模块实现统计学中三大相关系数的计算
'''


import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from scipy.stats import pearsonr,spearmanr,kendalltau




def calFunc():
    '''
    基于scipy模块实现统计学中三大相关系数的计算
    '''
    vector1=[1.34,5.43,2.11,6.08,-2.31,1.11,-0.19]
    vector2=[2.01,4.67,1.01,4.33,-3.45,-1.37,2.32]
    print 'vector1 and vector1 similarity:'
    print '{0} and {1} pearsonr: {2}'.format('vector1','vector1',pearsonr(vector1,vector1)) 
    print '{0} and {1} spearmanr: {2}'.format('vector1','vector1',spearmanr(vector1,vector1))
    print '{0} and {1} spearmanr: {2}'.format('vector1','vector1',kendalltau(vector1,vector1))

    print 'vector2 and vector2 similarity:'
    print '{0} and {1} pearsonr: {2}'.format('vector2','vector2',pearsonr(vector2,vector2)) 
    print '{0} and {1} spearmanr: {2}'.format('vector2','vector2',spearmanr(vector2,vector2))
    print '{0} and {1} spearmanr: {2}'.format('vector2','vector2',kendalltau(vector2,vector2))

    print 'vector1 and vector2 similarity:'
    print '{0} and {1} pearsonr: {2}'.format('vector1','vector2',pearsonr(vector1,vector2)) 
    print '{0} and {1} spearmanr: {2}'.format('vector1','vector2',spearmanr(vector1,vector2))
    print '{0} and {1} spearmanr: {2}'.format('vector1','vector2',kendalltau(vector1,vector2))



if __name__=='__main__':
    calFunc()

     结果如下:

vector1 and vector1 similarity:
vector1 and vector1 pearsonr: (1.0, 0.0)
vector1 and vector1 spearmanr: SpearmanrResult(correlation=1.0, pvalue=0.0)
vector1 and vector1 spearmanr: KendalltauResult(correlation=1.0, pvalue=0.001610794963892616)
vector2 and vector2 similarity:
vector2 and vector2 pearsonr: (1.0, 0.0)
vector2 and vector2 spearmanr: SpearmanrResult(correlation=1.0, pvalue=0.0)
vector2 and vector2 spearmanr: KendalltauResult(correlation=1.0, pvalue=0.001610794963892616)
vector1 and vector2 similarity:
vector1 and vector2 pearsonr: (0.8406893832306453, 0.017822028855280576)
vector1 and vector2 spearmanr: SpearmanrResult(correlation=0.7142857142857144, pvalue=0.07134356146753759)
vector1 and vector2 spearmanr: KendalltauResult(correlation=0.5238095238095238, pvalue=0.09852102062039901)

      从计算结果中可以看到:

               向量1和向量2的相关性还是很高的,向量自身之间的相关性都是1,每个计算结果的第二个参数表示的是置信度。

你可能感兴趣的:(编程技术,数学)