Tutorialに学ぶseabornの使い方④(Visualizing linear relationships)|Pythonによる可視化入門 #8

f:id:lib-arts:20190614213755p:plain

連載の経緯は#1をご確認ください。

#1〜#4まではMatplotlibに関して、#5はseabornチュートリアルの"Visualizing statistical relationships"、#6では"Plotting with categorical data"、#7では"Visualizing the distribution of a dataset"を元に使い方についてまとめました。

Tutorialに学ぶseabornの使い方①(概要&Visualizing statistical relationships)|Pythonによる可視化入門 #5 - lib-arts’s diary

Tutorialに学ぶseabornの使い方②(Plotting with categorical data)|Pythonによる可視化入門 #6 - lib-arts’s diary

Tutorialに学ぶseabornの使い方③(Visualizing the distribution of a dataset)|Pythonによる可視化入門 #7 - lib-arts’s diary
#7では#5、#6、#7に引き続きseabornのチュートリアルから"Visualizing linear relationships"について取り扱います。
以下目次になります。
1. Visualizing linear relationshipsについて
1-1. Functions to draw linear regression models
1-2. Fitting different kinds of models
1-3. Conditioning on other variables
1-4. Controlling the size and shape of the plot
1.5. Plotting a regression in other contexts
2. まとめ


1. Visualizing linear relationshipsについて
1節ではチュートリアルの"Visualizing linear relationships"について取り扱っていきます。

f:id:lib-arts:20190615232755p:plain

Visualizing linear relationships — seaborn 0.9.0 documentation

まずは簡単な概要をつかめればということで、冒頭のみ和訳します。(若干読みにくかったので意訳しました)

Many datasets contain multiple quantitative variables, and the goal of an analysis is often to relate those variables to each other. We previously discussed functions that can accomplish this by showing the joint distribution of two variables. It can be very helpful, though, to use statistical models to estimate a simple relationship between two noisy sets of observations. The functions discussed in this chapter will do so through the common framework of linear regression.

和訳:『多くのデータセットは複数の量的変数を持っており、分析の目的が変数間の関係性を知ることになることはよくあります。前のチュートリアル(#7)で二変数の連結した分布を用いて機能についてディスカッションしました。二つの観測データ間の単純な関係を推定する統計モデルを用いるにあたって非常に役に立つものです。このチャプターでは、線形回帰というよく使われる考え方を用いることについて説明します。』
タイトルからもわかるように、線形回帰(linear regression)の可視化を行ってくれるようです。大体の概要はつかめたので、下記のコードを実行して1-1の内容に入っていきましょう。

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

sns.set(color_codes=True)

tips = sns.load_dataset("tips")


1-1. Functions to draw linear regression models
1-1の"Functions to draw linear regression models"では、線形回帰モデル(linear regression model)の描画を行っていきます。まずは下記を実行してみましょう。

sns.regplot(x="total_bill", y="tip", data=tips);

実行結果は下記のようになります。

f:id:lib-arts:20190616000334p:plain

オーソドックスな線形回帰として、回帰直線が描画されている他に、傾きを表すaの95%信頼区間(CI; confidence interval)についても可視化が行われています。その他のグラフは似通っている印象だったので、1-1はここまでとします。


1-2. Fitting different kinds of models
1-2の"Functions to draw linear regression models"では、多項式近似やロジスティック回帰を用いた描画について取り扱われています。まずは下記を実行してみましょう。

anscombe = sns.load_dataset("anscombe")
sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'II'"),
                ci=None, scatter_kws={"s": 80});

f:id:lib-arts:20190616000928p:plain
上記を見るにうまくフィッティングできていないことがわかります。そこで下記のように二次関数でフィッティングを行います。

sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'II'"),
                order=2, ci=None, scatter_kws={"s": 80});

f:id:lib-arts:20190616001215p:plain
元々のデータが二次関数から生成させたものだったため、フィッティングがうまくいっていることがわかります。
また、同様にロジスティック回帰によるフィッティングも行ってみます。下記を実行してみてください。

tips["big_tip"] = (tips.tip / tips.total_bill) > .15
sns.lmplot(x="total_bill", y="big_tip", data=tips,
                logistic=True, y_jitter=.03);

f:id:lib-arts:20190616001553p:plain

上記のように多項式近似やロジスティック回帰を行うことができました。


1-3. Conditioning on other variables
1-3の"Conditioning on other variables"では、他の変数を用いて条件付けを行なった状況でのグラフの描き分けについてまとめられています。下記がサンプルコードになります。

sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips);

f:id:lib-arts:20190616002048p:plain
実行結果を確認すると、smokerの条件で分けた上でそれぞれ回帰分析を行なっていることがわかります。その他の例も概ね同様のことを行なっているようなので、1-3はここまでとします。


1-4. Controlling the size and shape of the plot
1-4の"Controlling the size and shape of the plot"では、描画するグラフのサイズの設定についてまとめられています。下記がサンプルコードになります。

f, ax = plt.subplots(figsize=(8, 6))
sns.regplot(x="total_bill", y="tip", data=tips, ax=ax);

f:id:lib-arts:20190616002539p:plain

実行結果を確認すると横の方がサイズの大きなグラフになっていることがわかります。figsizeの一つ目の値が横のサイズ、二つ目の値が縦のサイズを表しています。また下記のように複数グラフの描画でもfigsizeは設定することができます。

sns.lmplot(x="total_bill", y="tip", col="day", data=tips,
                col_wrap=2, height=3);

f:id:lib-arts:20190616002825p:plain
上記のように、figsizeを指定することで、グラフのサイズを指定することができます。


1.5. Plotting a regression in other contexts
1-5の"Plotting a regression in other contexts"では、その他の描画の状況において回帰を用いた例になります。下記がサンプルコードになります。

sns.jointplot(x="total_bill", y="tip", data=tips, kind="reg");

f:id:lib-arts:20190616003433p:plain
このように前回までに触れた状況にも回帰直線を入れ込むことができます。


2. まとめ
#8では回帰曲線をグラフに導入するにあたって"Visualizing linear relationships"について取り扱いました。
seabornのチュートリアルについては大体の内容を取り扱えたので、#8をもってseabornについては一区切りとできればと思います。