日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Python:Kendall tau相关系数的计算

發布時間:2024/1/1 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python:Kendall tau相关系数的计算 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

關于Kendall correlation coefficient的介紹可參見:

維基百科:https://en.wikipedia.org/wiki/Kendall_rank_correlation_coefficient

scipy庫:https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.kendalltau.html

寫了兩個版本的代碼:

import numpy as np from scipy.stats import kendalltaudata = np.array([[0.7,0.2,0.5,0],[0.6,0.1,0.1,0],[0.8,0.2,0.3,0],[0.6,0.3,0.5,0],[0.5,0.4,0.2,1],[0.4,0.5,0.3,1],[0.5,0.6,0.3,1],[0.4,0.5,0.1,1],[0.2,0.9,0.4,2],[0.2,1,0.6,2],[0.2,0.8,0.5,2],[0.1,1,0.1,2]])Lens =len(data) count = 0 for i in range(Lens-1):for j in range(i+1,Lens):count = count + np.sign(data[i][0] - data[j][0]) * np.sign(data[i][2] - data[j][2])print('-----') kendallCorrelation = count/((Lens*(Lens-1))/2) kendallCorrelation_1,p_value = kendalltau(data[:,0],data[:,2])print(kendallCorrelation) print(kendallCorrelation_1) from scipy.stats import kendalltau import numpy as np a = [1,2,3,4,5,6,7,8,9,10] b = [4,7,2,10,3,6,8,1,5,9] # a = [12,2,1,12,2] # b = [1,4,7,1,0] Lens = len(a) count = 0 number = 0 for i in range(Lens-1):for j in range(i+1,Lens):count = count + np.sign(a[i] - a[j]) * np.sign(b[i] - b[j])number += 1Kendallta1 = count/(Lens*(Lens-1)/2) Kendallta2,p_value = kendalltau(a,b) print(number) print(count) print(Kendallta1) print(Kendallta2)

?

結果為啥算出來不一樣呢?【原因:自己寫的是1938年的tau系數,scipy庫里面的1945年版本的tau】

下面是1945年版本的代碼:

from scipy.stats import kendalltau import numpy as np a = [1,1,2,2,5,5,8,8,9,10] b = [2,7,2,3,3,6,8,4,5,5] # a = [12,2,1,12,2] # b = [1,4,7,1,0] Lens = len(a)ties_onlyin_x = 0 ties_onlyin_y = 0 con_pair = 0 dis_pair = 0 for i in range(Lens-1):for j in range(i+1,Lens):test_tying_x = np.sign(a[i] - a[j])test_tying_y = np.sign(b[i] - b[j])panduan =test_tying_x * test_tying_yif panduan == 1:con_pair +=1elif panduan == -1:dis_pair +=1if test_tying_y ==0 and test_tying_x != 0:ties_onlyin_y += 1elif test_tying_x == 0 and test_tying_y !=0:ties_onlyin_x += 1Kendallta1 = (con_pair - dis_pair)/np.sqrt((con_pair + dis_pair + ties_onlyin_x)*(dis_pair +con_pair + ties_onlyin_y)) Kendallta2,p_value = kendalltau(a,b)print(Kendallta1) print(Kendallta2)

跑出來結果是一樣的:

0.33737388489831593
0.33737388489831593

?

總結

以上是生活随笔為你收集整理的Python:Kendall tau相关系数的计算的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。