Galle, LK--:--5/6 sites live

Seaborn Tutorial in Python

Seaborn දාන්නෙ data scientists හැම දාම group compare කරන්න පාවිච්චි කරන ඒ plots ම — box plots, heatmaps, pair plots — Matplotlib එකට වඩා code අඩුවෙන්.

Seaborn draws the exact plots data scientists use every day to compare groups — box plots, heatmaps, pair plots — with far less code than plain Matplotlib.

5 May 20265 min readSelf Guide · Stage 13#Python & Data Basics#Python Libraries#Machine learning

Lesson 12 of 64 · Part 1 · Python & Data Basics · Python Libraries · bilingual notes (English + සිංහල)

Why this matters. Seaborn draws the exact plots data scientists use every day to compare groups — box plots, heatmaps, pair plots — with far less code than plain Matplotlib.

සිංහලෙන්: Seaborn දාන්නෙ data scientists හැම දාම group compare කරන්න පාවිච්චි කරන ඒ plots ම — box plots, heatmaps, pair plots — Matplotlib එකට වඩා code අඩුවෙන්.

What you'll use this for. A correlation heatmap from Seaborn is often the fastest way to spot which features actually matter to your model before you even train it.

සිංහලෙන්: Model එකක් train කරන්නත් කලින්, ඇත්තටම වැදගත් features මොනවද කියලා ඉක්මනටම දකින්න පුළුවන් හොඳම විදිහ තමයි Seaborn correlation heatmap එක.

Key principles

  • Built for DataFrames, not raw numbers. Pass a whole DataFrame and a column name — Seaborn handles the grouping and colouring for you, which Matplotlib makes you do by hand.
    DataFrames සඳහා හදලා, raw numbers සඳහා නෙවෙයි

Walkthrough

Seaborn:

  • Data Visualization Library

Importing the Libraries

ExamplePython
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

Note : Seaborn has some built-in datasets

ExamplePython
# total bill vs tip dataset
tips = sns.load_dataset('tips')
ExamplePython
tips.head()
total_billtipsexsmokerdaytimesize
016.991.01FemaleNoSunDinner2
110.341.66MaleNoSunDinner3
221.013.50MaleNoSunDinner3
323.683.31MaleNoSunDinner2
424.593.61FemaleNoSunDinner4
ExamplePython
# setting a theme for the plots
sns.set_theme()
ExamplePython
# visualize the data
sns.relplot(data=tips, x ='total_bill',y='tip',col='time',hue='smoker',style='smoker',size='size')

Output

Output
<seaborn.axisgrid.FacetGrid at 0x7f9218a58dd0>

Seaborn Tutorial in Python — figure 1

ExamplePython
# load the iris dataset
iris = sns.load_dataset('iris')
ExamplePython
iris.head()
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa

Scatter Plot

ExamplePython
sns.scatterplot(x='sepal_length',y='petal_length',hue='species',data=iris)

Output

Output
<matplotlib.axes._subplots.AxesSubplot at 0x7f9217c554d0>

Seaborn Tutorial in Python — figure 2

ExamplePython
sns.scatterplot(x='sepal_length',y='petal_width',hue='species',data=iris)

Output

Output
<matplotlib.axes._subplots.AxesSubplot at 0x7f92184ca050>

Seaborn Tutorial in Python — figure 3

ExamplePython
# loading the titanic dataset
titanic = sns.load_dataset('titanic')
ExamplePython
titanic.head()
survivedpclasssexagesibspparchfare…
003male22.0107.2500…
111female38.01071.2833…
213female26.0007.9250…
311female35.01053.1000…
403male35.0008.0500…
ExamplePython
titanic.shape

Output

Output
(891, 15)

Count Plot

ExamplePython
sns.countplot(x='class',data=titanic)

Output

Output
<matplotlib.axes._subplots.AxesSubplot at 0x7f9217b7ad90>

