In [2]:
using Gadfly

In [3]:
data = readdlm("../../../datasets/ml-ex1/ex1data2.txt", ',');

In [4]:
oX = data[:,1:2];
y = data[:,3];
m = length(y);
X = [ones(m, 1) oX];

In [25]:
function feature_normalize(X)
    means = mean(X, 1)
    stds = std(X, 1)
    return (X .- means) ./ stds
end

function compute_cost(X, y, theta)
  return sum(((theta' * X')' - y).^2) / (2 * length(y))
end

function gradient_descent(X, y, theta, alpha, iterations)
  cost_history = zeros(iterations, 1)
  for i in collect(1:iterations)
    theta = (theta' - ((alpha / length(y)) * sum((sum((theta' .* X), 2) - y).*X, 1)))'
    cost_history[i] = compute_cost(X, y, theta)
  end
  return theta, cost_history
end


Out[25]:
gradient_descent (generic function with 1 method)

In [29]:
X = [ones(m, 1) feature_normalize(oX)]
num_features = length(X[1,:]);
theta = zeros(num_features, 1);
iterations = 50;
alpha = 0.1;
initial_cost = compute_cost(X, y, theta);
(ptheta, cost_history) = gradient_descent(X, y, theta, alpha, iterations);

plot(x=1:iterations, y=cost_history[:,1], Geom.line)


Out[29]:
x -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 -50 -48 -46 -44 -42 -40 -38 -36 -34 -32 -30 -28 -26 -24 -22 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 -50 0 50 100 -50 -45 -40 -35 -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 -7.0×1010 -6.0×1010 -5.0×1010 -4.0×1010 -3.0×1010 -2.0×1010 -1.0×1010 0 1.0×1010 2.0×1010 3.0×1010 4.0×1010 5.0×1010 6.0×1010 7.0×1010 8.0×1010 9.0×1010 1.0×1011 1.1×1011 1.2×1011 1.3×1011 -6.00×1010 -5.80×1010 -5.60×1010 -5.40×1010 -5.20×1010 -5.00×1010 -4.80×1010 -4.60×1010 -4.40×1010 -4.20×1010 -4.00×1010 -3.80×1010 -3.60×1010 -3.40×1010 -3.20×1010 -3.00×1010 -2.80×1010 -2.60×1010 -2.40×1010 -2.20×1010 -2.00×1010 -1.80×1010 -1.60×1010 -1.40×1010 -1.20×1010 -1.00×1010 -8.00×109 -6.00×109 -4.00×109 -2.00×109 0 2.00×109 4.00×109 6.00×109 8.00×109 1.00×1010 1.20×1010 1.40×1010 1.60×1010 1.80×1010 2.00×1010 2.20×1010 2.40×1010 2.60×1010 2.80×1010 3.00×1010 3.20×1010 3.40×1010 3.60×1010 3.80×1010 4.00×1010 4.20×1010 4.40×1010 4.60×1010 4.80×1010 5.00×1010 5.20×1010 5.40×1010 5.60×1010 5.80×1010 6.00×1010 6.20×1010 6.40×1010 6.60×1010 6.80×1010 7.00×1010 7.20×1010 7.40×1010 7.60×1010 7.80×1010 8.00×1010 8.20×1010 8.40×1010 8.60×1010 8.80×1010 9.00×1010 9.20×1010 9.40×1010 9.60×1010 9.80×1010 1.00×1011 1.02×1011 1.04×1011 1.06×1011 1.08×1011 1.10×1011 1.12×1011 1.14×1011 1.16×1011 1.18×1011 1.20×1011 -1×1011 0 1×1011 2×1011 -6.00×1010 -5.50×1010 -5.00×1010 -4.50×1010 -4.00×1010 -3.50×1010 -3.00×1010 -2.50×1010 -2.00×1010 -1.50×1010 -1.00×1010 -5.00×109 0 5.00×109 1.00×1010 1.50×1010 2.00×1010 2.50×1010 3.00×1010 3.50×1010 4.00×1010 4.50×1010 5.00×1010 5.50×1010 6.00×1010 6.50×1010 7.00×1010 7.50×1010 8.00×1010 8.50×1010 9.00×1010 9.50×1010 1.00×1011 1.05×1011 1.10×1011 1.15×1011 1.20×1011 y

In [33]:
function normal_equation(X, y)
    return (pinv(X'*X))*(X'*y)
end

normal_equation(X, y)


Out[33]:
3-element Array{Float64,1}:
 340413.0 
 110631.0 
  -6649.47

In [ ]: