import matplotlib.pyplot as pl
import numpy as np
Plots for machine learning ✅
1 Matplotlib
We will be covering key plotting APIs that will be very useful for visualization of machine learning data.
https://medium.com/mlpoint/matplotlib-for-machine-learning-6b5fcd4fbbc7
#
# an empty figure
#
= pl.figure(figsize=(3, 3))
fig 'X labels go here')
pl.xlabel('Y labels go here')
pl.ylabel(0,1,2,3,4,5])
pl.xticks([
#
# get the axis object to set the tick labels
#
= pl.gca()
ax 'a', 'b', 'c', 'd', 'e', 'f']); ax.set_xticklabels([
=(6,6))
pl.figure(figsize
= np.linspace(-2*np.pi, 2*np.pi, 1000)
x = np.sin(x)
y1 = np.cos(x)
y2
='red', linestyle='-')
pl.plot(x, y1, color='blue', linestyle='--')
pl.plot(x, y2, color
'y1=sin(x)', 'y2=cos(x)'], loc='upper right'); pl.legend([
2 Line plots
#
# Plotting line with annotation
#
= np.linspace(-1, 1, 20)
x = np.abs(np.random.randn(20))
y1 '-o')
pl.plot(x, y1,
10], y1[10], 'midpoint', fontsize=12); pl.text(x[
#
# Area plot
#
='blue', alpha=0.1)
pl.fill_between(x, y1, color=2, color='blue'); pl.plot(x, y1, linewidth
= - y1 / 2
y2
='gray', alpha=0.5);
pl.fill_between(x, y1, y2, color='black', linewidth=2); pl.plot(x, y1, x, y2, color
3 Bar Plots
= ['Jack', 'Jill', 'Joe']
names = np.array([0, 1, 2])
x = [70, 75, 80]
grades
pl.bar(x, grades); pl.xticks(x, names)
= [95, 95, 90]
grades_winter
+0.00, grades, width=0.25, label='Fall')
pl.bar(x+0.25, grades_winter, width=0.25, label='Winter')
pl.bar(xNone, 120)
pl.ylim(='upper left'); pl.legend(loc
#
# final, midterm, quiz
#
= np.array([
grades 30, 30, 10],
[50, 30, 15],
[45, 23, 5],
[
])
0], label='final')
pl.bar(x, grades[:, 1], bottom=grades[:, 0], label='midterm', )
pl.bar(x, grades[:, 2], bottom=grades[:, :2].sum(axis=1), label='quiz')
pl.bar(x, grades[:,
pl.legend(); pl.xticks(x, names)
4 Scatter Plots
= 500
n = np.random.randn(n)
x = np.random.randn(n)
y =10); pl.scatter(x, y, s
#
# with size variation
#
= np.abs(np.random.randn(n)) * 30
size =size, alpha=0.4, linewidth=0); pl.scatter(x, y, s
#
# with color variation
#
= np.full((n,), '#f00')
colors <= y] = '#0f0'
colors[x
=size, c=colors, alpha=0.4, edgecolors='none'); pl.scatter(x, y, s
5 More to come
As we proceed further with the topics of machine learning, we will encounter more mathematical objects and ways to show them graphically.
We will look at:
- generate meshgrids
- contour plots of potential functions
- quiver plots for field functions
- 3D plots for higher dimensional datasets