💮 The Great Iris Festival (Solution)¶
Scenario
In the enchanted land of Seabornea, flowers were not just pretty sights to admire but were sentient beings with aspirations, desires, and a knack for a little friendly competition. Every year, they organized the Great Iris Festival, where different Iris flowers showcased their unique talents.
This year, the festival was abuzz with excitement. The three legendary species of the Iris clan - Setosa, Versicolor, and Virginica - were to participate in a series of contests that would finally decide which species was the most illustrious of them all!
Exercise
Explore the Iris dataset and learn about the distributions and relationships of its features using Seaborn to help the judges decide which species is the most talented of them all!
import seaborn as sns
import matplotlib.pyplot as plt
iris_df = sns.load_dataset('iris')
Task 1: Distribution of Sepal Length
- Create a histogram of the
sepal_length
column usingsns.histplot
. - Color the histogram using the
species
column. - Customize the appearance (color, bin size, etc.).
# Histogram for sepal_length
sns.histplot(data=iris_df, x='sepal_length', bins=30, hue="species", palette="deep")
# Titles and labels
plt.title('Distribution of Sepal Lengths')
plt.xlabel('Sepal Length (cm)')
plt.ylabel('Count')
plt.show()
Task 2: Compare Sepal Width and Length
- Use
sns.scatterplot
to comparesepal_width
andsepal_length
. - Color the points based on the species.
- Add a title and labels for clarity.
# Scatter plot comparing sepal_width and sepal_length
sns.scatterplot(data=iris_df, x='sepal_length', y='sepal_width', hue='species')
# Titles and labels
plt.title('Comparison of Sepal Width and Sepal Length')
plt.xlabel('Sepal Length (cm)')
plt.ylabel('Sepal Width (cm)')
plt.show()
Task 3: Pairwise Relationships
- Use
sns.pairplot
to visualize pairwise relationships between all numerical columns. - Color the points based on species.
- Do you observe any clusters? Which features separate the species the best?
# Pairplot for all numerical columns
sns.pairplot(iris_df, hue='species')
plt.suptitle('Pairwise Relationships by Species', y=1.02)
plt.show()
/home/vscode/.pyenv/versions/3.9.17/lib/python3.9/site-packages/seaborn/axisgrid.py:118: UserWarning: The figure layout has changed to tight self._figure.tight_layout(*args, **kwargs)
*Observation: From the pair plot, it's evident that the setosa species is distinct from the other two, especially when considering petal width and length. There are clusters formed which separate the species, especially in the petal measurements.*
Task 4: Boxplots for Comparing Features
- Use
sns.boxplot
to showcase the distribution ofpetal_length
across different species. - Observe which species has the widest range of
petal_length
and which one has the smallest.
# Boxplot for petal_length across species
sns.boxplot(data=iris_df, x='species', y='petal_length', palette='pastel')
# Titles and labels
plt.title('Distribution of Petal Length across Species')
plt.xlabel('Species')
plt.ylabel('Petal Length (cm)')
plt.show()
*Observation: The setosa species has the smallest petal length range, while virginica has the widest.*
Task 5: Violin Plot Exploration
- Create a
sns.violinplot
comparing the distribution ofpetal_width
across different species. - Contrast this with the box plot. What additional information can you derive from the violin plot?
# Violin plot for petal_width across species
sns.violinplot(data=iris_df, x='species', y='petal_width', palette='pastel')
# Titles and labels
plt.title('Distribution of Petal Width across Species')
plt.xlabel('Species')
plt.ylabel('Petal Width (cm)')
plt.show()
*Observation: The violin plot combines the aspects of a box plot with a kernel density estimation. From this, we can see that setosa has a very narrow distribution for petal width, while versicolor and virginica have wider distributions. The thickness of the violin plot provides an indication of the density of the data.*
๐ By the end of the festival, the entire land of Seabornea was in joyous spirits. It was not about deciding which species was the best, but celebrating the unique qualities each one brought to the table. And as the sun set on Seabornea, the Irises, having put on a grand show, went back to their meadows, eagerly awaiting next year's festival.๐