python 利用Scipy計算person 和spearman相關系數


python 利用Scipy計算person 和spearman相關系數

覺得有用的話,歡迎一起討論相互學習~

我的微博我的github我的B站

  • 學習以下兩位大佬的講解

(Pearson)皮爾遜相關系數和spearman相關系數(附python實現)

相關性系數及其python實現

皮爾遜相關系數

  • 下面是皮爾遜相關系數的計算公式,只需要將(X和Y的協方差)/(X的標准差*Y的標准差)

spearman相關系數

簡單的相關系數的分類

那么對於這兩個系數,怎樣的值才是好的呢,遵循下面的關系

0.8-1.0:極強相關
0.6-0.8:強相關
0.4-0.6:中等強度相關
0.2-0.4:弱相關
0.0-0.2:極弱或者無相關

區別

  • 那么有個問題,是所有的變量都可以用這兩個系數嗎,這兩個變量當然是有區別的,區別如下。
  1. 連續數據,正態分布,線性關系,用pearson相關系數是最恰當,當然用spearman相關系數也可以,效率沒有pearson相關系數高。
  2. 上述任一條件不滿足,就用spearman相關系數,不能用pearson相關系數。
  3. 兩個定序測量數據(順序變量)之間也用spearman相關系數,不能用pearson相關系數。
  4. Pearson相關系數的一個明顯缺陷是,作為特征排序機制,他只對線性關系敏感。如果關系是非線性的,即便兩個變量具有一一對應的關系,Pearson相關性也可能會接近0。

Code

import scipy.stats

# Create two lists of random values
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [2, 1, 2, 4.5, 7, 6.5, 6, 9, 9.5]
print(scipy.stats.pearsonr(x, y)[0])
# 0.9412443251336238
print(scipy.stats.spearmanr(x, y)[0])
# 0.903773601456181

Code-檢驗spearman和pearson之間的關系

# Create two lists of random values
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [2, 1, 2, 4.5, 7, 6.5, 6, 9, 9.5]


# Calculate Spearman’s Rank Correlation
# Spearman’s rank correlation is the Pearson’s correlation coefficient of the ranked version of the variables.

# Create a function that takes in x's and y's
def spearmans_rank_correlation(xs, ys):
    # Calculate the rank of x's
    xranks = pd.Series(xs).rank()

    # Caclulate the ranking of the y's
    yranks = pd.Series(ys).rank()

    # Calculate Pearson's correlation coefficient on the ranked versions of the data
    return scipy.stats.pearsonr(xranks, yranks)  # 計算pearson系數


print(spearmans_rank_correlation(x, y)[0])
# 0.9037736014561808

# Calculate Spearman’s Correlation Using SciPy
# 計算spearman秩相關系數
# Just to check our results, here it Spearman's using Scipy
print(scipy.stats.spearmanr(x, y)[0])
# 0.903773601456181


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM