資料挖掘01-相關性分析及可視化【Pearson, Spearman, Kendall】
- 簡介
- 一、什么是相關性分析
- 二、常見的相關性分析方法
- 三、Pearson相關系數
- 使用pandas對資料做Pearson相關性分析
- 四、Spearman等級相關系數
- 4.1 什么是等級相關
- 4.2 為什么要運用等級相關?
- 4.3 使用pandas對資料做Spearman相關性分析
- 五、Kendall相關系數
- 使用pandas對資料做Kendall相關性分析
- 六、下三角相關性矩陣
- 七、重點相關性矩陣
- 八、參考資料:
簡介
? 有這么一句話在業界廣泛流傳:資料和特征決定了機器學習的上限,而模型和演算法只是逼近這個上限而已,
? 因此,資料挖掘在人工智能和大資料的時代下顯得尤為重要,本人在作業中也會經常為資料挖掘方面的任務頭疼,所以想將所見、所學、所整理的資料挖掘學習資料進行總結,
? 首先,就來說一下資料挖掘最常見的手段:相關性分析,
一、什么是相關性分析
? 相關性分析是指對兩個或多個具備相關性的變數元素進行分析,從而衡量兩個變數因素的相關密切程度,相關性的元素之間需要存在一定的聯系或者概率才可以進行相關性分析,相關性不等于因果性,也不是簡單的個性化,相關性所涵蓋的范圍和領域幾乎覆寫了我們所見到的方方面面,相關性在不同的學科里面的定義也有很大的差異,
二、常見的相關性分析方法
? 常見的相關性分析方法有三種:Pearson相關系數、Spearman等級相關系數和Kendall相關系數,現實場景中使用Pearson相關系數的情況比較多,
| 相關分析系數 | 適用場景 | 備注 |
|---|---|---|
| Pearson | 定量資料,資料滿足正態分布 | 正態圖可查看正態性,散點圖展示資料關系 |
| Spearman | 定量資料,資料不滿足正態分布 | 正態圖可查看正態性,散點圖展示資料關系 |
| Kendall | 定量資料一致性判斷 | 通常用于評分資料一致性水平研究【非關系研究】 如評委打分,資料排名等 |

三、Pearson相關系數
? Pearson相關性系數可以看做是升級版的歐式距離平方,因為它提供了對于變數取值范圍不同的處理步驟,因此對不同變數間的取值范圍沒有要求,最后得到的相關性所衡量的是趨勢,而不同變數量綱上的差別在計算程序中去掉了,等價于z-score標準化,【源自:如何理解皮爾遜相關系數(Pearson Correlation Coefficient)?】
使用pandas對資料做Pearson相關性分析
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 1.造資料
df = pd.DataFrame()
df["x"] = np.random.uniform(-2, 2, 1_000_000)
df["error"] = np.random.uniform(-0.5, 0.5, 1_000_000)
df["y"] = df["x"] * df["x"] + df["error"]
df["y_perfect"] = df["x"] * df["x"]
# 2.相關分析熱力圖可視化, df.corr()默認引數為pearson
plt.figure(figsize=[10, 6])
sns.heatmap(df.corr(), vmin=0, vmax=1, cmap="Reds", linewidths=0.5, annot=True)
plt.show()

四、Spearman等級相關系數
4.1 什么是等級相關
等級相關,也稱為秩相關,屬于非引數統計方法,但對原變數的分布不作要求,適用于那些不服從正態分布的資料,還有總體分布未知和原始資料用等級表示的資料,
4.2 為什么要運用等級相關?
實際中,如果遇到定類變數或者定序變數的“相關系數”,就需要用到Spearman(斯皮爾曼)等級相關系數和Kendall(肯德爾)的tau相關系數,
4.3 使用pandas對資料做Spearman相關性分析
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 1.造資料
df = pd.DataFrame()
df["x"] = np.random.uniform(-2, 2, 1_000_000)
df["error"] = np.random.uniform(-0.5, 0.5, 1_000_000)
df["y"] = df["x"] * df["x"] + df["error"]
df["y_perfect"] = df["x"] * df["x"]
# 2.相關分析熱力圖可視化, df.corr() method=spearman指定系數
plt.figure(figsize=[10, 6])
sns.heatmap(df.corr(method='spearman'), vmin=0, vmax=1, cmap="Reds", linewidths=0.5, annot=True)
plt.show()

