Python視覺化:Seaborn(一)

科賽網Kesci發表於2017-10-10

Python視覺化:Seaborn(一)

進行資料分析&挖掘時,描述性統計必不可少。比如,我們需要看下各個quantitative變數的分佈情況,良好的分佈視覺化效果能為之後進一步做資料建模打下基礎。

其中,Seaborn便是個功能強大的庫,可以用它做出很棒的資料視覺化效果。我們此處結合科賽網上公開的鏈家二手房資料集,對如何使用Seaborn做Distribution Visualization進行說明。

說明:文中所有程式碼部分均可通過K-Lab線上資料分析協作工具復現。可以登入科賽網,嘗試用不同的資料集利用Seaborn進行視覺化練習。


對於quantitative變數做分佈視覺化,主要有兩點:

  • 探尋變數自身的分佈規律,也就是univariate distributions視覺化;

  • 探尋兩個變數之間是否有分佈關係,也就是bivariate distributions視覺化。

Seaborn也是按這個workflow給出了plot function。

univariate distributions visualization:

  • distplot --- 繪製某單一變數的分佈情況

  • kdeplot --- fit某變數(單一變數或兩個變數之間)分佈的核密度估計(kernel density estimate)

  • rugplot --- 在座標軸上按戳的樣式(sticks)依次繪製資料點序列

bivariate distributions visualization:

  • jointplot --- 繪製某兩個變數之間的分佈關係


讀取資料

 import pandas as pd sh = pd.read_csv('sh.csv',encoding='gbk')

Python視覺化:Seaborn(一)

為了避免中文解碼出現bug,將表頭進行替換:

Python視覺化:Seaborn(一)

匯入繪圖的包

import warnings warnings.filterwarnings("ignore") import seaborn as sns import matplotlib.pyplot as plt %matplotlib inline


單一變數視覺化初探

在這個資料集中,quantitative的變數主要有房屋的面積Area,每平米單價Price,以及房屋總價Tprice。

先來看看上海每個行政區房屋總價Tprice的分佈情況,我們用distplot繪製需要注意的是,在預設情況下,distplot會直接給出變數核密度估計的fit曲線。

dist = sh.Dist.unique() plt.figure(1,figsize=(16,30))with sns.axes_style("ticks"):

for i in range(17): temp = sh[sh.Dist == dist[i]] plt.subplot(6,3,i+1) plt.title(dist[i]) sns.distplot(temp.Tprice) plt.xlabel(' ') plt.show()

Python視覺化:Seaborn(一)

當然,我們也可以關閉核密度估計fit曲線,直接去看直方圖分佈(histograms)。Seaborn在distplot function的API中給出了kde和rug這兩個引數,分別對應kernel density和rugplot(也就是在座標軸上繪製出datapoint所在的位置)。


我們單獨取出徐彙區(Xuhui)的資料,對kde和rug這兩個引數進行設定,做出的直方圖如下。

temp = sh[sh.Dist == 'Xuhui'] plt.figure(1,figsize=(6,6)) plt.title('Xuhui') sns.distplot(temp.Tprice,kde=False,bins=20,rug=True) plt.xlabel(' ') plt.show()

Python視覺化:Seaborn(一)

在Seaborn中,我們也可以直接呼叫kdeplot和rugplot做圖


現在我們去研究一下徐彙區資料中,房屋面積變數Area的分佈情況。

from scipy import stats, integrate plt.figure(1,figsize=(12,6))with sns.axes_style("ticks"): plt.subplot(1,2,1) sns.kdeplot(temp.Area,shade=True) sns.rugplot(temp.Area) plt.title('Xuhui --- Area Distribution') plt.subplot(1,2,2) plt.title('Xuhui - Area Distribution fits with gamma distribution') sns.distplot(temp.Area, kde=False, fit=stats.gamma) plt.show()

Python視覺化:Seaborn(一)

左:kdeplot function和rugplot function分別呼叫後的疊加,體現Seaborn做圖靈活性
右:在distplot function設定了fit引數,讓資料的分佈與gamma分佈進行擬合


兩個變數(pairs)視覺化

在做了單個quantitative變數分佈的視覺化研究後,我們來看看某兩個變數組之間是否存在分佈關係。

Seaborn在這裡提供了jointplot function使用。 下面我們來對整個資料集的房屋面積(Area)和房價(Tprice)這兩個變數進行視覺化分析。


繪製散點圖Scatterplot

sns.jointplot(x='Area',y='Tprice',data=sh) plt.show()

Python視覺化:Seaborn(一)

我們發現房價小於1000W並且面積小於200平方米的資料點很集中。設定一個filter,將這部分資料單獨拿出來做研究,重新繪製散點圖。

test = sh[(sh.Tprice<1000)&(sh.Area<200)]with sns.axes_style("white"): sns.jointplot(x='Area',y='Tprice',data=test) plt.show()

Python視覺化:Seaborn(一)

當資料量很大的時候,可以進一步利用hexbin plot去做視覺化,顯示資料集中分佈的區域,如下圖所示。

with sns.axes_style("white"): sns.jointplot(x='Tprice',y='Area',data=test,kind='hex') plt.show()

Python視覺化:Seaborn(一)

當然,我們也可以用kernel density estimation去做視覺化,看分佈情況。

with sns.axes_style("white"): sns.jointplot(x='Area',y='Tprice',data=test,kind='kde') plt.show()

Python視覺化:Seaborn(一)

小結

seaborn的巧妙之處就是利用最短的程式碼去視覺化儘可能多的內容,而且API十分靈活,只有你想不到,沒有你做不到。

另外,這篇小短文對資料集本身的探索與解釋不是很多,若希望更深層次的探索資料集,可以直接登入科賽網,點選「資料集」檢視。


本文由 保一雄@科賽網 資料分析師 原創。


相關文章