Selaa lähdekoodia

complete optional ex

astron 7 vuotta sitten
vanhempi
commit
b4251dd358

+ 1 - 1
machine learning/machine-learning-ex1/ex1/computeCostMulti.m

@@ -13,7 +13,7 @@ J = 0;
 % Instructions: Compute the cost of a particular choice of theta
 %               You should set J to the cost.
 
-
+J = 1 / (2 * m) * sum((X * theta - y) .^2);
 
 
 

+ 43 - 8
machine learning/machine-learning-ex1/ex1/ex1_multi.m

@@ -43,14 +43,38 @@ m = length(y);
 fprintf('First 10 examples from the dataset: \n');
 fprintf(' x = [%.0f %.0f], y = %.0f \n', [X(1:10,:) y(1:10,:)]');
 
-fprintf('Program paused. Press enter to continue.\n');
-pause;
+%% Plotting =======================
+fprintf('Plotting Data ...\n')
+subplot(2,1,1);
+plot(X(:,1),y,'rx','MarkerSize', 10);
+ylabel('price');
+xlabel('house size in sqft');
+subplot(2,1,2);
+ylabel('price');
+xlabel('num bedrooms');
+plot(X(:,2),y,'bo','MarkerSize', 10);
+
+%fprintf('Program paused. Press enter to continue.\n');
+%pause;
+
+
+
+
 
 % Scale features and set them to zero mean
 fprintf('Normalizing Features ...\n');
 
+
 [X mu sigma] = featureNormalize(X);
 
+fprintf('First 10 examples from the dataset: \n');
+fprintf(' x = [%f %f]\n', [X(1:10,:)]');
+
+
+
+
+
+
 % Add intercept term to X
 X = [ones(m, 1) X];
 
@@ -82,7 +106,7 @@ X = [ones(m, 1) X];
 fprintf('Running gradient descent ...\n');
 
 % Choose some alpha value
-alpha = 0.01;
+alpha = 0.1;
 num_iters = 400;
 
 % Init Theta and Run Gradient Descent 
@@ -104,16 +128,27 @@ fprintf('\n');
 % ====================== YOUR CODE HERE ======================
 % Recall that the first column of X is all-ones. Thus, it does
 % not need to be normalized.
-price = 0; % You should change this
+%price = 0; % You should change this
 
 
-% ============================================================
+X_norm = [1 ([1650 3] - mu) ./ sigma];
+price = X_norm * theta;
 
 fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
          '(using gradient descent):\n $%f\n'], price);
 
-fprintf('Program paused. Press enter to continue.\n');
-pause;
+
+X_norm = [1 ([2000 4 ] - mu) ./ sigma];
+price = X_norm * theta;
+
+
+% ============================================================
+
+fprintf(['Predicted price of a 2000 sq-ft, 4 br house ' ...
+         '(using gradient descent):\n $%f\n'], price);
+
+%fprintf('Program paused. Press enter to continue.\n');
+%pause;
 
 %% ================ Part 3: Normal Equations ================
 
@@ -150,7 +185,7 @@ fprintf('\n');
 % Estimate the price of a 1650 sq-ft, 3 br house
 % ====================== YOUR CODE HERE ======================
 price = 0; % You should change this
-
+price = [1 1650 3] * theta;
 
 % ============================================================
 

+ 3 - 1
machine learning/machine-learning-ex1/ex1/featureNormalize.m

@@ -26,7 +26,9 @@ sigma = zeros(1, size(X, 2));
 % Hint: You might find the 'mean' and 'std' functions useful.
 %       
 
-
+mu = mean(X)
+sigma = std(X)
+X_norm = (X - mu) ./ sigma;
 
 
 

+ 10 - 3
machine learning/machine-learning-ex1/ex1/gradientDescent.m

@@ -24,10 +24,17 @@ for iter = 1:num_iters
     % https://octave.org/doc/v4.0.3/Arithmetic-Ops.html
     % use .* to do element by element multiplicaton
     
-    temp0 = theta(1,1) - alpha / m * sum(hy .* X(:,1));
-    temp1 = theta(2,1) - alpha / m * sum(hy .* X(:,2));
+    %temp0 = theta(1,1) - alpha / m * sum(hy .* X(:,1));
+    %temp1 = theta(2,1) - alpha / m * sum(hy .* X(:,2));
     %fprintf ("temp0/1: %f // %f / %f\n", temp0, temp1, temp1 + temp0);
-    theta = [temp0;temp1];
+    %theta = [temp0;temp1]
+    
+    
+    % amend on 2018/12/20 to do the multiplication in one step
+    theta = (theta' - alpha / m * sum(hy .* X))';
+    
+    
+    
     
 
 

+ 3 - 2
machine learning/machine-learning-ex1/ex1/gradientDescentMulti.m

@@ -16,8 +16,9 @@ for iter = 1:num_iters
     % Hint: While debugging, it can be useful to print out the values
     %       of the cost function (computeCostMulti) and gradient here.
     %
-
-
+    h_theta = X * theta;
+    hy = (h_theta - y);
+    theta = (theta' - alpha / m * sum(hy .* X))';
 
 
 

+ 1 - 1
machine learning/machine-learning-ex1/ex1/normalEqn.m

@@ -12,7 +12,7 @@ theta = zeros(size(X, 2), 1);
 
 % ---------------------- Sample Solution ----------------------
 
-
+theta = (X' * X) ^ -1 * X' * y;
 
 
 % -------------------------------------------------------------