五、Kendall相關系數
Kendall協調系數,也稱作Kendall和諧系數,或Kendall一致性系數,通常用于比較多組資料的一致性程度,
kendall 相關是反映順序變數之間的相關程度的量,使用該相關分析方法時不需要變數所在的總體一定要呈正態分布,也不需要樣本容量大于30,可見,Kendall相關歸屬于非引數檢驗,
使用pandas對資料做Kendall相關性分析
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 1.造資料
df = pd.DataFrame()
df["x"] = np.random.uniform(-2, 2, 1_000_000)
df["error"] = np.random.uniform(-0.5, 0.5, 1_000_000)
df["y"] = df["x"] * df["x"] + df["error"]
df["y_perfect"] = df["x"] * df["x"]
# 2.相關分析熱力圖可視化, df.corr() method=kendall指定系數
plt.figure(figsize=[10, 6])
sns.heatmap(df.corr(method='kendall'), vmin=0, vmax=1, cmap="Reds", linewidths=0.5, annot=True)
plt.show()

六、下三角相關性矩陣
相關性矩陣繪制的是兩兩變數之間的相關性,所以是一個對稱的矩陣,所以只需保留上三角矩陣或者下三角矩陣的內容即可,
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 1.造資料
df = pd.DataFrame()
df["x"] = np.random.uniform(-2, 2, 1_000_000)
df["error"] = np.random.uniform(-0.5, 0.5, 1_000_000)
df["y"] = df["x"] * df["x"] + df["error"]
df["y_perfect"] = df["x"] * df["x"]
# 2.下三角相關矩陣熱力圖
plt.figure(figsize=[10, 6])
matrix = df.corr()
cmap = sns.diverging_palette(250, 15, s=75, l=40, n=9, center="light", as_cmap=True)
# mask掉上三角部分
mask = np.triu(np.ones_like(matrix, dtype=bool))
plt.figure(figsize=(12, 8))
sns.heatmap(matrix, mask=mask, center=0, annot=True, fmt='.2f', square=True, cmap=cmap)
plt.show()

七、重點相關性矩陣
在相關矩陣熱力圖中,我們可以依據顏色的深淺來判別特征之間的強弱相關性,但是在實際場景中我們只想關注相關性較高的那塊,可以通過過濾來實作,
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 1.造資料
df = pd.DataFrame()
df["x"] = np.random.uniform(-2, 2, 1_000_000)
df["error"] = np.random.uniform(-0.5, 0.5, 1_000_000)
df["y"] = df["x"] * df["x"] + df["error"]
df["y_perfect"] = df["x"] * df["x"]
# 2.重點相關性矩陣熱力圖
plt.figure(figsize=[10, 6])
matrix = df.corr()
cmap = sns.diverging_palette(250, 15, s=75, l=40, n=9, center="light", as_cmap=True)
# mask掉上三角 & 小于某個閾值的值
mask1 = np.triu(np.ones_like(matrix, dtype=bool))
mask2 = np.abs(matrix) <= 0.1
mask = mask1 | mask2
plt.figure(figsize=(12, 8))
sns.heatmap(matrix, mask=mask, center=0, annot=True, fmt='.2f', square=True, cmap=cmap)
plt.show()

八、參考資料:
【知乎】皮爾遜相關性分析怎么看?
【知乎】斯皮爾曼等級相關(Spearman’s correlation coefficient for ranked data)
【知乎】Spearman等級相關
【微信公眾號-kaggle競賽寶典】特征相關性挖掘神器-線性非線性關系一鍵挖掘!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/299940.html
標籤:其他
