inclass_assignment_6
September 18, 2025
In-Class Assignment 6: Data Visualization
Data Exploration (yes, it overlaps with data cleaning & data visualization)
1. Load the data
2. Shape
3. Summary statistics
4. Species distribution
5. Pairplot
6. Boxplot
7. Correlation Heatmap
8. Histogram
[4]: # QUESTION 1
# Import pandas, numpy, seaborn as sns, [Link]
import pandas as pd
import numpy as np
import seaborn as sns
import [Link] as plt
# Load tips dataset
data=sns.load_dataset("tips")
[31]: # QUESTION 2
# take a look at the columns up to 5 rows
print([Link]())
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
[29]: # QUESTION 3
# Explore the data, get counts, idea of how many data points, shape
1
print([Link]())
print("Shape of the dataset:", [Link])
print("Columns:", [Link]())
[Link]()
print([Link]())
print("Missing values:\n", [Link]().sum())
print("Unique values:\n", [Link]())
print("Value counts for 'time':\n", df['time'].value_counts())
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
Shape of the dataset: (244, 7)
Columns: ['total_bill', 'tip', 'sex', 'smoker', 'day', 'time', 'size']
<class '[Link]'>
RangeIndex: 244 entries, 0 to 243
Data columns (total 7 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 total_bill 244 non-null float64
1 tip 244 non-null float64
2 sex 244 non-null category
3 smoker 244 non-null category
4 day 244 non-null category
5 time 244 non-null object
6 size 244 non-null int64
dtypes: category(3), float64(2), int64(1), object(1)
memory usage: 8.9+ KB
total_bill tip size
count 244.000000 244.000000 244.000000
mean 19.785943 2.998279 2.569672
std 8.902412 1.383638 0.951100
min 3.070000 1.000000 1.000000
25% 13.347500 2.000000 2.000000
50% 17.795000 2.900000 2.000000
75% 24.127500 3.562500 3.000000
max 50.810000 10.000000 6.000000
2
Missing values:
total_bill 0
tip 0
sex 0
smoker 0
day 0
time 0
size 0
dtype: int64
Unique values:
total_bill 229
tip 123
sex 2
smoker 2
day 4
time 2
size 6
dtype: int64
Value counts for 'time':
time
Dinner 176
Lunch 68
Name: count, dtype: int64
[14]: # QUESTION 4
# Summary of the columns and data type using info()
[Link]()
<class '[Link]'>
RangeIndex: 244 entries, 0 to 243
Data columns (total 7 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 total_bill 244 non-null float64
1 tip 244 non-null float64
2 sex 244 non-null category
3 smoker 244 non-null category
4 day 244 non-null category
5 time 244 non-null object
6 size 244 non-null int64
dtypes: category(3), float64(2), int64(1), object(1)
memory usage: 8.9+ KB
[25]: # QUESTION 5
# Summary stats using describe function
[Link](include='all')
3
[25]: total_bill tip sex smoker day time size
count 244.000000 244.000000 244 244 244 244 244.000000
unique NaN NaN 2 2 4 2 NaN
top NaN NaN Male No Sat Dinner NaN
freq NaN NaN 157 151 87 176 NaN
mean 19.785943 2.998279 NaN NaN NaN NaN 2.569672
std 8.902412 1.383638 NaN NaN NaN NaN 0.951100
min 3.070000 1.000000 NaN NaN NaN NaN 1.000000
25% 13.347500 2.000000 NaN NaN NaN NaN 2.000000
50% 17.795000 2.900000 NaN NaN NaN NaN 2.000000
75% 24.127500 3.562500 NaN NaN NaN NaN 3.000000
max 50.810000 10.000000 NaN NaN NaN NaN 6.000000
[39]: # QUESTION 4
# determine the size distribution
def graph(y):
[Link](x="species", y=y, data=iris)
[Link](figsize=(8,8))
# Adding the subplot at the specified
# grid position
[Link](221)
graph('sepal_length')
[Link](222)
graph('sepal_width')
[Link](223)
graph('petal_length')
[Link](224)
graph('petal_width')
[Link]()
4
[11]: # QUESTION 5
# create a pairplot for pairwise relationships and distributions for the column␣
↪'time':
df = sns.load_dataset("tips")
df['time'] = df['time'].astype(str)
[Link](df, hue='time', diag_kind='kde', corner=True, palette='Set2')
[Link]('Pairplot Grouped by Time', y=1.02, fontsize=16)
[Link]()
5
[10]: # QUESTION 6
# Boxplot for each feature by 'time'
df = sns.load_dataset("tips")
df['time'] = df['time'].astype(str)
numeric_features = df.select_dtypes(include='number').columns
[Link](style="whitegrid")
for feature in numeric_features:
[Link](figsize=(8, 5))
[Link](data=df, x='time', y=feature, palette="Set2")
[Link](f'Boxplot of {feature} by Time')
[Link]('Time')
[Link](feature)
plt.tight_layout()
6
/tmp/[Link]: FutureWarning:
Passing `palette` without assigning `hue` is deprecated and will be removed in
v0.14.0. Assign the `x` variable to `hue` and set `legend=False` for the same
effect.
[Link](data=df, x='time', y=feature, palette="Set2")
/tmp/[Link]: FutureWarning:
Passing `palette` without assigning `hue` is deprecated and will be removed in
v0.14.0. Assign the `x` variable to `hue` and set `legend=False` for the same
effect.
[Link](data=df, x='time', y=feature, palette="Set2")
/tmp/[Link]: FutureWarning:
Passing `palette` without assigning `hue` is deprecated and will be removed in
v0.14.0. Assign the `x` variable to `hue` and set `legend=False` for the same
effect.
[Link](data=df, x='time', y=feature, palette="Set2")
7
[9]: # QUESTION 7
# Correlation heatmap
8
corr = [Link](numeric_only=True)
[Link](figsize=(10, 8))
[Link](corr,
annot=True,
fmt=".2f",
cmap='coolwarm',
square=True,
linewidths=0.5,
cbar_kws={"shrink": .8})
[Link]("Correlation Heatmap", fontsize=16)
[Link]()
9
[ ]: import pandas as pd
import numpy as np
[ ]: iris = pd.read_csv('[Link]
↪master/[Link]')
[Link]()
[ ]: sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
[ ]: import seaborn as sns
import [Link] as plt
# Countplot of species: x='species', data=iris
[Link](x = 'species', data=iris,)
[Link]('Species Distribution')
[Link]()
10
[ ]: [Link](x='sepal_length', y='sepal_width',
hue='species', data=iris, )
# Placing Legend outside the Figure
[Link](bbox_to_anchor=(1, 1), loc=2)
[Link]()
From the above plot, we can infer that
• Species Setosa has smaller sepal lengths but larger sepal widths.
• Versicolor Species lies in the middle of the other two species in terms of sepal length and
width
• Species Virginica has larger sepal lengths but smaller sepal widths.
[ ]: [Link](iris, hue='species')
#[Link]()
[ ]: <[Link] at 0x790f11b98dd0>
11
We can see many types of relationships from this plot such as the species Setosa has the smallest
of petals widths and lengths. It also has the smallest sepal length but larger sepal widths.
[ ]: fig, axes = [Link](2, 2, figsize=(10,10))
axes[0,0].set_title("Sepal Length")
axes[0,0].hist(iris['sepal_length'], bins=7)
axes[0,1].set_title("Sepal Width")
axes[0,1].hist(iris['sepal_width'], bins=5);
axes[1,0].set_title("Petal Length")
axes[1,0].hist(iris['petal_length'], bins=6);
axes[1,1].set_title("Petal Width")
axes[1,1].hist(iris['petal_width'], bins=6);
12
From the above plot, we can see that –
• The highest frequency of the sepal length is between 30 and 35 which is between 5.5 and 6
• The highest frequency of the sepal Width is around 70 which is between 3.0 and 3.5
• The highest frequency of the petal length is around 50 which is between 1 and 2
• The highest frequency of the petal width is between 40 and 50 which is between 0.0 and 0.5
[ ]: def graph(y):
[Link](x="species", y=y, data=iris)
[Link](figsize=(10,10))
# Adding the subplot at the specified
13
# grid position
[Link](221)
graph('sepal_length')
[Link](222)
graph('sepal_width')
[Link](223)
graph('petal_length')
[Link](224)
graph('petal_width')
[Link]()
14
From the above graph, we can see that –
• Species Setosa has the smallest features and less distributed with some outliers.
• Species Versicolor has the average features.
• Species Virginica has the highest features
[ ]: plot = [Link](iris, hue="species")
[Link]([Link], "sepal_length").add_legend()
plot = [Link](iris, hue="species")
[Link]([Link], "sepal_width").add_legend()
plot = [Link](iris, hue="species")
[Link]([Link], "petal_length").add_legend()
plot = [Link](iris, hue="species")
[Link]([Link], "petal_width").add_legend()
[Link]()
/usr/local/lib/python3.11/dist-packages/seaborn/[Link]: UserWarning:
`distplot` is a deprecated function and will be removed in seaborn v0.14.0.
Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).
For a guide to updating your code to use the new functions, please see
[Link]
func(*plot_args, **plot_kwargs)
/usr/local/lib/python3.11/dist-packages/seaborn/[Link]: UserWarning:
`distplot` is a deprecated function and will be removed in seaborn v0.14.0.
Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).
For a guide to updating your code to use the new functions, please see
[Link]
func(*plot_args, **plot_kwargs)
/usr/local/lib/python3.11/dist-packages/seaborn/[Link]: UserWarning:
`distplot` is a deprecated function and will be removed in seaborn v0.14.0.
Please adapt your code to use either `displot` (a figure-level function with
15
similar flexibility) or `histplot` (an axes-level function for histograms).
For a guide to updating your code to use the new functions, please see
[Link]
func(*plot_args, **plot_kwargs)
/usr/local/lib/python3.11/dist-packages/seaborn/[Link]: UserWarning:
`distplot` is a deprecated function and will be removed in seaborn v0.14.0.
Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).
For a guide to updating your code to use the new functions, please see
[Link]
func(*plot_args, **plot_kwargs)
/usr/local/lib/python3.11/dist-packages/seaborn/[Link]: UserWarning:
`distplot` is a deprecated function and will be removed in seaborn v0.14.0.
Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).
For a guide to updating your code to use the new functions, please see
[Link]
func(*plot_args, **plot_kwargs)
/usr/local/lib/python3.11/dist-packages/seaborn/[Link]: UserWarning:
`distplot` is a deprecated function and will be removed in seaborn v0.14.0.
Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).
For a guide to updating your code to use the new functions, please see
[Link]
func(*plot_args, **plot_kwargs)
/usr/local/lib/python3.11/dist-packages/seaborn/[Link]: UserWarning:
`distplot` is a deprecated function and will be removed in seaborn v0.14.0.
Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).
For a guide to updating your code to use the new functions, please see
[Link]
16
func(*plot_args, **plot_kwargs)
/usr/local/lib/python3.11/dist-packages/seaborn/[Link]: UserWarning:
`distplot` is a deprecated function and will be removed in seaborn v0.14.0.
Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).
For a guide to updating your code to use the new functions, please see
[Link]
func(*plot_args, **plot_kwargs)
/usr/local/lib/python3.11/dist-packages/seaborn/[Link]: UserWarning:
`distplot` is a deprecated function and will be removed in seaborn v0.14.0.
Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).
For a guide to updating your code to use the new functions, please see
[Link]
func(*plot_args, **plot_kwargs)
/usr/local/lib/python3.11/dist-packages/seaborn/[Link]: UserWarning:
`distplot` is a deprecated function and will be removed in seaborn v0.14.0.
Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).
For a guide to updating your code to use the new functions, please see
[Link]
func(*plot_args, **plot_kwargs)
/usr/local/lib/python3.11/dist-packages/seaborn/[Link]: UserWarning:
`distplot` is a deprecated function and will be removed in seaborn v0.14.0.
Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).
For a guide to updating your code to use the new functions, please see
[Link]
func(*plot_args, **plot_kwargs)
/usr/local/lib/python3.11/dist-packages/seaborn/[Link]: UserWarning:
17
`distplot` is a deprecated function and will be removed in seaborn v0.14.0.
Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).
For a guide to updating your code to use the new functions, please see
[Link]
func(*plot_args, **plot_kwargs)
18
From the above plots, we can see that –
• In the case of Sepal Length, there is a huge amount of overlapping.
• In the case of Sepal Width also, there is a huge amount of overlapping.
• In the case of Petal Length, there is a very little amount of overlapping.
• In the case of Petal Width also, there is a very little amount of overlapping.
So we can use Petal Length and Petal Width as the classification feature.
[ ]: iris.select_dtypes(include=['number']).corr(method='pearson')
19
# Note that using iris.select_dtypes(include=['number']) we separated the␣
↪'species' column as it does not contain number and to calculate correlation␣
↪we need numbers.
[ ]: sepal_length sepal_width petal_length petal_width
sepal_length 1.000000 -0.117570 0.871754 0.817941
sepal_width -0.117570 1.000000 -0.428440 -0.366126
petal_length 0.871754 -0.428440 1.000000 0.962865
petal_width 0.817941 -0.366126 0.962865 1.000000
[ ]: correlation_matrix = iris.select_dtypes(include=['number']).corr()
[Link](correlation_matrix, annot=True, cmap='coolwarm')
[Link]('Correlation Heatmap')
[Link]()
20