Seaborn Tutorial in Python — figure 4

ExamplePython
sns.countplot(x='survived',data=titanic)

Output

Output
<matplotlib.axes._subplots.AxesSubplot at 0x7f9217af4710>

Seaborn Tutorial in Python — figure 5

Bar Chart

ExamplePython
sns.barplot(x='sex',y='survived',hue='class',data=titanic)

Output

Output
<matplotlib.axes._subplots.AxesSubplot at 0x7f9217a9ed50>

Seaborn Tutorial in Python — figure 6

ExamplePython
# house price dataset
from sklearn.datasets import load_boston
house_boston = load_boston()

house = pd.DataFrame(house_boston.data, columns=house_boston.feature_names)
house['PRICE'] = house_boston.target
ExamplePython
print(house_boston)

Output

Output
{'data': array([[6.3200e-03, 1.8000e+01, 2.3100e+00, ..., 1.5300e+01, 3.9690e+02,
        4.9800e+00],
       [2.7310e-02, 0.0000e+00, 7.0700e+00, ..., 1.7800e+01, 3.9690e+02,
        9.1400e+00],
       [2.7290e-02, 0.0000e+00, 7.0700e+00, ..., 1.7800e+01, 3.9283e+02,
        4.0300e+00],
       ...,
       [6.0760e-02, 0.0000e+00, 1.1930e+01, ..., 2.1000e+01, 3.9690e+02,
        5.6400e+00],
       [1.0959e-01, 0.0000e+00, 1.1930e+01, ..., 2.1000e+01, 3.9345e+02,
        6.4800e+00],
       [4.7410e-02, 0.0000e+00, 1.1930e+01, ..., 2.1000e+01, 3.9690e+02,
        7.8800e+00]]), 'target': array([24. , 21.6, 34.7, 33.4, 36.2, 28.7, 22.9, 27.1, 16.5, 18.9, 15. ,
       18.9, 21.7, 20.4, 18.2, 19.9, 23.1, 17.5, 20.2, 18.2, 13.6, 19.6,
       15.2, 14.5, 15.6, 13.9, 16.6, 14.8, 18.4, 21. , 12.7, 14.5, 13.2,
       13.1, 13.5, 18.9, 20. , 21. , 24.7, 30.8, 34.9, 26.6, 25.3, 24.7,
       21.2, 19.3, 20. , 16.6, 14.4, 19.4, 19.7, 20.5, 25. , 23.4, 18.9,
       35.4, 24.7, 31.6, 23.3, 19.6, 18.7, 16. , 22.2, 25. , 33. , 23.5,
… (41 more lines)
ExamplePython
house.head()
CRIMZNINDUSCHASNOXRMAGE…
00.0063218.02.310.00.5386.57565.2…
10.027310.07.070.00.4696.42178.9…
20.027290.07.070.00.4697.18561.1…
30.032370.02.180.00.4586.99845.8…
40.069050.02.180.00.4587.14754.2…

Distribution Plot

ExamplePython
sns.distplot(house['PRICE'])

Output

Output
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:2557: FutureWarning: `distplot` is a deprecated function and will be removed in a future version
  warnings.warn(msg, FutureWarning)

Output

Output
<matplotlib.axes._subplots.AxesSubplot at 0x7f9210bc1a50>

Seaborn Tutorial in Python — figure 7

Correlation:

  • Positive Correlation
  • Negative Correlation

Heat Map

ExamplePython
correlation = house.corr()
ExamplePython
# constructing a Heat Map
plt.figure(figsize=(10,10))
sns.heatmap(, cbar=True, square=True, fmt='.1f', annot=True, annot_kws={'size':8}, cmap='Blues')

Output

Output
<matplotlib.axes._subplots.AxesSubplot at 0x7f921123f750>

Seaborn Tutorial in Python — figure 8


Keep going: ← Lesson 11 · Lesson 13 → · Track it in the Self Guide