#はじめに
Kaggleなどでデータ分析を行う際の探索的データ解析(EDA)の段階で、
自分自身がよく使うデータのビジュアル化、グラフ化に関する手法をまとめました。
今回はmatplotlibのラッパー、seabornをメインで活用していきます。
参考: https://seaborn.pydata.org/index.html
#各グラフの実装
###■インストール/ライブラリの読み込み
#未インストールの方は
pip install seaborn
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
%matplotlib inline
###■データの読み込み
データはKaggleのHousePriceのTrainデータを使用します
https://www.kaggle.com/c/house-prices-advanced-regression-techniques
df = pd.read_csv('House Price/train.csv')
df.head()
![Screen Shot 2019-02-10 at 21.05.44.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F278569%2F92a18a5a-a24a-76bf-7ce5-634c568272d3.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=db819be14bc27fc9d711b79a09026ac7)
81列のデータで、ここでは確認できませんが最終列の SalePrice が目的変数データです。
###■棒グラフの活用
項目ごとのデータの数量を把握する際に使います。
- sns.countplot(x='列名', data = データ名)
sns.countplot(x='YrSold', data = df);
![Screen Shot 2019-02-10 at 19.29.39.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F278569%2F18d033a5-ed03-5c60-c3b7-4299190e99c4.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=7fbd54af86e913220f9dee44c99710ff)
###■ヒストグラムの活用
データの分布を把握したい際に利用します。
- sns.distplot(データ名.列名);
sns.distplot(df.YearBuilt);
![Screen Shot 2019-02-10 at 19.30.57.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F278569%2Fb307093c-622f-8e56-db63-7701e8a48b77.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=481af660f0d671589a98640b9b8a3ace)
カーネル密度推定(KDE)も加えて出力してくれます。
###■散布図の活用
散布図を活用し、2つの変数間の相関関係を可視化します。
- データ名.plot(kind='scatter', x='列名1', y='列名2')
df.plot(kind='scatter', x='LotFrontage', y='SalePrice');
![Screen Shot 2019-02-10 at 19.32.11.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F278569%2F4badf4e1-405b-71e5-f2e1-a3d9b564d843.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=5a4fefbd0380f5b057d50207d5219acf)
- sns.regplot(x=データ名.列名1, y=データ名.列名2)
こちらは線形回帰直線を含むタイプです。
sns.regplot(x=df.LotFrontage, y=df.SalePrice);
![Screen Shot 2019-02-10 at 19.33.17.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F278569%2F91c41290-d150-c258-eb5c-d178915be4bb.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=42d86d131b981432c0c20239b6bb9bf8)
正の相関があり、LotFrontageが上がればSalePriceも上がることが分かります。
###■散布図の応用
複数散布図をまとめて表示
目的変数(SalePrice)と他の数値データの相関関係をまとめて表示します。
最大表示可能数は30なので、変数が30を超える場合は全て表示できません。
#数値データの列のみに絞る
df_n = df.select_dtypes(include=[np.number])
#グラフを作る
fig = plt.figure(figsize=(14,9))
for i in np.arange(30): #30が最大
ax = fig.add_subplot(5,6,i+1)
sns.regplot(x=df_n.iloc[:,i], y=df_n.SalePrice)
#グラフを整えて表示
plt.tight_layout()
plt.show()
![Screen Shot 2019-02-10 at 19.34.16.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F278569%2Fed90ee59-7439-08fb-4a8d-67bb9d932d8f.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=0e6fbf36c0c67f64624eb08cc0ca55d1)
###■ボックスプロット(箱ひげ図)の活用
ボックスプロットを使い、オブジェクトデータと他の変数の関係性を視覚化します。
sns.boxplot(x="列名1", y="列名2", data=データ名)
sns.boxplot(x="SaleCondition", y="SalePrice", data=df);
![Screen Shot 2019-02-10 at 19.35.24.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F278569%2F83412e8c-6185-3794-b8d6-348b2e965036.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=a474e59e41b7f8bf7a52c6f8cc7d04ea)
###■ボックスプロットの応用
全てのオブジェクトデータと目的変数(SalePrice)の関係性をまとめて表示
dfの部分にデータ名、SalePriceの部分に目的変数を入力し直すと他のデータでも使えます。
categorical_features = df.select_dtypes(include=[np.object])
for c in categorical_features:
df[c] = df[c].astype('category')
if df[c].isnull().any():
df[c] = df[c].cat.add_categories(['MISSING'])
df[c] = df[c].fillna('MISSING')
def boxplot(x, y, **kwargs):
sns.boxplot(x=x, y=y)
x=plt.xticks(rotation=90)
f = pd.melt(df, id_vars=['SalePrice'], value_vars=categorical_features)
g = sns.FacetGrid(f, col="variable", col_wrap=2, sharex=False, sharey=False, size=5)
g = g.map(boxplot, "value", "SalePrice")
出力グラフの一部を表示
(実際には全てのオブジェクト変数のボックスプロットが表示されます)
![Screen Shot 2019-02-10 at 19.36.09.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F278569%2Fcd2ace26-8cd1-1f59-a933-de8a9463b12a.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=a92782617f27fcdcfa3cb29599530dc1)
###■ヒートマップの活用
全ての数値データ同士の相関関係をヒートマップで表示
plt.figure(figsize=(13, 11))
sns.heatmap(df.corr())
plt.tight_layout();
![Screen Shot 2019-02-10 at 19.37.45.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F278569%2F29cc4363-4783-0996-4e7b-cd97a4a5b980.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=7abbe71c087afc22cba2c29ecec99365)
###■ヒートマップの応用
目的変数列(SalePrice)と相関度の高い列ベスト10をヒートマップで表示
どの変数が重要かがすぐに理解でき、変数を絞る際に有効です。
cols = df.corr().nlargest(10,'SalePrice')['SalePrice'].index
cm = np.corrcoef(df[cols].values.T)
plt.subplots(figsize = (12,10))
sns.heatmap(cm, vmax=.8, linewidths=0.01, annot=True, cmap='viridis',
xticklabels=cols.values, yticklabels=cols.values, annot_kws={'size':14});
![Screen Shot 2019-02-10 at 18.42.54.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F278569%2Ff1ce035d-f5ef-afdd-d40e-f91ad5ec6c69.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=a83611ccf06aa70c734d887ec0a94811)
###■ペアプロットの活用
ペアプロットで指定の数値データ列の相関関係をまとめて表示
cols = ['SalePrice', '1stFlrSF', 'PoolArea', 'OverallQual'] #任意の列を指定、さらに増やしてもよい
sns.pairplot(df[cols], size=3)
plt.tight_layout();
![Screen Shot 2019-02-10 at 19.13.44.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F278569%2F2392c641-a366-45d4-0bb7-3c23b996a9f9.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=27a55c4241b47621e5e92119b18e881b)
まとめ
分析の際によく使うデータビジュアリゼーションの手法を列記しました。
変数間の関係性を可視化し理解することで、データ分析の精度をさらに高めることが可能です。
また、seabornは視認性の高さもさることながら、背景色や色のトーン等非常にセンスがよく分析を楽しくしてくれます。
参考
Matplotlib&Seaborn実装ハンドブック(秀和システム)
The Python Graph Gallery : https://python-graph-gallery.com/
データ構造を把握した後の、欠損値処理のまとめ記事もよろしければご覧ください。
https://qiita.com/ryo111/items/4177c732cc9801bccb17