updated plots of the data analysis chapters
This commit is contained in:
@@ -16,11 +16,11 @@ def create_data():
|
||||
|
||||
|
||||
def plot_data(ax, x, y, c):
|
||||
ax.scatter(x, y, marker='o', color=colors['blue'], s=40, zorder=10)
|
||||
ax.plot(x, y, zorder=10, **psAm)
|
||||
xx = np.linspace(2.1, 3.9, 100)
|
||||
ax.plot(xx, c*xx**3.0, color=colors['red'], lw=2, zorder=5)
|
||||
ax.plot(xx, c*xx**3.0, zorder=5, **lsBm)
|
||||
for cc in [0.25*c, 0.5*c, 2.0*c, 4.0*c]:
|
||||
ax.plot(xx, cc*xx**3.0, color=colors['orange'], lw=1.5, zorder=5)
|
||||
ax.plot(xx, cc*xx**3.0, zorder=5, **lsDm)
|
||||
ax.set_xlabel('Size x', 'm')
|
||||
ax.set_ylabel('Weight y', 'kg')
|
||||
ax.set_xlim(2, 4)
|
||||
@@ -42,15 +42,15 @@ def plot_data_errors(ax, x, y, c):
|
||||
xytext=(3.4, 70), textcoords='data', ha='left',
|
||||
arrowprops=dict(arrowstyle="->", relpos=(0.9,1.0),
|
||||
connectionstyle="angle3,angleA=50,angleB=-30") )
|
||||
ax.scatter(x[:40], y[:40], color=colors['blue'], s=10, zorder=0)
|
||||
ax.plot(x[:40], y[:40], zorder=0, **psAm)
|
||||
inxs = [3, 10, 11, 17, 18, 21, 28, 30, 33]
|
||||
ax.scatter(x[inxs], y[inxs], color=colors['blue'], s=40, zorder=10)
|
||||
ax.plot(x[inxs], y[inxs], zorder=10, **psA)
|
||||
xx = np.linspace(2.1, 3.9, 100)
|
||||
ax.plot(xx, c*xx**3.0, color=colors['red'], lw=2)
|
||||
ax.plot(xx, c*xx**3.0, **lsBm)
|
||||
for i in inxs :
|
||||
xx = [x[i], x[i]]
|
||||
yy = [c*x[i]**3.0, y[i]]
|
||||
ax.plot(xx, yy, color=colors['orange'], lw=2, zorder=5)
|
||||
ax.plot(xx, yy, zorder=5, **lsDm)
|
||||
|
||||
def plot_error_hist(ax, x, y, c):
|
||||
ax.set_xlabel('Squared error')
|
||||
@@ -67,7 +67,7 @@ def plot_error_hist(ax, x, y, c):
|
||||
xytext=(800, 3), textcoords='data', ha='left',
|
||||
arrowprops=dict(arrowstyle="->", relpos=(0.0,0.2),
|
||||
connectionstyle="angle3,angleA=10,angleB=90") )
|
||||
ax.hist(errors, bins, color=colors['orange'])
|
||||
ax.hist(errors, bins, **fsC)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -16,11 +16,11 @@ if __name__ == "__main__":
|
||||
fig, ax = plt.subplots(figsize=cm_size(figure_width, 1.4*figure_height))
|
||||
fig.subplots_adjust(**adjust_fs(left=6.0, right=1.2))
|
||||
|
||||
ax.scatter(x, y, marker='o', color=colors['blue'], s=40, zorder=10)
|
||||
ax.plot(x, y, zorder=10, **psA)
|
||||
xx = np.linspace(2.1, 3.9, 100)
|
||||
ax.plot(xx, c*xx**3.0, color=colors['red'], lw=3, zorder=5)
|
||||
ax.plot(xx, c*xx**3.0, zorder=5, **lsB)
|
||||
for cc in [0.25*c, 0.5*c, 2.0*c, 4.0*c]:
|
||||
ax.plot(xx, cc*xx**3.0, color=colors['orange'], lw=2, zorder=5)
|
||||
ax.plot(xx, cc*xx**3.0, zorder=5, **lsDm)
|
||||
ax.set_xlabel('Size x', 'm')
|
||||
ax.set_ylabel('Weight y', 'kg')
|
||||
ax.set_xlim(2, 4)
|
||||
|
||||
@@ -39,9 +39,9 @@ def plot_mse(ax, x, y, c, cs):
|
||||
for i, cc in enumerate(ccs):
|
||||
mses[i] = np.mean((y-(cc*x**3.0))**2.0)
|
||||
|
||||
ax.plot(ccs, mses, colors['blue'], lw=2, zorder=10)
|
||||
ax.scatter(cs, ms, color=colors['red'], s=40, zorder=20)
|
||||
ax.scatter(cs[-1], ms[-1], color=colors['orange'], s=60, zorder=30)
|
||||
ax.plot(ccs, mses, zorder=10, **lsAm)
|
||||
ax.plot(cs[:12], ms[:12], zorder=20, **psB)
|
||||
ax.plot(cs[-1], ms[-1], zorder=30, **psC)
|
||||
for i in range(4):
|
||||
ax.annotate('',
|
||||
xy=(cs[i+1]+0.2, ms[i+1]), xycoords='data',
|
||||
@@ -56,12 +56,12 @@ def plot_mse(ax, x, y, c, cs):
|
||||
ax.set_yticks(np.arange(0, 30001, 10000))
|
||||
|
||||
def plot_descent(ax, cs, mses):
|
||||
ax.plot(np.arange(len(mses))+1, mses, '-o', c=colors['red'], mew=0, ms=8)
|
||||
ax.plot(np.arange(len(mses))+1, mses, **lpsBm)
|
||||
ax.set_xlabel('Iteration')
|
||||
#ax.set_ylabel('Mean squared error')
|
||||
ax.set_xlim(0, 10.5)
|
||||
ax.set_xlim(0, 12.5)
|
||||
ax.set_ylim(0, 25000)
|
||||
ax.set_xticks(np.arange(0.0, 10.1, 2.0))
|
||||
ax.set_xticks(np.arange(0.0, 12.1, 2.0))
|
||||
ax.set_yticks(np.arange(0, 30001, 10000))
|
||||
ax.set_yticklabels([])
|
||||
|
||||
|
||||
@@ -4,8 +4,7 @@ from plotstyle import *
|
||||
|
||||
plain_style()
|
||||
|
||||
fig = plt.figure( figsize=(2.5,3.4) )
|
||||
ax = fig.add_subplot(1, 1, 1)
|
||||
fig, ax = plt.subplots(figsize=(2.5,3.4))
|
||||
|
||||
# parabula:
|
||||
x1 = -0.2
|
||||
@@ -14,7 +13,7 @@ x = np.linspace(x1, x2, 200)
|
||||
y = x*x
|
||||
ax.set_xlim(x1, x2)
|
||||
ax.set_ylim(-0.2, 0.7)
|
||||
ax.plot(x, y, c=colors['blue'], lw=4, zorder=0)
|
||||
ax.plot(x, y, zorder=0, **lsA)
|
||||
# secant:
|
||||
x = np.asarray([0.1, 0.7])
|
||||
y = x*x
|
||||
@@ -22,33 +21,33 @@ ax.set_xticks(x)
|
||||
ax.set_yticks(y)
|
||||
ax.set_xticklabels(['$x$','$x+\Delta x$'])
|
||||
ax.set_yticklabels(['',''])
|
||||
ax.scatter(x, y, c=colors['red'], edgecolor='none', s=150, zorder=10)
|
||||
ax.plot(x, y, zorder=10, **psB)
|
||||
# function values:
|
||||
ax.plot([x[0], x[0], x1],[-0.2, y[0], y[0]], '--k', lw=1, zorder=6)
|
||||
ax.plot([x[1], x[1], x1],[-0.2, y[1], y[1]], '--k', lw=1, zorder=6)
|
||||
ax.plot([x[0], x[0], x1],[-0.2, y[0], y[0]], zorder=6, **lsGrid)
|
||||
ax.plot([x[1], x[1], x1],[-0.2, y[1], y[1]], zorder=6, **lsGrid)
|
||||
ax.text(x1+0.05, y[0]+0.05, '$f(x)$', zorder=6)
|
||||
ax.text(x1+0.05, y[1]+0.05, '$f(x+\Delta x)$', zorder=6)
|
||||
# slope triangle:
|
||||
ax.plot([x[0], x[1], x[1]],[y[0], y[0], y[1]], '-k', lw=2, zorder=7)
|
||||
ax.text(np.mean(x), y[0]-0.08, '$\Delta x$', ha='center', zorder=7)
|
||||
ax.plot([x[0], x[1], x[1]],[y[0], y[0], y[1]], zorder=7, **lsMarker)
|
||||
ax.text(np.mean(x), y[0]-0.07, '$\Delta x$', ha='center', zorder=7)
|
||||
ax.text(x[1]+0.05, np.mean(y), '$f(x+\Delta x)-f(x)$', va='center', rotation='vertical', zorder=7)
|
||||
# secant line:
|
||||
m = np.diff(y)/np.diff(x)
|
||||
xl = [x1, x2]
|
||||
yl = m*(xl-x[0])+y[0]
|
||||
ax.plot(xl, yl, c=colors['red'], lw=3, zorder=7)
|
||||
ax.plot(xl, yl, zorder=7, **lsBm)
|
||||
|
||||
# derivative:
|
||||
md = 2.0*x[0]
|
||||
yl = md*(xl-x[0])+y[0]
|
||||
ax.plot(xl, yl, c=colors['yellow'], lw=3, zorder=5)
|
||||
ax.plot(xl, yl, zorder=5, **lsDm)
|
||||
|
||||
# limit:
|
||||
for ml in np.linspace(md, m, 5)[1:] :
|
||||
yl = ml*(xl-x[0])+y[0]
|
||||
xs = 0.5*(ml+np.sqrt(ml*ml-4.0*(ml*x[0]-y[0])))
|
||||
ax.scatter([xs], [xs*xs], c=colors['orange'], edgecolor='none', s=80, zorder=3)
|
||||
ax.plot(xl, yl, c=colors['orange'], lw=2, zorder=3)
|
||||
ax.plot([xs], [xs*xs], zorder=3, **psC)
|
||||
ax.plot(xl, yl, zorder=3, **lsCm)
|
||||
|
||||
fig.subplots_adjust(**adjust_fs(fig, 0.5, 0.5, 1.4, 0.5))
|
||||
plt.savefig('derivative.pdf')
|
||||
|
||||
@@ -14,7 +14,7 @@ def create_data():
|
||||
|
||||
|
||||
def plot_data(ax, x, y):
|
||||
ax.scatter(x, y, marker='o', color=colors['blue'], s=40)
|
||||
ax.plot(x, y, **psA)
|
||||
ax.set_xlabel('Input x')
|
||||
ax.set_ylabel('Output y')
|
||||
ax.set_xlim(0, 120)
|
||||
@@ -24,10 +24,10 @@ def plot_data(ax, x, y):
|
||||
|
||||
|
||||
def plot_data_slopes(ax, x, y, m, n):
|
||||
ax.scatter(x, y, marker='o', color=colors['blue'], s=40)
|
||||
ax.plot(x, y, **psA)
|
||||
xx = np.asarray([2, 118])
|
||||
for i in np.linspace(0.3*m, 2.0*m, 5):
|
||||
ax.plot(xx, i*xx+n, color=colors['red'], lw=2)
|
||||
ax.plot(xx, i*xx+n, **lsBm)
|
||||
ax.set_xlabel('Input x')
|
||||
#ax.set_ylabel('Output y')
|
||||
ax.set_xlim(0, 120)
|
||||
@@ -37,10 +37,10 @@ def plot_data_slopes(ax, x, y, m, n):
|
||||
|
||||
|
||||
def plot_data_intercepts(ax, x, y, m, n):
|
||||
ax.scatter(x, y, marker='o', color=colors['blue'], s=40)
|
||||
ax.plot(x, y, **psA)
|
||||
xx = np.asarray([2, 118])
|
||||
for i in np.linspace(n-1*n, n+1*n, 5):
|
||||
ax.plot(xx, m*xx + i, color=colors['red'], lw=2)
|
||||
ax.plot(xx, m*xx + i, **lsBm)
|
||||
ax.set_xlabel('Input x')
|
||||
#ax.set_ylabel('Output y')
|
||||
ax.set_xlim(0, 120)
|
||||
|
||||
@@ -25,15 +25,15 @@ def plot_data(ax, x, y, m, n):
|
||||
xytext=(80, -50), textcoords='data', ha='left',
|
||||
arrowprops=dict(arrowstyle="->", relpos=(0.9,1.0),
|
||||
connectionstyle="angle3,angleA=50,angleB=-30") )
|
||||
ax.scatter(x[:40], y[:40], color=colors['blue'], s=10, zorder=0)
|
||||
ax.plot(x[:40], y[:40], zorder=0, **psAm)
|
||||
inxs = [3, 13, 16, 19, 25, 34, 36]
|
||||
ax.scatter(x[inxs], y[inxs], color=colors['blue'], s=40, zorder=10)
|
||||
ax.plot(x[inxs], y[inxs], zorder=10, **psA)
|
||||
xx = np.asarray([2, 118])
|
||||
ax.plot(xx, m*xx+n, color=colors['red'], lw=2)
|
||||
ax.plot(xx, m*xx+n, **lsBm)
|
||||
for i in inxs :
|
||||
xx = [x[i], x[i]]
|
||||
yy = [m*x[i]+n, y[i]]
|
||||
ax.plot(xx, yy, color=colors['orange'], lw=2, zorder=5)
|
||||
ax.plot(xx, yy, zorder=5, **lsDm)
|
||||
|
||||
|
||||
def plot_error_hist(ax, x, y, m, n):
|
||||
@@ -51,7 +51,7 @@ def plot_error_hist(ax, x, y, m, n):
|
||||
xytext=(350, 20), textcoords='data', ha='left',
|
||||
arrowprops=dict(arrowstyle="->", relpos=(0.0,0.2),
|
||||
connectionstyle="angle3,angleA=10,angleB=90") )
|
||||
ax.hist(errors, bins, color=colors['orange'])
|
||||
ax.hist(errors, bins, **fsD)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user