22
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【1行で】Pandasだけで相関行列をヒートマップっぽく可視化する

Last updated at Posted at 2019-09-20

Pandasだけで相関行列をヒートマップっぽく可視化します。
MatplotlibもSeabornも使いません。

やり方

df.corr().style.background_gradient(axis=None)

具体例

scikit-learnに付属するBoston house-pricesのデータセットを使います。

import pandas as pd
from sklearn import datasets
boston = datasets.load_boston()
df = pd.DataFrame(boston['data'], columns=boston['feature_names'])
df.corr().style.background_gradient(axis=None)

image.png

カラーマップも使えます。

df.corr().style.background_gradient(axis=None, cmap='viridis')

image.png

ただし、カラーバーの範囲が[min, 1]ですので、[-1, 1]にするにはlow=(1 + df.corr().values.min()) / (1 - df.corr().values.min())を指定します。

df.corr().style.background_gradient(axis=None, cmap='viridis', low=(1 + df.corr().values.min()) / (1 - df.corr().values.min()))

image.png

22
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?