from sklearn import datasets
import matplotlib.pyplot as plt
= datasets.load_iris()
iris = plt.subplots()
_, ax = ax.scatter(iris.data[:, 0], iris.data[:, 1], c=iris.target)
scatter set(xlabel=iris.feature_names[0], ylabel=iris.feature_names[1])
ax.= ax.legend(scatter.legend_elements()[0], iris.target_names, loc="lower right", title="Classes")
_ 2.0, 4.5)
ax.set_ylim( plt.show()
Module 1
Introduction and Background
Class Activities
Week 1
Example: Iris Dataset Visualization
This example demonstrates how to visualize the Iris dataset using a scatter plot. The Iris dataset is a classic dataset in machine learning, containing measurements of iris flowers from three different species. The example is adapted from the sklearn documentation.
Separating the setosa class with other classes using a separator line. Below is an example of adding the divider to the scatter plot.
from sklearn import datasets
import numpy as np
import matplotlib.pyplot as plt
= datasets.load_iris()
iris = plt.subplots()
_, ax = ax.scatter(iris.data[:, 0], iris.data[:, 1], c=iris.target)
scatter set(xlabel=iris.feature_names[0], ylabel=iris.feature_names[1])
ax.= ax.legend(scatter.legend_elements()[0], iris.target_names, loc="lower right", title="Classes")
_ # Adding a separator line
= 1 # slope
m = 2.3 # intercept
b
# x range based on current plot limits
= np.linspace(*ax.get_xlim(), 100)
x_vals = m * x_vals - b
y_vals
'r--', label="Separator: y = x - {:.1f}".format(b))
ax.plot(x_vals, y_vals,
ax.legend()
2.0, 4.5)
ax.set_ylim(
plt.show()
Weekly Step Count Analysis
Our goal is to identify the most active walking days of each week. The code below shows how to visualize the step counts for two weeks using basic data visualization techniques (line chart and bar chart). This example is adapted from The Python Coding Book.
import matplotlib.pyplot as plt
= ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
days = [8934, 14902, 3409, 25672, 12300, 2023, 6890]
steps_walked = [9788, 8710, 5308, 17630, 21309, 4002, 5223]
steps_last_week
= plt.subplots(1, 2, figsize=(12, 5))
fig, axs
# Plot line chart
0].plot(days, steps_walked, "o-g")
axs[0].plot(days, steps_last_week, "v--m")
axs[0].set_title("Step count | This week (green) and last week (magenta)")
axs[0].set_xlabel("Days of the week")
axs[0].set_ylabel("Steps walked")
axs[0].grid(True)
axs[
# Plot bar chart
= [-0.2, 0.8, 1.8, 2.8, 3.8, 4.8, 5.8]
x_range_current = [0.2, 1.2, 2.2, 3.2, 4.2, 5.2, 6.2]
x_range_previous
1].bar(x_range_current, steps_walked)
axs[1].bar(x_range_previous, steps_last_week)
axs[1].set_title("Step count | This week (blue) and last week (orange)")
axs[1].set_xlabel("Days of the week")
axs[1].set_ylabel("Steps walked")
axs[1].grid(True)
axs[
plt.show()