What is visualization?
- Machine Learning course
- Beginner
- Lesson 103/112
- 5 min
Data visualization aims to make data understandable with charts.
What is visualisation?
Tables of numbers hide patterns; plots show them. In ML, visualisation serves three jobs: exploring data (distribution, outliers), understanding the model (errors, learning curves) and communicating the result.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6, 4))
ax.plot([1, 2, 3], [2, 4, 3])
ax.set_title("İlk grafik")
ax.set_xlabel("x")
ax.set_ylabel("y")
fig.savefig("ilk.png")plt.subplots returns a figure (fig) and an axis (ax); drawing goes on the axis, saving on the figure.
Anscombe's lesson
Four datasets with the same mean, variance and correlation look completely different when plotted (Anscombe's quartet). Summary statistics alone aren't enough:
x = np.array([10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5])
y1 = np.array([8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68])
y2 = np.array([9.14, 8.14, 8.74, 8.77, 9.26, 8.10, 6.13, 3.10, 9.13, 7.26, 4.74])
print(round(y1.mean(), 2), round(y2.mean(), 2)) # 7.5 7.5
# y1 is a linear cloud, y2 a smooth curvePlotting the data before building a model prevents choosing the wrong model from the start.
Practice First plot
Open a figure with plt.subplots(), plot y against x with ax.plot, give the title and axis names and save it with fig.savefig("egri.png"). Finally print the largest value of y.
Starter code
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(5)
y = x ** 2
# buraya yazSample solution
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(5)
y = x ** 2
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title("Kareler")
ax.set_xlabel("x")
ax.set_ylabel("x²")
fig.savefig("egri.png")
print(y.max())Expected output
16
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 In <code>fig, ax = plt.subplots()</code>, onto which object is the line drawn?
- ax Correct answer
- fig
- plt.show
- savefig
Explanation: The axis object carries the drawing methods like plot, scatter and hist.
2 Save the plot to a file.
fig.____("grafik.png")
fig.savefig("grafik.png")
Explanation: savefig writes PNG, SVG or PDF depending on the file extension.
3 Match the parts.
| Term | Meaning |
|---|---|
fig |
The whole figure |
ax |
Drawing area |
set_xlabel |
Axis name |
figsize |
Figure size |