簡(jiǎn)介
? 有這么一句話在業(yè)界廣泛流傳:數(shù)據(jù)和特征決定了機(jī)器學(xué)習(xí)的上限,而模型和算法只是逼近這個(gè)上限而已。
? 因此,數(shù)據(jù)挖掘在人工智能和大數(shù)據(jù)的時(shí)代下顯得尤為重要。本人在工作中也會(huì)經(jīng)常為數(shù)據(jù)挖掘方面的任務(wù)頭疼,所以想將所見、所學(xué)、所整理的數(shù)據(jù)挖掘?qū)W習(xí)資料進(jìn)行總結(jié)。
? 首先,就來說一下數(shù)據(jù)挖掘最常見的手段:相關(guān)性分析。
一、什么是相關(guān)性分析
? 相關(guān)性分析是指對(duì)兩個(gè)或多個(gè)具備相關(guān)性的變量元素進(jìn)行分析,從而衡量兩個(gè)變量因素的相關(guān)密切程度。相關(guān)性的元素之間需要存在一定的聯(lián)系或者概率才可以進(jìn)行相關(guān)性分析。相關(guān)性不等于因果性,也不是簡(jiǎn)單的個(gè)性化,相關(guān)性所涵蓋的范圍和領(lǐng)域幾乎覆蓋了我們所見到的方方面面,相關(guān)性在不同的學(xué)科里面的定義也有很大的差異。
二、常見的相關(guān)性分析方法
? 常見的相關(guān)性分析方法有三種:Pearson相關(guān)系數(shù)、Spearman等級(jí)相關(guān)系數(shù)和Kendall相關(guān)系數(shù)?,F(xiàn)實(shí)場(chǎng)景中使用Pearson相關(guān)系數(shù)的情況比較多。
相關(guān)分析系數(shù) | 適用場(chǎng)景 | 備注 |
---|---|---|
Pearson | 定量數(shù)據(jù),數(shù)據(jù)滿足正態(tài)分布 | 正態(tài)圖可查看正態(tài)性,散點(diǎn)圖展示數(shù)據(jù)關(guān)系 |
Spearman | 定量數(shù)據(jù),數(shù)據(jù)不滿足正態(tài)分布 | 正態(tài)圖可查看正態(tài)性,散點(diǎn)圖展示數(shù)據(jù)關(guān)系 |
Kendall | 定量數(shù)據(jù)一致性判斷 | 通常用于評(píng)分?jǐn)?shù)據(jù)一致性水平研究【非關(guān)系研究】 如評(píng)委打分,數(shù)據(jù)排名等 |
三、Pearson相關(guān)系數(shù)
? Pearson相關(guān)性系數(shù)可以看做是升級(jí)版的歐式距離平方,因?yàn)樗峁┝藢?duì)于變量取值范圍不同的處理步驟。因此對(duì)不同變量間的取值范圍沒有要求,最后得到的相關(guān)性所衡量的是趨勢(shì),而不同變量量綱上的差別在計(jì)算過程中去掉了,等價(jià)于z-score標(biāo)準(zhǔn)化。【源自:如何理解皮爾遜相關(guān)系數(shù)(Pearson Correlation Coefficient)?】
使用pandas對(duì)數(shù)據(jù)做Pearson相關(guān)性分析
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 1.造數(shù)據(jù)
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.相關(guān)分析熱力圖可視化, df.corr()默認(rèn)參數(shù)為pearson
plt.figure(figsize=[10, 6])
sns.heatmap(df.corr(), vmin=0, vmax=1, cmap="Reds", linewidths=0.5, annot=True)
plt.show()
四、Spearman等級(jí)相關(guān)系數(shù)
4.1 什么是等級(jí)相關(guān)
等級(jí)相關(guān),也稱為秩相關(guān),屬于非參數(shù)統(tǒng)計(jì)方法,但對(duì)原變量的分布不作要求。適用于那些不服從正態(tài)分布的數(shù)據(jù),還有總體分布未知和原始數(shù)據(jù)用等級(jí)表示的數(shù)據(jù)。
4.2 為什么要運(yùn)用等級(jí)相關(guān)?
實(shí)際中,如果遇到定類變量或者定序變量的“相關(guān)系數(shù)”,就需要用到Spearman(斯皮爾曼)等級(jí)相關(guān)系數(shù)和Kendall(肯德爾)的tau相關(guān)系數(shù)。
4.3 使用pandas對(duì)數(shù)據(jù)做Spearman相關(guān)性分析
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 1.造數(shù)據(jù)
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.相關(guān)分析熱力圖可視化, df.corr() method=spearman指定系數(shù)
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相關(guān)系數(shù)
Kendall協(xié)調(diào)系數(shù),也稱作Kendall和諧系數(shù),或Kendall一致性系數(shù)。通常用于比較多組數(shù)據(jù)的一致性程度。
kendall 相關(guān)是反映順序變量之間的相關(guān)程度的量,使用該相關(guān)分析方法時(shí)不需要變量所在的總體一定要呈正態(tài)分布,也不需要樣本容量大于30,可見,Kendall相關(guān)歸屬于非參數(shù)檢驗(yàn)。
使用pandas對(duì)數(shù)據(jù)做Kendall相關(guān)性分析
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 1.造數(shù)據(jù)
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.相關(guān)分析熱力圖可視化, df.corr() method=kendall指定系數(shù)
plt.figure(figsize=[10, 6])
sns.heatmap(df.corr(method='kendall'), vmin=0, vmax=1, cmap="Reds", linewidths=0.5, annot=True)
plt.show()
六、下三角相關(guān)性矩陣
相關(guān)性矩陣?yán)L制的是兩兩變量之間的相關(guān)性,所以是一個(gè)對(duì)稱的矩陣,所以只需保留上三角矩陣或者下三角矩陣的內(nèi)容即可。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 1.造數(shù)據(jù)
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.下三角相關(guān)矩陣熱力圖
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()
七、重點(diǎn)相關(guān)性矩陣
在相關(guān)矩陣熱力圖中,我們可以依據(jù)顏色的深淺來判別特征之間的強(qiáng)弱相關(guān)性,但是在實(shí)際場(chǎng)景中我們只想關(guān)注相關(guān)性較高的那塊,可以通過過濾來實(shí)現(xiàn)。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 1.造數(shù)據(jù)
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.重點(diǎn)相關(guān)性矩陣熱力圖
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掉上三角 & 小于某個(gè)閾值的值
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()
八、參考資料:
【知乎】皮爾遜相關(guān)性分析怎么看?
【知乎】斯皮爾曼等級(jí)相關(guān)(Spearman’s correlation coefficient for ranked data)
【知乎】Spearman等級(jí)相關(guān)文章來源:http://www.zghlxwxcb.cn/news/detail-432056.html
【微信公眾號(hào)-kaggle競(jìng)賽寶典】特征相關(guān)性挖掘神器-線性非線性關(guān)系一鍵挖掘!文章來源地址http://www.zghlxwxcb.cn/news/detail-432056.html
到了這里,關(guān)于數(shù)據(jù)挖掘01-相關(guān)性分析及可視化【Pearson, Spearman, Kendall】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!