Seaborn を使ったカテゴリカルプロット入門

Pythonでカテゴリカルデータを扱う

Kasey Jones

Research Data Scientist

3 つ目のデータセット

  • 名称: Las Vegas TripAdvisor Reviews - reviews
  • 行数: 504
  • 列数: 20
Pythonでカテゴリカルデータを扱う

ラスベガスのレビュー

reviews.info()
RangeIndex: 504 entries, 0 to 503
Data columns (total 20 columns):
 #   Column             Non-Null Count  Dtype 
     ------             --------------  ----- 
 0   User country       504 non-null    object
...
 6   Traveler type      504 non-null    object
 7   Pool               504 non-null    object
 8   Gym                504 non-null    object
 9   Tennis court       504 non-null    object
...
dtypes: int64(7), object(13)
memory usage: 78.9+ KB
1 https://www.kaggle.com/crawford/las-vegas-tripadvisor-reviews
Pythonでカテゴリカルデータを扱う

Seaborn

カテゴリカルプロット:

import seaborn as sns
import matplotlib.pyplot as plt

sns.catplot(...)
plt.show()
Pythonでカテゴリカルデータを扱う

catplot 関数

パラメータ:

  • x: data 内の変数名
  • y: data 内の変数名
  • data: DataFrame
  • kind: 作成するプロットの種類。"strip", "swarm", "box", "violin", "boxen", "point", "bar", "count" のいずれか
Pythonでカテゴリカルデータを扱う

箱ひげ図

Pythonでカテゴリカルデータを扱う

レビューのスコア

reviews["Score"].value_counts()
5    227
4    164
3     72
2     30
1     11
Pythonでカテゴリカルデータを扱う

箱ひげ図の例

sns.catplot(
  x="Pool",
  y="Score",
  data=reviews,
  kind="box"
)
plt.show()

Pythonでカテゴリカルデータを扱う

手早い設定 2 つ

# フォントサイズと背景の設定
sns.set(font_scale=1.4)

sns.set_style("whitegrid")
sns.catplot(
  x="Pool",
  y="Score",
  data=reviews,
  kind="box"
)
plt.show()

Pythonでカテゴリカルデータを扱う

箱ひげ図の練習

Pythonでカテゴリカルデータを扱う

Preparing Video For Download...