EDA中常用的九個可視化圖表介紹和代碼示例
探索性數(shù)據(jù)分析(EDA)是數(shù)據(jù)科學(xué)家用來分析和調(diào)查數(shù)據(jù)集并總結(jié)其主要特征的一種方法,通常采用數(shù)據(jù)可視化技術(shù)。我們可以說EDA是通過創(chuàng)建可視化和摘要來調(diào)查和理解數(shù)據(jù)集的過程。EDA是我們詢問數(shù)據(jù)問題的一種方式,可以找出關(guān)于數(shù)據(jù)的所有信息,并理解它為什么是這樣的(即識別趨勢、模式、異常等)。
在這篇文章中我們介紹EDA中常用的9個圖表,并且針對每個圖表給出代碼示例。
1、條形圖/計數(shù)圖
顯示分類變量的分布??梢暬瘮?shù)據(jù)集中每個類別的頻率或計數(shù)。
import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset('tips')
sns.countplot(x='day', data=data)
plt.title('Count of Tips by Day')
plt.show()
2、箱線圖
顯示數(shù)據(jù)中的平均值、中位數(shù)、分位數(shù)和離群值。比較多個變量的分布。可以識別擴(kuò)散的數(shù)值變量,檢測數(shù)據(jù)集中潛在的異常值。
import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset('tips')
sns.boxplot(x='day', y='total_bill', data=data)
plt.title('Box Plot of Total Bill by Day')
plt.show()
3、密度圖
作為數(shù)據(jù)科學(xué)家,建議使用密度圖而不是直方圖,因?yàn)槲覀儾聹y最佳的裝箱數(shù)量是有問題的。
密度圖可以可視化連續(xù)變量的分布。識別數(shù)據(jù)中的峰值、低谷和總體模式。了解分布的形狀并比較多個變量的分布。
import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset('tips')
sns.kdeplot(data['total_bill'], shade=True)
plt.title('Density Plot of Total Bill')
plt.show()
4、散點(diǎn)圖
探索兩個連續(xù)變量之間的關(guān)系。識別數(shù)據(jù)中的模式、相關(guān)性或集群。
import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset('tips')
sns.scatterplot(x='total_bill', y='tip', data=data)
plt.title('Scatter Plot of Total Bill vs. Tip')
plt.show()
5、線型圖
在時間序列中顯示趨勢或模式。表示連續(xù)區(qū)間內(nèi)兩個連續(xù)變量之間的關(guān)系,還可以比較連續(xù)范圍內(nèi)變量的變化。
import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset('tips')
sns.lineplot(x='total_bill', y='tip', data=data)
plt.title('Line Plot of Tip Over Total Bill')
plt.show()
6、熱圖
顯示數(shù)值變量的相關(guān)矩陣。識別大型數(shù)據(jù)集中的模式和關(guān)系。
import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset('tips')
correlation_matrix = data.corr()
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')
plt.title('Correlation Heatmap')
plt.show()
7、小提琴圖
結(jié)合了箱形圖和核密度圖的特點(diǎn),可以可視化一個數(shù)值變量在不同類別中的分布。
import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset('tips')
sns.violinplot(x='day', y='total_bill', data=data)
plt.title('Violin Plot of Total Bill by Day')
plt.show()
8、子圖
為了進(jìn)行對比,可以在同一圖中并排比較多個子圖。
import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset('tips')
plt.figure(figsize=(12, 8))
plt.subplot(2, 2, 1)
sns.scatterplot(x='total_bill', y='tip', data=data)
plt.title('Scatter Plot of Total Bill vs Tip')
plt.subplot(2, 2, 2)
sns.boxplot(x='day', y='total_bill', data=data)
plt.title('Box Plot of Total Bill by Day')
plt.subplot(2, 2, 3)
sns.barplot(x='day', y='total_bill', data=data)
plt.title('Bar Plot of Total Bill by Day')
plt.subplot(2, 2, 4)
sns.histplot(data['total_bill'], kde=True)
plt.title('Histogram of Total Bill')
plt.tight_layout()
plt.show()
9、關(guān)系圖
Pairplot在中文中沒有特定的翻譯,我這里把它稱作關(guān)系圖,因?yàn)樗怯糜诶L制變量之間的關(guān)系,通過對多個變量進(jìn)行可視化來探索它們之間的相關(guān)性和趨勢。
import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset('tips')
sns.pairplot(data, hue='day')
plt.suptitle('Pairplot of Numerical Variables by Day', y=1.02)
plt.show()
總結(jié)
以上就是在EDA中常用的圖表,可以看到seaborn是可以非常好用的工具,它基于matplotlib但是更加美觀,并且需要編寫的代碼更少,所以在EDA需要簡單的出圖的時候可以優(yōu)先使用它。