欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pandas庫中dataframe.corr()函數(shù)的使用

 更新時間:2024年07月22日 11:35:59   作者:Midsummer-逐夢  
dataframe.corr()是Pandas庫中的一個函數(shù),用于計算DataFrame中各列之間的相關系數(shù),本文主要介紹了Pandas庫中dataframe.corr()函數(shù)的使用,具有一定的參考價值,感興趣的可以了解一下

一、簡介

dataframe.corr()是Pandas庫中的一個函數(shù),用于計算DataFrame中各列之間的相關系數(shù)。相關系數(shù)衡量的是兩個變量之間線性關系的強度和方向,結果在-1到1之間,分別表示完全負相關和完全正相關。

二、語法和參數(shù)

DataFrame.corr(method='pearson', min_periods=1)

method: 可選。計算相關系數(shù)的方法,有’pearson’(默認)、‘kendall’、'spearman’三種可選。

  • 'pearson':標準皮爾遜相關系數(shù)。
  • 'kendall':肯德爾等級相關系數(shù)。
  • 'spearman':斯皮爾曼等級相關系數(shù)。

min_periods: 可選。每對元素的最小數(shù)量,以便計算相關系數(shù)。

三、實例

3.1 計算默認的皮爾遜相關系數(shù)

import pandas as pd

# 創(chuàng)建示例數(shù)據(jù)
data = {
    'A': [1, 2, 3, 4, 5],
    'B': [5, 4, 3, 2, 1],
    'C': [2, 2, 3, 4, 4]
}
df = pd.DataFrame(data)

# 計算相關系數(shù)
correlation_matrix = df.corr()
print(correlation_matrix)

輸出:

          A         B         C
A  1.000000 -1.000000  0.948683
B -1.000000  1.000000 -0.948683
C  0.948683 -0.948683  1.000000

3.2 計算斯皮爾曼相關系數(shù)

import pandas as pd

# 創(chuàng)建示例數(shù)據(jù)
data = {
    'A': [1, 2, 3, 4, 5],
    'B': [5, 4, 3, 2, 1],
    'C': [2, 2, 3, 4, 4]
}
df = pd.DataFrame(data)

# 計算相關系數(shù)
correlation_matrix = df.corr(method='spearman')
print(correlation_matrix)

輸出:

          A         B         C
A  1.000000 -1.000000  0.948683
B -1.000000  1.000000 -0.948683
C  0.948683 -0.948683  1.000000

3.3 計算斯皮爾曼相關系數(shù)

import pandas as pd

# 創(chuàng)建示例數(shù)據(jù)
data = {
    'A': [1, 2, 3, 4, 5],
    'B': [5, 4, 3, 2, 1],
    'C': [2, 2, 3, 4, 4]
}
df = pd.DataFrame(data)

# 計算相關系數(shù)
correlation_matrix = df.corr(method='kendall')
print(correlation_matrix)

輸出

          A         B         C
A  1.000000 -1.000000  0.894427
B -1.000000  1.000000 -0.894427
C  0.894427 -0.894427  1.000000

四、注意事項

  • 當使用kendallspearman方法時,計算可能會比pearson方法慢,因為這些方法需要排序。
  • 如果數(shù)據(jù)集中存在NaN值,默認情況下這些值會被忽略。
  • 計算相關系數(shù)前,確保數(shù)據(jù)已經(jīng)清洗并準備好,以避免錯誤或不準確的結果。

到此這篇關于Pandas庫中dataframe.corr()函數(shù)的使用的文章就介紹到這了,更多相關Pandas dataframe.corr()函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家! 

相關文章

最新評論