บทนำสู่ categorical plots ด้วย Seaborn

การทำงานกับข้อมูลเชิงกลุ่มใน Python

Kasey Jones

Research Data Scientist

ชุดข้อมูลที่สาม

  • ชื่อ: Las Vegas TripAdvisor Reviews - reviews
  • จำนวนแถว: 504
  • จำนวนคอลัมน์: 20
การทำงานกับข้อมูลเชิงกลุ่มใน Python

รีวิว Las Vegas

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

Categorical plots:

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

Box plot

การทำงานกับข้อมูลเชิงกลุ่มใน Python

คะแนนรีวิว

reviews["Score"].value_counts()
5    227
4    164
3     72
2     30
1     11
การทำงานกับข้อมูลเชิงกลุ่มใน Python

ตัวอย่าง Box plot

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

การทำงานกับข้อมูลเชิงกลุ่มใน Python

ตัวเลือกเพิ่มเติมสองอย่าง

# Setting font size and plot background
sns.set(font_scale=1.4)

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

การทำงานกับข้อมูลเชิงกลุ่มใน Python

ฝึกใช้ Boxplot

การทำงานกับข้อมูลเชิงกลุ่มใน Python

Preparing Video For Download...