Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

seabornのpairplotをカスタムして相関係数を表示する(欠損値にも対応)

More than 1 year has passed since last update.

seabornのpairplotは便利だが、相関係数も確認できればな・・・と思ったので作ってみた。
欠損値があってもwarningが出ないようにしている。

import numpy as np
import pandas as pd
from scipy import stats
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

def draw_hist(x, **kws):
    plt.hist(x[~np.isnan(x)])

def corr_func(x, y, **kws):
    mask = ~np.logical_or(np.isnan(x), np.isnan(y))
    x, y = x[mask], y[mask]
    r, _ = stats.pearsonr(x, y)
    ax = plt.gca()
    ax.annotate("r = {:.3f}".format(r),
               xy=(.2, .5), 
               xycoords=ax.transAxes,
               size=16)

def pairplot(df):
    g = sns.PairGrid(df, height=1.6, dropna=False)
    g.map_diag(draw_hist)
    g.map_upper(sns.regplot, scatter_kws={"s": 8}, line_kws={"color":  "r"})
    g.map_lower(corr_func)
from sklearn.datasets  import load_boston
boston = load_boston()
boston_df = pd.DataFrame(boston["data"],  columns=boston["feature_names"])

pairplot(boston_df.iloc[:,:6])

ちなみに、sns.heatmapでannot=Trueとすると値が表示されることに後から気づいた。まあ散布図と一緒に確認できるので。。

sns.heatmap(boston_df.iloc[:,:6].corr(), annot=True)

kidaufo
農学部→ドイツ→医療系研究者→コンサルでデータサイエンティスト ベイジアン見習い。
http://kibya.hatenablog.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away