annotate plotFit.m @ 5:eddd33e57f6a default tip

Justify loop in trainLinearReg
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Sun, 27 Nov 2011 15:58:14 -0500
parents 0f14514e907f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
1 function plotFit(min_x, max_x, mu, sigma, theta, p)
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
2 %PLOTFIT Plots a learned polynomial regression fit over an existing figure.
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
3 %Also works with linear regression.
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
4 % PLOTFIT(min_x, max_x, mu, sigma, theta, p) plots the learned polynomial
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
5 % fit with power p and feature normalization (mu, sigma).
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
6
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
7 % Hold on to the current figure
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
8 hold on;
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
9
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
10 % We plot a range slightly bigger than the min and max values to get
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
11 % an idea of how the fit will vary outside the range of the data points
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
12 x = (min_x - 15: 0.05 : max_x + 25)';
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
13
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
14 % Map the X values
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
15 X_poly = polyFeatures(x, p);
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
16 X_poly = bsxfun(@minus, X_poly, mu);
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
17 X_poly = bsxfun(@rdivide, X_poly, sigma);
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
18
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
19 % Add ones
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
20 X_poly = [ones(size(x, 1), 1) X_poly];
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
21
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
22 % Plot
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
23 plot(x, X_poly * theta, '--', 'LineWidth', 2)
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
24
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
25 % Hold off to the current figure
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
26 hold off
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
27
0f14514e907f initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
28 end