From 14bc59e4aa906787da85fe04fe8ddd0d03b0e49a Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Tue, 25 Jun 2019 20:08:20 +0530 Subject: [PATCH 01/27] Added week 1 assignment via upload --- .../Week 1/jayakar_week1/computeCost.m | 23 +++ .../Week 1/jayakar_week1/computeCostMulti.m | 24 +++ .../Week 1/jayakar_week1/ex1.m | 135 +++++++++++++++ .../Week 1/jayakar_week1/ex1_multi.m | 162 ++++++++++++++++++ .../Week 1/jayakar_week1/ex1data1.txt | 97 +++++++++++ .../Week 1/jayakar_week1/ex1data2.txt | 47 +++++ .../Week 1/jayakar_week1/featureNormalize.m | 42 +++++ .../Week 1/jayakar_week1/gradientDescent.m | 31 ++++ .../jayakar_week1/gradientDescentMulti.m | 44 +++++ .../Week 1/jayakar_week1/normalEqn.m | 23 +++ .../Week 1/jayakar_week1/plotData.m | 27 +++ .../Week 1/jayakar_week1/warmUpExercise.m | 21 +++ 12 files changed, 676 insertions(+) create mode 100644 Programming Assignment/Week 1/jayakar_week1/computeCost.m create mode 100644 Programming Assignment/Week 1/jayakar_week1/computeCostMulti.m create mode 100644 Programming Assignment/Week 1/jayakar_week1/ex1.m create mode 100644 Programming Assignment/Week 1/jayakar_week1/ex1_multi.m create mode 100644 Programming Assignment/Week 1/jayakar_week1/ex1data1.txt create mode 100644 Programming Assignment/Week 1/jayakar_week1/ex1data2.txt create mode 100644 Programming Assignment/Week 1/jayakar_week1/featureNormalize.m create mode 100644 Programming Assignment/Week 1/jayakar_week1/gradientDescent.m create mode 100644 Programming Assignment/Week 1/jayakar_week1/gradientDescentMulti.m create mode 100644 Programming Assignment/Week 1/jayakar_week1/normalEqn.m create mode 100644 Programming Assignment/Week 1/jayakar_week1/plotData.m create mode 100644 Programming Assignment/Week 1/jayakar_week1/warmUpExercise.m diff --git a/Programming Assignment/Week 1/jayakar_week1/computeCost.m b/Programming Assignment/Week 1/jayakar_week1/computeCost.m new file mode 100644 index 0000000..67babf2 --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week1/computeCost.m @@ -0,0 +1,23 @@ +function J = computeCost(X, y, theta) +%COMPUTECOST Compute cost for linear regression +% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the +% parameter for linear regression to fit the data points in X and y + +% Initialize some useful values +m = length(y); +h=zeros(m); % number of training examples +h=X*theta; +% You need to return the following variables correctly +J = 0; +for i=1:m,J=J+((h(i)-y(i))^2);,end; +J=(J/(2*m)); +% ====================== YOUR CODE HERE ====================== +% Instructions: Compute the cost of a particular choice of theta +% You should set J to the cost. + + + + + +% ========================================================================= +end diff --git a/Programming Assignment/Week 1/jayakar_week1/computeCostMulti.m b/Programming Assignment/Week 1/jayakar_week1/computeCostMulti.m new file mode 100644 index 0000000..b2aec95 --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week1/computeCostMulti.m @@ -0,0 +1,24 @@ +function J = computeCostMulti(X, y, theta) +%COMPUTECOSTMULTI Compute cost for linear regression with multiple variables +% J = COMPUTECOSTMULTI(X, y, theta) computes the cost of using theta as the +% parameter for linear regression to fit the data points in X and y + +% Initialize some useful values +m = length(y); +h=zeros(m); % number of training examples +h=X*theta; +% You need to return the following variables correctly +J = 0; +for i=1:m,J=J+((h(i)-y(i))^2);,end; +J=(J/(2*m)); +% ====================== YOUR CODE HERE ====================== +% Instructions: Compute the cost of a particular choice of theta +% You should set J to the cost. + + + + + +% ========================================================================= + +end diff --git a/Programming Assignment/Week 1/jayakar_week1/ex1.m b/Programming Assignment/Week 1/jayakar_week1/ex1.m new file mode 100644 index 0000000..ad6a4aa --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week1/ex1.m @@ -0,0 +1,135 @@ +%% Machine Learning Online Class - Exercise 1: Linear Regression + +% Instructions +% ------------ +% +% This file contains code that helps you get started on the +% linear exercise. You will need to complete the following functions +% in this exericse: +% +% warmUpExercise.m +% plotData.m +% gradientDescent.m +% computeCost.m +% gradientDescentMulti.m +% computeCostMulti.m +% featureNormalize.m +% normalEqn.m +% +% For this exercise, you will not need to change any code in this file, +% or any other files other than those mentioned above. +% +% x refers to the population size in 10,000s +% y refers to the profit in $10,000s +% + +%% Initialization +clear ; close all; clc + +%% ==================== Part 1: Basic Function ==================== +% Complete warmUpExercise.m +fprintf('Running warmUpExercise ... \n'); +fprintf('5x5 Identity Matrix: \n'); +warmUpExercise() + +fprintf('Program paused. Press enter to continue.\n'); +pause; + + +%% ======================= Part 2: Plotting ======================= +fprintf('Plotting Data ...\n') +data = load('ex1data1.txt'); +X = data(:, 1); y = data(:, 2); +m = length(y); % number of training examples + +% Plot Data +% Note: You have to complete the code in plotData.m +plotData(X, y); + +fprintf('Program paused. Press enter to continue.\n'); +pause; + +%% =================== Part 3: Cost and Gradient descent =================== + +X = [ones(m, 1), data(:,1)]; % Add a column of ones to x +theta = zeros(2, 1); % initialize fitting parameters + +% Some gradient descent settings +iterations = 1500; +alpha = 0.01; + +fprintf('\nTesting the cost function ...\n') +% compute and display initial cost +J = computeCost(X, y, theta); +fprintf('With theta = [0 ; 0]\nCost computed = %f\n', J); +fprintf('Expected cost value (approx) 32.07\n'); + +% further testing of the cost function +J = computeCost(X, y, [-1 ; 2]); +fprintf('\nWith theta = [-1 ; 2]\nCost computed = %f\n', J); +fprintf('Expected cost value (approx) 54.24\n'); + +fprintf('Program paused. Press enter to continue.\n'); +pause; + +fprintf('\nRunning Gradient Descent ...\n') +% run gradient descent +theta = gradientDescent(X, y, theta, alpha, iterations); + +% print theta to screen +fprintf('Theta found by gradient descent:\n'); +fprintf('%f\n', theta); +fprintf('Expected theta values (approx)\n'); +fprintf(' -3.6303\n 1.1664\n\n'); + +% Plot the linear fit +hold on; % keep previous plot visible +plot(X(:,2), X*theta, '-') +legend('Training data', 'Linear regression') +hold off % don't overlay any more plots on this figure + +% Predict values for population sizes of 35,000 and 70,000 +predict1 = [1, 3.5] *theta; +fprintf('For population = 35,000, we predict a profit of %f\n',... + predict1*10000); +predict2 = [1, 7] * theta; +fprintf('For population = 70,000, we predict a profit of %f\n',... + predict2*10000); + +fprintf('Program paused. Press enter to continue.\n'); +pause; + +%% ============= Part 4: Visualizing J(theta_0, theta_1) ============= +fprintf('Visualizing J(theta_0, theta_1) ...\n') + +% Grid over which we will calculate J +theta0_vals = linspace(-10, 10, 100); +theta1_vals = linspace(-1, 4, 100); + +% initialize J_vals to a matrix of 0's +J_vals = zeros(length(theta0_vals), length(theta1_vals)); + +% Fill out J_vals +for i = 1:length(theta0_vals) + for j = 1:length(theta1_vals) + t = [theta0_vals(i); theta1_vals(j)]; + J_vals(i,j) = computeCost(X, y, t); + end +end + + +% Because of the way meshgrids work in the surf command, we need to +% transpose J_vals before calling surf, or else the axes will be flipped +J_vals = J_vals'; +% Surface plot +figure; +surf(theta0_vals, theta1_vals, J_vals) +xlabel('\theta_0'); ylabel('\theta_1'); + +% Contour plot +figure; +% Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100 +contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20)) +xlabel('\theta_0'); ylabel('\theta_1'); +hold on; +plot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2); diff --git a/Programming Assignment/Week 1/jayakar_week1/ex1_multi.m b/Programming Assignment/Week 1/jayakar_week1/ex1_multi.m new file mode 100644 index 0000000..cfb2a20 --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week1/ex1_multi.m @@ -0,0 +1,162 @@ +%% Machine Learning Online Class +% Exercise 1: Linear regression with multiple variables +% +% Instructions +% ------------ +% +% This file contains code that helps you get started on the +% linear regression exercise. +% +% You will need to complete the following functions in this +% exericse: +% +% warmUpExercise.m +% plotData.m +% gradientDescent.m +% computeCost.m +% gradientDescentMulti.m +% computeCostMulti.m +% featureNormalize.m +% normalEqn.m +% +% For this part of the exercise, you will need to change some +% parts of the code below for various experiments (e.g., changing +% learning rates). +% + +%% Initialization + +%% ================ Part 1: Feature Normalization ================ + +%% Clear and Close Figures +clear ; close all; clc + +fprintf('Loading data ...\n'); + +%% Load Data +data = load('ex1data2.txt'); +X = data(:, 1:2); +y = data(:, 3); +m = length(y); + +% Print out some data points +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; + +% Scale features and set them to zero mean +fprintf('Normalizing Features ...\n'); + +[X mu sigma] = featureNormalize(X); + +% Add intercept term to X +X = [ones(m, 1) X]; + + +%% ================ Part 2: Gradient Descent ================ + +% ====================== YOUR CODE HERE ====================== +% Instructions: We have provided you with the following starter +% code that runs gradient descent with a particular +% learning rate (alpha). +% +% Your task is to first make sure that your functions - +% computeCost and gradientDescent already work with +% this starter code and support multiple variables. +% +% After that, try running gradient descent with +% different values of alpha and see which one gives +% you the best result. +% +% Finally, you should complete the code at the end +% to predict the price of a 1650 sq-ft, 3 br house. +% +% Hint: By using the 'hold on' command, you can plot multiple +% graphs on the same figure. +% +% Hint: At prediction, make sure you do the same feature normalization. +% + +fprintf('Running gradient descent ...\n'); + +% Choose some alpha value +alpha = 0.01; +num_iters = 400; +% Init Theta and Run Gradient Descent +theta = zeros(3, 1); +[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters); +alpha=0.3; +[theta, J1] = gradientDescentMulti(X, y, theta, alpha, num_iters); +alpha=0.1; +[theta, J2] = gradientDescentMulti(X, y, theta, alpha, num_iters); +alpha=0.03; +[theta, J3] = gradientDescentMulti(X, y, theta, alpha, num_iters); +% Plot the convergence graph +figure; +plot(1:numel(J_history), J_history, '-b', 'LineWidth', 2); +xlabel('Number of iterations'); +ylabel('Cost J'); +% Display gradient descent's result +fprintf('Theta computed from gradient descent: \n'); +fprintf(' %f \n', theta); +fprintf('\n'); + +% Estimate the price of a 1650 sq-ft, 3 br house +% ====================== 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 +pogo=([1650,3]-mu)./sigma; +price=([1,pogo])*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; + +%% ================ Part 3: Normal Equations ================ + +fprintf('Solving with normal equations...\n'); + +% ====================== YOUR CODE HERE ====================== +% Instructions: The following code computes the closed form +% solution for linear regression using the normal +% equations. You should complete the code in +% normalEqn.m +% +% After doing so, you should complete this code +% to predict the price of a 1650 sq-ft, 3 br house. +% + +%% Load Data +data = csvread('ex1data2.txt'); +X = data(:, 1:2); +y = data(:, 3); +m = length(y); + +% Add intercept term to X +X = [ones(m, 1) X]; + +% Calculate the parameters from the normal equation +theta = normalEqn(X, y); + +% Display normal equation's result +fprintf('Theta computed from the normal equations: \n'); +fprintf(' %f \n', theta); +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; + +% ============================================================ + +fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ... + '(using normal equations):\n $%f\n'], price); + diff --git a/Programming Assignment/Week 1/jayakar_week1/ex1data1.txt b/Programming Assignment/Week 1/jayakar_week1/ex1data1.txt new file mode 100644 index 0000000..0f88ccb --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week1/ex1data1.txt @@ -0,0 +1,97 @@ +6.1101,17.592 +5.5277,9.1302 +8.5186,13.662 +7.0032,11.854 +5.8598,6.8233 +8.3829,11.886 +7.4764,4.3483 +8.5781,12 +6.4862,6.5987 +5.0546,3.8166 +5.7107,3.2522 +14.164,15.505 +5.734,3.1551 +8.4084,7.2258 +5.6407,0.71618 +5.3794,3.5129 +6.3654,5.3048 +5.1301,0.56077 +6.4296,3.6518 +7.0708,5.3893 +6.1891,3.1386 +20.27,21.767 +5.4901,4.263 +6.3261,5.1875 +5.5649,3.0825 +18.945,22.638 +12.828,13.501 +10.957,7.0467 +13.176,14.692 +22.203,24.147 +5.2524,-1.22 +6.5894,5.9966 +9.2482,12.134 +5.8918,1.8495 +8.2111,6.5426 +7.9334,4.5623 +8.0959,4.1164 +5.6063,3.3928 +12.836,10.117 +6.3534,5.4974 +5.4069,0.55657 +6.8825,3.9115 +11.708,5.3854 +5.7737,2.4406 +7.8247,6.7318 +7.0931,1.0463 +5.0702,5.1337 +5.8014,1.844 +11.7,8.0043 +5.5416,1.0179 +7.5402,6.7504 +5.3077,1.8396 +7.4239,4.2885 +7.6031,4.9981 +6.3328,1.4233 +6.3589,-1.4211 +6.2742,2.4756 +5.6397,4.6042 +9.3102,3.9624 +9.4536,5.4141 +8.8254,5.1694 +5.1793,-0.74279 +21.279,17.929 +14.908,12.054 +18.959,17.054 +7.2182,4.8852 +8.2951,5.7442 +10.236,7.7754 +5.4994,1.0173 +20.341,20.992 +10.136,6.6799 +7.3345,4.0259 +6.0062,1.2784 +7.2259,3.3411 +5.0269,-2.6807 +6.5479,0.29678 +7.5386,3.8845 +5.0365,5.7014 +10.274,6.7526 +5.1077,2.0576 +5.7292,0.47953 +5.1884,0.20421 +6.3557,0.67861 +9.7687,7.5435 +6.5159,5.3436 +8.5172,4.2415 +9.1802,6.7981 +6.002,0.92695 +5.5204,0.152 +5.0594,2.8214 +5.7077,1.8451 +7.6366,4.2959 +5.8707,7.2029 +5.3054,1.9869 +8.2934,0.14454 +13.394,9.0551 +5.4369,0.61705 diff --git a/Programming Assignment/Week 1/jayakar_week1/ex1data2.txt b/Programming Assignment/Week 1/jayakar_week1/ex1data2.txt new file mode 100644 index 0000000..79e9a80 --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week1/ex1data2.txt @@ -0,0 +1,47 @@ +2104,3,399900 +1600,3,329900 +2400,3,369000 +1416,2,232000 +3000,4,539900 +1985,4,299900 +1534,3,314900 +1427,3,198999 +1380,3,212000 +1494,3,242500 +1940,4,239999 +2000,3,347000 +1890,3,329999 +4478,5,699900 +1268,3,259900 +2300,4,449900 +1320,2,299900 +1236,3,199900 +2609,4,499998 +3031,4,599000 +1767,3,252900 +1888,2,255000 +1604,3,242900 +1962,4,259900 +3890,3,573900 +1100,3,249900 +1458,3,464500 +2526,3,469000 +2200,3,475000 +2637,3,299900 +1839,2,349900 +1000,1,169900 +2040,4,314900 +3137,3,579900 +1811,4,285900 +1437,3,249900 +1239,3,229900 +2132,4,345000 +4215,4,549000 +2162,4,287000 +1664,2,368500 +2238,3,329900 +2567,4,314000 +1200,3,299000 +852,2,179900 +1852,4,299900 +1203,3,239500 diff --git a/Programming Assignment/Week 1/jayakar_week1/featureNormalize.m b/Programming Assignment/Week 1/jayakar_week1/featureNormalize.m new file mode 100644 index 0000000..49660a5 --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week1/featureNormalize.m @@ -0,0 +1,42 @@ +function [X_norm, mu, sigma] = featureNormalize(X) +%FEATURENORMALIZE Normalizes the features in X +% FEATURENORMALIZE(X) returns a normalized version of X where +% the mean value of each feature is 0 and the standard deviation +% is 1. This is often a good preprocessing step to do when +% working with learning algorithms. + +% You need to set these values correctly +X_norm = X; +mu = zeros(1, size(X, 2)); +sigma = zeros(1, size(X, 2)); +mu=mean(X_norm); +X_norm=X_norm-mu; +sigma=std(X_norm); +X_norm=X_norm./sigma; +% ====================== YOUR CODE HERE ====================== +% Instructions: First, for each feature dimension, compute the mean +% of the feature and subtract it from the dataset, +% storing the mean value in mu. Next, compute the +% standard deviation of each feature and divide +% each feature by it's standard deviation, storing +% the standard deviation in sigma. +% +% Note that X is a matrix where each column is a +% feature and each row is an example. You need +% to perform the normalization separately for +% each feature. +% +% Hint: You might find the 'mean' and 'std' functions useful. +% + + + + + + + + + +% ============================================================ + +end diff --git a/Programming Assignment/Week 1/jayakar_week1/gradientDescent.m b/Programming Assignment/Week 1/jayakar_week1/gradientDescent.m new file mode 100644 index 0000000..f4c81a7 --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week1/gradientDescent.m @@ -0,0 +1,31 @@ +function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters) +%GRADIENTDESCENT Performs gradient descent to learn theta +% theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by +% taking num_iters gradient steps with learning rate alpha + +% Initialize some useful values +m = length(y); % number of training examples +J_history = zeros(num_iters, 1); +h=zeros(m); +delta=zeros(size(X,2)); +for iter = 1:num_iters + h=X*theta; + t=theta; + delta=(X'*(h-y)); + theta=t-((alpha/m)*delta); + if(theta==t) + break; + end; + % ====================== YOUR CODE HERE ====================== + % Instructions: Perform a single gradient step on the parameter vector + % theta. + % + % Hint: While debugging, it can be useful to print out the values + % of the cost function (computeCost) and gradient here. + % ============================================================ + + % Save the cost J in every iteration + J_history(iter) = computeCost(X, y, theta); +end + +end diff --git a/Programming Assignment/Week 1/jayakar_week1/gradientDescentMulti.m b/Programming Assignment/Week 1/jayakar_week1/gradientDescentMulti.m new file mode 100644 index 0000000..489e34b --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week1/gradientDescentMulti.m @@ -0,0 +1,44 @@ +function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters) +%GRADIENTDESCENTMULTI Performs gradient descent to learn theta +% theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by +% taking num_iters gradient steps with learning rate alpha + +% Initialize some useful values +m = length(y); % number of training examples +J_history = zeros(num_iters, 1); +h=zeros(m); +delta=zeros(size(X,2)); +for iter = 1:num_iters + h=X*theta; + t=theta; + delta=(X'*(h-y)); + theta=t-((alpha/m)*delta); + if(theta==t) + break; + end; + % ====================== YOUR CODE HERE ====================== + % Instructions: Perform a single gradient step on the parameter vector + % theta. + % + % Hint: While debugging, it can be useful to print out the values + % of the cost function (computeCostMulti) and gradient here. + % + + + + + + + + + + + + % ============================================================ + + % Save the cost J in every iteration + J_history(iter) = computeCostMulti(X, y, theta); + +end + +end diff --git a/Programming Assignment/Week 1/jayakar_week1/normalEqn.m b/Programming Assignment/Week 1/jayakar_week1/normalEqn.m new file mode 100644 index 0000000..a3186f4 --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week1/normalEqn.m @@ -0,0 +1,23 @@ +function [theta] = normalEqn(X, y) +%NORMALEQN Computes the closed-form solution to linear regression +% NORMALEQN(X,y) computes the closed-form solution to linear +% regression using the normal equations. + +theta = zeros(size(X, 2), 1); +theta=pinv(X'*X)*X'*y; +% ====================== YOUR CODE HERE ====================== +% Instructions: Complete the code to compute the closed form solution +% to linear regression and put the result in theta. +% + +% ---------------------- Sample Solution ---------------------- + + + + +% ------------------------------------------------------------- + + +% ============================================================ + +end diff --git a/Programming Assignment/Week 1/jayakar_week1/plotData.m b/Programming Assignment/Week 1/jayakar_week1/plotData.m new file mode 100644 index 0000000..a223398 --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week1/plotData.m @@ -0,0 +1,27 @@ +function plotData(x, y) +%PLOTDATA Plots the data points x and y into a new figure +% PLOTDATA(x,y) plots the data points and gives the figure axes labels of +% population and profit. + +figure; % open a new figure window +plot(x, y, 'rx', 'MarkerSize', 10); +ylabel('Profit in $10,000s'); +xlabel('Population of City in 10,000s'); +% ====================== YOUR CODE HERE ====================== +% Instructions: Plot the training data into a figure using the +% "figure" and "plot" commands. Set the axes labels using +% the "xlabel" and "ylabel" commands. Assume the +% population and revenue data have been passed in +% as the x and y arguments of this function. +% +% Hint: You can use the 'rx' option with plot to have the markers +% appear as red crosses. Furthermore, you can make the +% markers larger by using plot(..., 'rx', 'MarkerSize', 10); + + + + + +% ============================================================ + +end diff --git a/Programming Assignment/Week 1/jayakar_week1/warmUpExercise.m b/Programming Assignment/Week 1/jayakar_week1/warmUpExercise.m new file mode 100644 index 0000000..cbd295f --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week1/warmUpExercise.m @@ -0,0 +1,21 @@ +function A = warmUpExercise() +%WARMUPEXERCISE Example function in octave +% A = WARMUPEXERCISE() is an example function that returns the 5x5 identity matrix + +A = eye(5); +% ============= YOUR CODE HERE ============== +% Instructions: Return the 5x5 identity matrix +% In octave, we return values by defining which variables +% represent the return values (at the top of the file) +% and then set them accordingly. + + + + + + + +% =========================================== + + +end From b9e8e4b70b038e3081c18f3492664d22b22a7eaf Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Tue, 25 Jun 2019 21:14:59 +0530 Subject: [PATCH 02/27] Delete ex1data1.txt --- .../Week 1/jayakar_week1/ex1data1.txt | 97 ------------------- 1 file changed, 97 deletions(-) delete mode 100644 Programming Assignment/Week 1/jayakar_week1/ex1data1.txt diff --git a/Programming Assignment/Week 1/jayakar_week1/ex1data1.txt b/Programming Assignment/Week 1/jayakar_week1/ex1data1.txt deleted file mode 100644 index 0f88ccb..0000000 --- a/Programming Assignment/Week 1/jayakar_week1/ex1data1.txt +++ /dev/null @@ -1,97 +0,0 @@ -6.1101,17.592 -5.5277,9.1302 -8.5186,13.662 -7.0032,11.854 -5.8598,6.8233 -8.3829,11.886 -7.4764,4.3483 -8.5781,12 -6.4862,6.5987 -5.0546,3.8166 -5.7107,3.2522 -14.164,15.505 -5.734,3.1551 -8.4084,7.2258 -5.6407,0.71618 -5.3794,3.5129 -6.3654,5.3048 -5.1301,0.56077 -6.4296,3.6518 -7.0708,5.3893 -6.1891,3.1386 -20.27,21.767 -5.4901,4.263 -6.3261,5.1875 -5.5649,3.0825 -18.945,22.638 -12.828,13.501 -10.957,7.0467 -13.176,14.692 -22.203,24.147 -5.2524,-1.22 -6.5894,5.9966 -9.2482,12.134 -5.8918,1.8495 -8.2111,6.5426 -7.9334,4.5623 -8.0959,4.1164 -5.6063,3.3928 -12.836,10.117 -6.3534,5.4974 -5.4069,0.55657 -6.8825,3.9115 -11.708,5.3854 -5.7737,2.4406 -7.8247,6.7318 -7.0931,1.0463 -5.0702,5.1337 -5.8014,1.844 -11.7,8.0043 -5.5416,1.0179 -7.5402,6.7504 -5.3077,1.8396 -7.4239,4.2885 -7.6031,4.9981 -6.3328,1.4233 -6.3589,-1.4211 -6.2742,2.4756 -5.6397,4.6042 -9.3102,3.9624 -9.4536,5.4141 -8.8254,5.1694 -5.1793,-0.74279 -21.279,17.929 -14.908,12.054 -18.959,17.054 -7.2182,4.8852 -8.2951,5.7442 -10.236,7.7754 -5.4994,1.0173 -20.341,20.992 -10.136,6.6799 -7.3345,4.0259 -6.0062,1.2784 -7.2259,3.3411 -5.0269,-2.6807 -6.5479,0.29678 -7.5386,3.8845 -5.0365,5.7014 -10.274,6.7526 -5.1077,2.0576 -5.7292,0.47953 -5.1884,0.20421 -6.3557,0.67861 -9.7687,7.5435 -6.5159,5.3436 -8.5172,4.2415 -9.1802,6.7981 -6.002,0.92695 -5.5204,0.152 -5.0594,2.8214 -5.7077,1.8451 -7.6366,4.2959 -5.8707,7.2029 -5.3054,1.9869 -8.2934,0.14454 -13.394,9.0551 -5.4369,0.61705 From 3383f30451c5e2b667735c8eb7d8ad54cad8a164 Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Tue, 25 Jun 2019 21:15:32 +0530 Subject: [PATCH 03/27] Delete ex1data2.txt --- .../Week 1/jayakar_week1/ex1data2.txt | 47 ------------------- 1 file changed, 47 deletions(-) delete mode 100644 Programming Assignment/Week 1/jayakar_week1/ex1data2.txt diff --git a/Programming Assignment/Week 1/jayakar_week1/ex1data2.txt b/Programming Assignment/Week 1/jayakar_week1/ex1data2.txt deleted file mode 100644 index 79e9a80..0000000 --- a/Programming Assignment/Week 1/jayakar_week1/ex1data2.txt +++ /dev/null @@ -1,47 +0,0 @@ -2104,3,399900 -1600,3,329900 -2400,3,369000 -1416,2,232000 -3000,4,539900 -1985,4,299900 -1534,3,314900 -1427,3,198999 -1380,3,212000 -1494,3,242500 -1940,4,239999 -2000,3,347000 -1890,3,329999 -4478,5,699900 -1268,3,259900 -2300,4,449900 -1320,2,299900 -1236,3,199900 -2609,4,499998 -3031,4,599000 -1767,3,252900 -1888,2,255000 -1604,3,242900 -1962,4,259900 -3890,3,573900 -1100,3,249900 -1458,3,464500 -2526,3,469000 -2200,3,475000 -2637,3,299900 -1839,2,349900 -1000,1,169900 -2040,4,314900 -3137,3,579900 -1811,4,285900 -1437,3,249900 -1239,3,229900 -2132,4,345000 -4215,4,549000 -2162,4,287000 -1664,2,368500 -2238,3,329900 -2567,4,314000 -1200,3,299000 -852,2,179900 -1852,4,299900 -1203,3,239500 From 15e42b6ffff27b095787efe4e327c59223e8feea Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:45:46 +0530 Subject: [PATCH 04/27] Add files via upload --- costFunction.m | 35 ++++++++++ costFunctionReg.m | 32 +++++++++ ex2.m | 151 +++++++++++++++++++++++++++++++++++++++++ ex2_reg.m | 136 +++++++++++++++++++++++++++++++++++++ mapFeature.m | 21 ++++++ plotData.m | 32 +++++++++ plotDecisionBoundary.m | 48 +++++++++++++ predict.m | 28 ++++++++ sigmoid.m | 20 ++++++ 9 files changed, 503 insertions(+) create mode 100644 costFunction.m create mode 100644 costFunctionReg.m create mode 100644 ex2.m create mode 100644 ex2_reg.m create mode 100644 mapFeature.m create mode 100644 plotData.m create mode 100644 plotDecisionBoundary.m create mode 100644 predict.m create mode 100644 sigmoid.m diff --git a/costFunction.m b/costFunction.m new file mode 100644 index 0000000..9f7d711 --- /dev/null +++ b/costFunction.m @@ -0,0 +1,35 @@ +function [J, grad] = costFunction(theta, X, y) +%COSTFUNCTION Compute cost and gradient for logistic regression +% J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the +% parameter for logistic regression and the gradient of the cost +% w.r.t. to the parameters. + +% Initialize some useful values +m = length(y); % number of training examples +% You need to return the following variables correctly +h=X*theta; +h=sigmoid(h); +J = 0; +J=(-1)*(((y')*log(h))+((1-y)'*log(1-h))); +J=J/m; +grad = zeros(size(theta)); +grad=(1/m)*(X'*(h-y)); +% ====================== YOUR CODE HERE ====================== +% Instructions: Compute the cost of a particular choice of theta. +% You should set J to the cost. +% Compute the partial derivatives and set grad to the partial +% derivatives of the cost w.r.t. each parameter in theta +% +% Note: grad should have the same dimensions as theta +% + + + + + + + + +% ============================================================= + +end diff --git a/costFunctionReg.m b/costFunctionReg.m new file mode 100644 index 0000000..86e2e21 --- /dev/null +++ b/costFunctionReg.m @@ -0,0 +1,32 @@ +function [J, grad] = costFunctionReg(theta, X, y, lambda) +%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization +% J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using +% theta as the parameter for regularized logistic regression and the +% gradient of the cost w.r.t. to the parameters. + +% Initialize some useful values +m = length(y); % number of training examples + +% You need to return the following variables correctly +J = 0; +grad = zeros(size(theta)); + +% ====================== YOUR CODE HERE ====================== +% Instructions: Compute the cost of a particular choice of theta. +% You should set J to the cost. +% Compute the partial derivatives and set grad to the partial +% derivatives of the cost w.r.t. each parameter in theta +h=X*theta; +h=sigmoid(h); +J=(-1)*(((y')*log(h))+((1-y)'*log(1-h))); +J=J/m; +k=ones(1,size(theta,1)); +l=theta.^2; +J=J+(lambda/(2*m))*(k*l); +J=J-(lambda/(2*m))*l(1,1); +grad=(1/m)*(X'*(h-y)); +grad=grad+(lambda/m)*theta; +grad(1)=grad(1)-(lambda/m)*theta(1); +% ============================================================= + +end diff --git a/ex2.m b/ex2.m new file mode 100644 index 0000000..103fe11 --- /dev/null +++ b/ex2.m @@ -0,0 +1,151 @@ +%% Machine Learning Online Class - Exercise 2: Logistic Regression +% +% Instructions +% ------------ +% +% This file contains code that helps you get started on the logistic +% regression exercise. You will need to complete the following functions +% in this exericse: +% +% sigmoid.m +% costFunction.m +% predict.m +% costFunctionReg.m +% +% For this exercise, you will not need to change any code in this file, +% or any other files other than those mentioned above. +% + +%% Initialization +clear ; close all; clc + +%% Load Data +% The first two columns contains the exam scores and the third column +% contains the label. + +data = load('ex2data1.txt'); +X = data(:, [1, 2]); y = data(:, 3); + +%% ==================== Part 1: Plotting ==================== +% We start the exercise by first plotting the data to understand the +% the problem we are working with. + +fprintf(['Plotting data with + indicating (y = 1) examples and o ' ... + 'indicating (y = 0) examples.\n']); + +plotData(X, y); + +% Put some labels +hold on; +% Labels and Legend +xlabel('Exam 1 score') +ylabel('Exam 2 score') + +% Specified in plot order +legend('Admitted', 'Not admitted') +hold off; + +fprintf('\nProgram paused. Press enter to continue.\n'); +pause; + + +%% ============ Part 2: Compute Cost and Gradient ============ +% In this part of the exercise, you will implement the cost and gradient +% for logistic regression. You neeed to complete the code in +% costFunction.m + +% Setup the data matrix appropriately, and add ones for the intercept term +[m, n] = size(X); + +% Add intercept term to x and X_test +X = [ones(m, 1) X]; + +% Initialize fitting parameters +initial_theta = zeros(n + 1, 1); + +% Compute and display initial cost and gradient +[cost, grad] = costFunction(initial_theta, X, y); + +fprintf('Cost at initial theta (zeros): %f\n', cost); +fprintf('Expected cost (approx): 0.693\n'); +fprintf('Gradient at initial theta (zeros): \n'); +fprintf(' %f \n', grad); +fprintf('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628\n'); + +% Compute and display cost and gradient with non-zero theta +test_theta = [-24; 0.2; 0.2]; +[cost, grad] = costFunction(test_theta, X, y); + +fprintf('\nCost at test theta: %f\n', cost); +fprintf('Expected cost (approx): 0.218\n'); +fprintf('Gradient at test theta: \n'); +fprintf(' %f \n', grad); +fprintf('Expected gradients (approx):\n 0.043\n 2.566\n 2.647\n'); + +fprintf('\nProgram paused. Press enter to continue.\n'); +pause; + + +%% ============= Part 3: Optimizing using fminunc ============= +% In this exercise, you will use a built-in function (fminunc) to find the +% optimal parameters theta. + +% Set options for fminunc +options = optimset('GradObj', 'on', 'MaxIter', 400); + +% Run fminunc to obtain the optimal theta +% This function will return theta and the cost +[theta, cost] = ... + fminunc(@(t)(costFunction(t, X, y)), initial_theta, options); + +% Print theta to screen +fprintf('Cost at theta found by fminunc: %f\n', cost); +fprintf('Expected cost (approx): 0.203\n'); +fprintf('theta: \n'); +fprintf(' %f \n', theta); +fprintf('Expected theta (approx):\n'); +fprintf(' -25.161\n 0.206\n 0.201\n'); + +% Plot Boundary +plotDecisionBoundary(theta, X, y); + +% Put some labels +hold on; +% Labels and Legend +xlabel('Exam 1 score') +ylabel('Exam 2 score') + +% Specified in plot order +legend('Admitted', 'Not admitted') +hold off; + +fprintf('\nProgram paused. Press enter to continue.\n'); +pause; + +%% ============== Part 4: Predict and Accuracies ============== +% After learning the parameters, you'll like to use it to predict the outcomes +% on unseen data. In this part, you will use the logistic regression model +% to predict the probability that a student with score 45 on exam 1 and +% score 85 on exam 2 will be admitted. +% +% Furthermore, you will compute the training and test set accuracies of +% our model. +% +% Your task is to complete the code in predict.m + +% Predict probability for a student with score 45 on exam 1 +% and score 85 on exam 2 + +prob = sigmoid([1 45 85] * theta); +fprintf(['For a student with scores 45 and 85, we predict an admission ' ... + 'probability of %f\n'], prob); +fprintf('Expected value: 0.775 +/- 0.002\n\n'); + +% Compute accuracy on our training set +p = predict(theta, X); + +fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100); +fprintf('Expected accuracy (approx): 89.0\n'); +fprintf('\n'); + + diff --git a/ex2_reg.m b/ex2_reg.m new file mode 100644 index 0000000..f363318 --- /dev/null +++ b/ex2_reg.m @@ -0,0 +1,136 @@ +%% Machine Learning Online Class - Exercise 2: Logistic Regression +% +% Instructions +% ------------ +% +% This file contains code that helps you get started on the second part +% of the exercise which covers regularization with logistic regression. +% +% You will need to complete the following functions in this exericse: +% +% sigmoid.m +% costFunction.m +% predict.m +% costFunctionReg.m +% +% For this exercise, you will not need to change any code in this file, +% or any other files other than those mentioned above. +% + +%% Initialization +clear ; close all; clc + +%% Load Data +% The first two columns contains the X values and the third column +% contains the label (y). + +data = load('ex2data2.txt'); +X = data(:, [1, 2]); y = data(:, 3); + +plotData(X, y); + +% Put some labels +hold on; + +% Labels and Legend +xlabel('Microchip Test 1') +ylabel('Microchip Test 2') + +% Specified in plot order +legend('y = 1', 'y = 0') +hold off; + + +%% =========== Part 1: Regularized Logistic Regression ============ +% In this part, you are given a dataset with data points that are not +% linearly separable. However, you would still like to use logistic +% regression to classify the data points. +% +% To do so, you introduce more features to use -- in particular, you add +% polynomial features to our data matrix (similar to polynomial +% regression). +% + +% Add Polynomial Features + +% Note that mapFeature also adds a column of ones for us, so the intercept +% term is handled +X = mapFeature(X(:,1), X(:,2)); + +% Initialize fitting parameters +initial_theta = zeros(size(X, 2), 1); + +% Set regularization parameter lambda to 1 +lambda = 1; + +% Compute and display initial cost and gradient for regularized logistic +% regression +[cost, grad] = costFunctionReg(initial_theta, X, y, lambda); + +fprintf('Cost at initial theta (zeros): %f\n', cost); +fprintf('Expected cost (approx): 0.693\n'); +fprintf('Gradient at initial theta (zeros) - first five values only:\n'); +fprintf(' %f \n', grad(1:5)); +fprintf('Expected gradients (approx) - first five values only:\n'); +fprintf(' 0.0085\n 0.0188\n 0.0001\n 0.0503\n 0.0115\n'); + +fprintf('\nProgram paused. Press enter to continue.\n'); +pause; + +% Compute and display cost and gradient +% with all-ones theta and lambda = 10 +test_theta = ones(size(X,2),1); +[cost, grad] = costFunctionReg(test_theta, X, y, 10); + +fprintf('\nCost at test theta (with lambda = 10): %f\n', cost); +fprintf('Expected cost (approx): 3.16\n'); +fprintf('Gradient at test theta - first five values only:\n'); +fprintf(' %f \n', grad(1:5)); +fprintf('Expected gradients (approx) - first five values only:\n'); +fprintf(' 0.3460\n 0.1614\n 0.1948\n 0.2269\n 0.0922\n'); + +fprintf('\nProgram paused. Press enter to continue.\n'); +pause; + +%% ============= Part 2: Regularization and Accuracies ============= +% Optional Exercise: +% In this part, you will get to try different values of lambda and +% see how regularization affects the decision coundart +% +% Try the following values of lambda (0, 1, 10, 100). +% +% How does the decision boundary change when you vary lambda? How does +% the training set accuracy vary? +% + +% Initialize fitting parameters +initial_theta = zeros(size(X, 2), 1); + +% Set regularization parameter lambda to 1 (you should vary this) +lambda = 1; + +% Set Options +options = optimset('GradObj', 'on', 'MaxIter', 400); + +% Optimize +[theta, J, exit_flag] = ... + fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options); + +% Plot Boundary +plotDecisionBoundary(theta, X, y); +hold on; +title(sprintf('lambda = %g', lambda)) + +% Labels and Legend +xlabel('Microchip Test 1') +ylabel('Microchip Test 2') + +legend('y = 1', 'y = 0', 'Decision boundary') +hold off; + +% Compute accuracy on our training set +p = predict(theta, X); + +fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100); +fprintf('Expected accuracy (with lambda = 1): 83.1 (approx)\n'); + diff --git a/mapFeature.m b/mapFeature.m new file mode 100644 index 0000000..d02a72a --- /dev/null +++ b/mapFeature.m @@ -0,0 +1,21 @@ +function out = mapFeature(X1, X2) +% MAPFEATURE Feature mapping function to polynomial features +% +% MAPFEATURE(X1, X2) maps the two input features +% to quadratic features used in the regularization exercise. +% +% Returns a new feature array with more features, comprising of +% X1, X2, X1.^2, X2.^2, X1*X2, X1*X2.^2, etc.. +% +% Inputs X1, X2 must be the same size +% + +degree = 6; +out = ones(size(X1(:,1))); +for i = 1:degree + for j = 0:i + out(:, end+1) = (X1.^(i-j)).*(X2.^j); + end +end + +end \ No newline at end of file diff --git a/plotData.m b/plotData.m new file mode 100644 index 0000000..d8e7f9d --- /dev/null +++ b/plotData.m @@ -0,0 +1,32 @@ +function plotData(X, y) +%PLOTDATA Plots the data points X and y into a new figure +% PLOTDATA(x,y) plots the data points with + for the positive examples +% and o for the negative examples. X is assumed to be a Mx2 matrix. +figure; +pos = find(y==1); neg = find(y == 0); +plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2,'MarkerSize', 7); + hold on; +plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y','MarkerSize', 7); + + +% ====================== YOUR CODE HERE ====================== +% Instructions: Plot the positive and negative examples on a +% 2D plot, using the option 'k+' for the positive +% examples and 'ko' for the negative examples. +% + + + + + + + + + +% ========================================================================= + + + +hold off; + +end diff --git a/plotDecisionBoundary.m b/plotDecisionBoundary.m new file mode 100644 index 0000000..cd36314 --- /dev/null +++ b/plotDecisionBoundary.m @@ -0,0 +1,48 @@ +function plotDecisionBoundary(theta, X, y) +%PLOTDECISIONBOUNDARY Plots the data points X and y into a new figure with +%the decision boundary defined by theta +% PLOTDECISIONBOUNDARY(theta, X,y) plots the data points with + for the +% positive examples and o for the negative examples. X is assumed to be +% a either +% 1) Mx3 matrix, where the first column is an all-ones column for the +% intercept. +% 2) MxN, N>3 matrix, where the first column is all-ones + +% Plot Data +plotData(X(:,2:3), y); +hold on + +if size(X, 2) <= 3 + % Only need 2 points to define a line, so choose two endpoints + plot_x = [min(X(:,2))-2, max(X(:,2))+2]; + + % Calculate the decision boundary line + plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1)); + + % Plot, and adjust axes for better viewing + plot(plot_x, plot_y) + + % Legend, specific for the exercise + legend('Admitted', 'Not admitted', 'Decision Boundary') + axis([30, 100, 30, 100]) +else + % Here is the grid range + u = linspace(-1, 1.5, 50); + v = linspace(-1, 1.5, 50); + + z = zeros(length(u), length(v)); + % Evaluate z = theta*x over the grid + for i = 1:length(u) + for j = 1:length(v) + z(i,j) = mapFeature(u(i), v(j))*theta; + end + end + z = z'; % important to transpose z before calling contour + + % Plot z = 0 + % Notice you need to specify the range [0, 0] + contour(u, v, z, [0, 0], 'LineWidth', 2) +end +hold off + +end diff --git a/predict.m b/predict.m new file mode 100644 index 0000000..c85ac58 --- /dev/null +++ b/predict.m @@ -0,0 +1,28 @@ +function p = predict(theta, X) +%PREDICT Predict whether the label is 0 or 1 using learned logistic +%regression parameters theta +% p = PREDICT(theta, X) computes the predictions for X using a +% threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1) + +m = size(X, 1); % Number of training examples + +% You need to return the following variables correctly +p = zeros(m, 1); +% ====================== YOUR CODE HERE ====================== +% Instructions: Complete the following code to make predictions using +% your learned logistic regression parameters. +% You should set p to a vector of 0's and 1's +% +h=sigmoid(X*theta); +for i=1:m + if h(i)>=0.5 + p(i)=1; + end; + if h(i) < 0.5 + p(i)=0; + end; +end; +% ========================================================================= + + +end diff --git a/sigmoid.m b/sigmoid.m new file mode 100644 index 0000000..e36d8d0 --- /dev/null +++ b/sigmoid.m @@ -0,0 +1,20 @@ +function g = sigmoid(z) +%SIGMOID Compute sigmoid function +% g = SIGMOID(z) computes the sigmoid of z. + +% You need to return the following variables correctly +g = zeros(size(z)); +z= -1*z; +g= 1+exp(z); +g=1./g; +% ====================== YOUR CODE HERE ====================== +% Instructions: Compute the sigmoid of each value of z (z can be a matrix, +% vector or scalar). + + + + + +% ============================================================= + +end From 39e93116db27bbc027a38547975a13cb4a624f1d Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:47:43 +0530 Subject: [PATCH 05/27] Delete costFunction.m --- costFunction.m | 35 ----------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 costFunction.m diff --git a/costFunction.m b/costFunction.m deleted file mode 100644 index 9f7d711..0000000 --- a/costFunction.m +++ /dev/null @@ -1,35 +0,0 @@ -function [J, grad] = costFunction(theta, X, y) -%COSTFUNCTION Compute cost and gradient for logistic regression -% J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the -% parameter for logistic regression and the gradient of the cost -% w.r.t. to the parameters. - -% Initialize some useful values -m = length(y); % number of training examples -% You need to return the following variables correctly -h=X*theta; -h=sigmoid(h); -J = 0; -J=(-1)*(((y')*log(h))+((1-y)'*log(1-h))); -J=J/m; -grad = zeros(size(theta)); -grad=(1/m)*(X'*(h-y)); -% ====================== YOUR CODE HERE ====================== -% Instructions: Compute the cost of a particular choice of theta. -% You should set J to the cost. -% Compute the partial derivatives and set grad to the partial -% derivatives of the cost w.r.t. each parameter in theta -% -% Note: grad should have the same dimensions as theta -% - - - - - - - - -% ============================================================= - -end From f90793bf01ed468afe34e48677be12214ecfff6e Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:47:54 +0530 Subject: [PATCH 06/27] Delete costFunctionReg.m --- costFunctionReg.m | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 costFunctionReg.m diff --git a/costFunctionReg.m b/costFunctionReg.m deleted file mode 100644 index 86e2e21..0000000 --- a/costFunctionReg.m +++ /dev/null @@ -1,32 +0,0 @@ -function [J, grad] = costFunctionReg(theta, X, y, lambda) -%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization -% J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using -% theta as the parameter for regularized logistic regression and the -% gradient of the cost w.r.t. to the parameters. - -% Initialize some useful values -m = length(y); % number of training examples - -% You need to return the following variables correctly -J = 0; -grad = zeros(size(theta)); - -% ====================== YOUR CODE HERE ====================== -% Instructions: Compute the cost of a particular choice of theta. -% You should set J to the cost. -% Compute the partial derivatives and set grad to the partial -% derivatives of the cost w.r.t. each parameter in theta -h=X*theta; -h=sigmoid(h); -J=(-1)*(((y')*log(h))+((1-y)'*log(1-h))); -J=J/m; -k=ones(1,size(theta,1)); -l=theta.^2; -J=J+(lambda/(2*m))*(k*l); -J=J-(lambda/(2*m))*l(1,1); -grad=(1/m)*(X'*(h-y)); -grad=grad+(lambda/m)*theta; -grad(1)=grad(1)-(lambda/m)*theta(1); -% ============================================================= - -end From d8a47c064c3ab6a34460b3a230b1149b8c94ad4e Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:48:10 +0530 Subject: [PATCH 07/27] Delete ex2.m --- ex2.m | 151 ---------------------------------------------------------- 1 file changed, 151 deletions(-) delete mode 100644 ex2.m diff --git a/ex2.m b/ex2.m deleted file mode 100644 index 103fe11..0000000 --- a/ex2.m +++ /dev/null @@ -1,151 +0,0 @@ -%% Machine Learning Online Class - Exercise 2: Logistic Regression -% -% Instructions -% ------------ -% -% This file contains code that helps you get started on the logistic -% regression exercise. You will need to complete the following functions -% in this exericse: -% -% sigmoid.m -% costFunction.m -% predict.m -% costFunctionReg.m -% -% For this exercise, you will not need to change any code in this file, -% or any other files other than those mentioned above. -% - -%% Initialization -clear ; close all; clc - -%% Load Data -% The first two columns contains the exam scores and the third column -% contains the label. - -data = load('ex2data1.txt'); -X = data(:, [1, 2]); y = data(:, 3); - -%% ==================== Part 1: Plotting ==================== -% We start the exercise by first plotting the data to understand the -% the problem we are working with. - -fprintf(['Plotting data with + indicating (y = 1) examples and o ' ... - 'indicating (y = 0) examples.\n']); - -plotData(X, y); - -% Put some labels -hold on; -% Labels and Legend -xlabel('Exam 1 score') -ylabel('Exam 2 score') - -% Specified in plot order -legend('Admitted', 'Not admitted') -hold off; - -fprintf('\nProgram paused. Press enter to continue.\n'); -pause; - - -%% ============ Part 2: Compute Cost and Gradient ============ -% In this part of the exercise, you will implement the cost and gradient -% for logistic regression. You neeed to complete the code in -% costFunction.m - -% Setup the data matrix appropriately, and add ones for the intercept term -[m, n] = size(X); - -% Add intercept term to x and X_test -X = [ones(m, 1) X]; - -% Initialize fitting parameters -initial_theta = zeros(n + 1, 1); - -% Compute and display initial cost and gradient -[cost, grad] = costFunction(initial_theta, X, y); - -fprintf('Cost at initial theta (zeros): %f\n', cost); -fprintf('Expected cost (approx): 0.693\n'); -fprintf('Gradient at initial theta (zeros): \n'); -fprintf(' %f \n', grad); -fprintf('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628\n'); - -% Compute and display cost and gradient with non-zero theta -test_theta = [-24; 0.2; 0.2]; -[cost, grad] = costFunction(test_theta, X, y); - -fprintf('\nCost at test theta: %f\n', cost); -fprintf('Expected cost (approx): 0.218\n'); -fprintf('Gradient at test theta: \n'); -fprintf(' %f \n', grad); -fprintf('Expected gradients (approx):\n 0.043\n 2.566\n 2.647\n'); - -fprintf('\nProgram paused. Press enter to continue.\n'); -pause; - - -%% ============= Part 3: Optimizing using fminunc ============= -% In this exercise, you will use a built-in function (fminunc) to find the -% optimal parameters theta. - -% Set options for fminunc -options = optimset('GradObj', 'on', 'MaxIter', 400); - -% Run fminunc to obtain the optimal theta -% This function will return theta and the cost -[theta, cost] = ... - fminunc(@(t)(costFunction(t, X, y)), initial_theta, options); - -% Print theta to screen -fprintf('Cost at theta found by fminunc: %f\n', cost); -fprintf('Expected cost (approx): 0.203\n'); -fprintf('theta: \n'); -fprintf(' %f \n', theta); -fprintf('Expected theta (approx):\n'); -fprintf(' -25.161\n 0.206\n 0.201\n'); - -% Plot Boundary -plotDecisionBoundary(theta, X, y); - -% Put some labels -hold on; -% Labels and Legend -xlabel('Exam 1 score') -ylabel('Exam 2 score') - -% Specified in plot order -legend('Admitted', 'Not admitted') -hold off; - -fprintf('\nProgram paused. Press enter to continue.\n'); -pause; - -%% ============== Part 4: Predict and Accuracies ============== -% After learning the parameters, you'll like to use it to predict the outcomes -% on unseen data. In this part, you will use the logistic regression model -% to predict the probability that a student with score 45 on exam 1 and -% score 85 on exam 2 will be admitted. -% -% Furthermore, you will compute the training and test set accuracies of -% our model. -% -% Your task is to complete the code in predict.m - -% Predict probability for a student with score 45 on exam 1 -% and score 85 on exam 2 - -prob = sigmoid([1 45 85] * theta); -fprintf(['For a student with scores 45 and 85, we predict an admission ' ... - 'probability of %f\n'], prob); -fprintf('Expected value: 0.775 +/- 0.002\n\n'); - -% Compute accuracy on our training set -p = predict(theta, X); - -fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100); -fprintf('Expected accuracy (approx): 89.0\n'); -fprintf('\n'); - - From a7476804c749c3f136094d9da8cae6ac7a0e387e Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:48:19 +0530 Subject: [PATCH 08/27] Delete ex2_reg.m --- ex2_reg.m | 136 ------------------------------------------------------ 1 file changed, 136 deletions(-) delete mode 100644 ex2_reg.m diff --git a/ex2_reg.m b/ex2_reg.m deleted file mode 100644 index f363318..0000000 --- a/ex2_reg.m +++ /dev/null @@ -1,136 +0,0 @@ -%% Machine Learning Online Class - Exercise 2: Logistic Regression -% -% Instructions -% ------------ -% -% This file contains code that helps you get started on the second part -% of the exercise which covers regularization with logistic regression. -% -% You will need to complete the following functions in this exericse: -% -% sigmoid.m -% costFunction.m -% predict.m -% costFunctionReg.m -% -% For this exercise, you will not need to change any code in this file, -% or any other files other than those mentioned above. -% - -%% Initialization -clear ; close all; clc - -%% Load Data -% The first two columns contains the X values and the third column -% contains the label (y). - -data = load('ex2data2.txt'); -X = data(:, [1, 2]); y = data(:, 3); - -plotData(X, y); - -% Put some labels -hold on; - -% Labels and Legend -xlabel('Microchip Test 1') -ylabel('Microchip Test 2') - -% Specified in plot order -legend('y = 1', 'y = 0') -hold off; - - -%% =========== Part 1: Regularized Logistic Regression ============ -% In this part, you are given a dataset with data points that are not -% linearly separable. However, you would still like to use logistic -% regression to classify the data points. -% -% To do so, you introduce more features to use -- in particular, you add -% polynomial features to our data matrix (similar to polynomial -% regression). -% - -% Add Polynomial Features - -% Note that mapFeature also adds a column of ones for us, so the intercept -% term is handled -X = mapFeature(X(:,1), X(:,2)); - -% Initialize fitting parameters -initial_theta = zeros(size(X, 2), 1); - -% Set regularization parameter lambda to 1 -lambda = 1; - -% Compute and display initial cost and gradient for regularized logistic -% regression -[cost, grad] = costFunctionReg(initial_theta, X, y, lambda); - -fprintf('Cost at initial theta (zeros): %f\n', cost); -fprintf('Expected cost (approx): 0.693\n'); -fprintf('Gradient at initial theta (zeros) - first five values only:\n'); -fprintf(' %f \n', grad(1:5)); -fprintf('Expected gradients (approx) - first five values only:\n'); -fprintf(' 0.0085\n 0.0188\n 0.0001\n 0.0503\n 0.0115\n'); - -fprintf('\nProgram paused. Press enter to continue.\n'); -pause; - -% Compute and display cost and gradient -% with all-ones theta and lambda = 10 -test_theta = ones(size(X,2),1); -[cost, grad] = costFunctionReg(test_theta, X, y, 10); - -fprintf('\nCost at test theta (with lambda = 10): %f\n', cost); -fprintf('Expected cost (approx): 3.16\n'); -fprintf('Gradient at test theta - first five values only:\n'); -fprintf(' %f \n', grad(1:5)); -fprintf('Expected gradients (approx) - first five values only:\n'); -fprintf(' 0.3460\n 0.1614\n 0.1948\n 0.2269\n 0.0922\n'); - -fprintf('\nProgram paused. Press enter to continue.\n'); -pause; - -%% ============= Part 2: Regularization and Accuracies ============= -% Optional Exercise: -% In this part, you will get to try different values of lambda and -% see how regularization affects the decision coundart -% -% Try the following values of lambda (0, 1, 10, 100). -% -% How does the decision boundary change when you vary lambda? How does -% the training set accuracy vary? -% - -% Initialize fitting parameters -initial_theta = zeros(size(X, 2), 1); - -% Set regularization parameter lambda to 1 (you should vary this) -lambda = 1; - -% Set Options -options = optimset('GradObj', 'on', 'MaxIter', 400); - -% Optimize -[theta, J, exit_flag] = ... - fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options); - -% Plot Boundary -plotDecisionBoundary(theta, X, y); -hold on; -title(sprintf('lambda = %g', lambda)) - -% Labels and Legend -xlabel('Microchip Test 1') -ylabel('Microchip Test 2') - -legend('y = 1', 'y = 0', 'Decision boundary') -hold off; - -% Compute accuracy on our training set -p = predict(theta, X); - -fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100); -fprintf('Expected accuracy (with lambda = 1): 83.1 (approx)\n'); - From 735b8e8235452e5a9a4fc9b13132610af3d5c5f5 Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:48:30 +0530 Subject: [PATCH 09/27] Delete mapFeature.m --- mapFeature.m | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 mapFeature.m diff --git a/mapFeature.m b/mapFeature.m deleted file mode 100644 index d02a72a..0000000 --- a/mapFeature.m +++ /dev/null @@ -1,21 +0,0 @@ -function out = mapFeature(X1, X2) -% MAPFEATURE Feature mapping function to polynomial features -% -% MAPFEATURE(X1, X2) maps the two input features -% to quadratic features used in the regularization exercise. -% -% Returns a new feature array with more features, comprising of -% X1, X2, X1.^2, X2.^2, X1*X2, X1*X2.^2, etc.. -% -% Inputs X1, X2 must be the same size -% - -degree = 6; -out = ones(size(X1(:,1))); -for i = 1:degree - for j = 0:i - out(:, end+1) = (X1.^(i-j)).*(X2.^j); - end -end - -end \ No newline at end of file From 68430037ed996dd6eb800901f2a7459bc0f204a0 Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:48:39 +0530 Subject: [PATCH 10/27] Delete plotData.m --- plotData.m | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 plotData.m diff --git a/plotData.m b/plotData.m deleted file mode 100644 index d8e7f9d..0000000 --- a/plotData.m +++ /dev/null @@ -1,32 +0,0 @@ -function plotData(X, y) -%PLOTDATA Plots the data points X and y into a new figure -% PLOTDATA(x,y) plots the data points with + for the positive examples -% and o for the negative examples. X is assumed to be a Mx2 matrix. -figure; -pos = find(y==1); neg = find(y == 0); -plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2,'MarkerSize', 7); - hold on; -plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y','MarkerSize', 7); - - -% ====================== YOUR CODE HERE ====================== -% Instructions: Plot the positive and negative examples on a -% 2D plot, using the option 'k+' for the positive -% examples and 'ko' for the negative examples. -% - - - - - - - - - -% ========================================================================= - - - -hold off; - -end From e0f746ed029f03d2e0e34942d30b7414882d65b0 Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:48:51 +0530 Subject: [PATCH 11/27] Delete plotDecisionBoundary.m --- plotDecisionBoundary.m | 48 ------------------------------------------ 1 file changed, 48 deletions(-) delete mode 100644 plotDecisionBoundary.m diff --git a/plotDecisionBoundary.m b/plotDecisionBoundary.m deleted file mode 100644 index cd36314..0000000 --- a/plotDecisionBoundary.m +++ /dev/null @@ -1,48 +0,0 @@ -function plotDecisionBoundary(theta, X, y) -%PLOTDECISIONBOUNDARY Plots the data points X and y into a new figure with -%the decision boundary defined by theta -% PLOTDECISIONBOUNDARY(theta, X,y) plots the data points with + for the -% positive examples and o for the negative examples. X is assumed to be -% a either -% 1) Mx3 matrix, where the first column is an all-ones column for the -% intercept. -% 2) MxN, N>3 matrix, where the first column is all-ones - -% Plot Data -plotData(X(:,2:3), y); -hold on - -if size(X, 2) <= 3 - % Only need 2 points to define a line, so choose two endpoints - plot_x = [min(X(:,2))-2, max(X(:,2))+2]; - - % Calculate the decision boundary line - plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1)); - - % Plot, and adjust axes for better viewing - plot(plot_x, plot_y) - - % Legend, specific for the exercise - legend('Admitted', 'Not admitted', 'Decision Boundary') - axis([30, 100, 30, 100]) -else - % Here is the grid range - u = linspace(-1, 1.5, 50); - v = linspace(-1, 1.5, 50); - - z = zeros(length(u), length(v)); - % Evaluate z = theta*x over the grid - for i = 1:length(u) - for j = 1:length(v) - z(i,j) = mapFeature(u(i), v(j))*theta; - end - end - z = z'; % important to transpose z before calling contour - - % Plot z = 0 - % Notice you need to specify the range [0, 0] - contour(u, v, z, [0, 0], 'LineWidth', 2) -end -hold off - -end From c6d6e668292fb9c522ff20267541d87c9d5ad239 Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:49:01 +0530 Subject: [PATCH 12/27] Delete predict.m --- predict.m | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 predict.m diff --git a/predict.m b/predict.m deleted file mode 100644 index c85ac58..0000000 --- a/predict.m +++ /dev/null @@ -1,28 +0,0 @@ -function p = predict(theta, X) -%PREDICT Predict whether the label is 0 or 1 using learned logistic -%regression parameters theta -% p = PREDICT(theta, X) computes the predictions for X using a -% threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1) - -m = size(X, 1); % Number of training examples - -% You need to return the following variables correctly -p = zeros(m, 1); -% ====================== YOUR CODE HERE ====================== -% Instructions: Complete the following code to make predictions using -% your learned logistic regression parameters. -% You should set p to a vector of 0's and 1's -% -h=sigmoid(X*theta); -for i=1:m - if h(i)>=0.5 - p(i)=1; - end; - if h(i) < 0.5 - p(i)=0; - end; -end; -% ========================================================================= - - -end From ac157f30d1eb3ea4c6ba1a89e64ce4fefa76b691 Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:49:13 +0530 Subject: [PATCH 13/27] Delete sigmoid.m --- sigmoid.m | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 sigmoid.m diff --git a/sigmoid.m b/sigmoid.m deleted file mode 100644 index e36d8d0..0000000 --- a/sigmoid.m +++ /dev/null @@ -1,20 +0,0 @@ -function g = sigmoid(z) -%SIGMOID Compute sigmoid function -% g = SIGMOID(z) computes the sigmoid of z. - -% You need to return the following variables correctly -g = zeros(size(z)); -z= -1*z; -g= 1+exp(z); -g=1./g; -% ====================== YOUR CODE HERE ====================== -% Instructions: Compute the sigmoid of each value of z (z can be a matrix, -% vector or scalar). - - - - - -% ============================================================= - -end From f1b7cc2ab788b7faaae88178e07164bae9a57ab8 Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:50:12 +0530 Subject: [PATCH 14/27] Create Jayakar_week2 --- Programming Assignment/Week_2/Jayakar_week2 | 1 + 1 file changed, 1 insertion(+) create mode 100644 Programming Assignment/Week_2/Jayakar_week2 diff --git a/Programming Assignment/Week_2/Jayakar_week2 b/Programming Assignment/Week_2/Jayakar_week2 new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Programming Assignment/Week_2/Jayakar_week2 @@ -0,0 +1 @@ + From 5f0b9e7c3a1bb4b61840565e5ac711d6d04aaa86 Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:50:28 +0530 Subject: [PATCH 15/27] Delete Jayakar_week2 --- Programming Assignment/Week_2/Jayakar_week2 | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Programming Assignment/Week_2/Jayakar_week2 diff --git a/Programming Assignment/Week_2/Jayakar_week2 b/Programming Assignment/Week_2/Jayakar_week2 deleted file mode 100644 index 8b13789..0000000 --- a/Programming Assignment/Week_2/Jayakar_week2 +++ /dev/null @@ -1 +0,0 @@ - From 01c398a5462b8b1b1c1ce200b8af1b1a26e6722c Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:56:13 +0530 Subject: [PATCH 16/27] Add files via upload --- .../Week 1/jayakar_week2/costFunction.m | 35 ++++ .../Week 1/jayakar_week2/costFunctionReg.m | 32 ++++ .../Week 1/jayakar_week2/ex2.m | 151 ++++++++++++++++++ .../Week 1/jayakar_week2/ex2_reg.m | 136 ++++++++++++++++ .../Week 1/jayakar_week2/mapFeature.m | 21 +++ .../Week 1/jayakar_week2/plotData.m | 32 ++++ .../jayakar_week2/plotDecisionBoundary.m | 48 ++++++ .../Week 1/jayakar_week2/predict.m | 28 ++++ .../Week 1/jayakar_week2/sigmoid.m | 20 +++ 9 files changed, 503 insertions(+) create mode 100644 Programming Assignment/Week 1/jayakar_week2/costFunction.m create mode 100644 Programming Assignment/Week 1/jayakar_week2/costFunctionReg.m create mode 100644 Programming Assignment/Week 1/jayakar_week2/ex2.m create mode 100644 Programming Assignment/Week 1/jayakar_week2/ex2_reg.m create mode 100644 Programming Assignment/Week 1/jayakar_week2/mapFeature.m create mode 100644 Programming Assignment/Week 1/jayakar_week2/plotData.m create mode 100644 Programming Assignment/Week 1/jayakar_week2/plotDecisionBoundary.m create mode 100644 Programming Assignment/Week 1/jayakar_week2/predict.m create mode 100644 Programming Assignment/Week 1/jayakar_week2/sigmoid.m diff --git a/Programming Assignment/Week 1/jayakar_week2/costFunction.m b/Programming Assignment/Week 1/jayakar_week2/costFunction.m new file mode 100644 index 0000000..9f7d711 --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week2/costFunction.m @@ -0,0 +1,35 @@ +function [J, grad] = costFunction(theta, X, y) +%COSTFUNCTION Compute cost and gradient for logistic regression +% J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the +% parameter for logistic regression and the gradient of the cost +% w.r.t. to the parameters. + +% Initialize some useful values +m = length(y); % number of training examples +% You need to return the following variables correctly +h=X*theta; +h=sigmoid(h); +J = 0; +J=(-1)*(((y')*log(h))+((1-y)'*log(1-h))); +J=J/m; +grad = zeros(size(theta)); +grad=(1/m)*(X'*(h-y)); +% ====================== YOUR CODE HERE ====================== +% Instructions: Compute the cost of a particular choice of theta. +% You should set J to the cost. +% Compute the partial derivatives and set grad to the partial +% derivatives of the cost w.r.t. each parameter in theta +% +% Note: grad should have the same dimensions as theta +% + + + + + + + + +% ============================================================= + +end diff --git a/Programming Assignment/Week 1/jayakar_week2/costFunctionReg.m b/Programming Assignment/Week 1/jayakar_week2/costFunctionReg.m new file mode 100644 index 0000000..86e2e21 --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week2/costFunctionReg.m @@ -0,0 +1,32 @@ +function [J, grad] = costFunctionReg(theta, X, y, lambda) +%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization +% J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using +% theta as the parameter for regularized logistic regression and the +% gradient of the cost w.r.t. to the parameters. + +% Initialize some useful values +m = length(y); % number of training examples + +% You need to return the following variables correctly +J = 0; +grad = zeros(size(theta)); + +% ====================== YOUR CODE HERE ====================== +% Instructions: Compute the cost of a particular choice of theta. +% You should set J to the cost. +% Compute the partial derivatives and set grad to the partial +% derivatives of the cost w.r.t. each parameter in theta +h=X*theta; +h=sigmoid(h); +J=(-1)*(((y')*log(h))+((1-y)'*log(1-h))); +J=J/m; +k=ones(1,size(theta,1)); +l=theta.^2; +J=J+(lambda/(2*m))*(k*l); +J=J-(lambda/(2*m))*l(1,1); +grad=(1/m)*(X'*(h-y)); +grad=grad+(lambda/m)*theta; +grad(1)=grad(1)-(lambda/m)*theta(1); +% ============================================================= + +end diff --git a/Programming Assignment/Week 1/jayakar_week2/ex2.m b/Programming Assignment/Week 1/jayakar_week2/ex2.m new file mode 100644 index 0000000..103fe11 --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week2/ex2.m @@ -0,0 +1,151 @@ +%% Machine Learning Online Class - Exercise 2: Logistic Regression +% +% Instructions +% ------------ +% +% This file contains code that helps you get started on the logistic +% regression exercise. You will need to complete the following functions +% in this exericse: +% +% sigmoid.m +% costFunction.m +% predict.m +% costFunctionReg.m +% +% For this exercise, you will not need to change any code in this file, +% or any other files other than those mentioned above. +% + +%% Initialization +clear ; close all; clc + +%% Load Data +% The first two columns contains the exam scores and the third column +% contains the label. + +data = load('ex2data1.txt'); +X = data(:, [1, 2]); y = data(:, 3); + +%% ==================== Part 1: Plotting ==================== +% We start the exercise by first plotting the data to understand the +% the problem we are working with. + +fprintf(['Plotting data with + indicating (y = 1) examples and o ' ... + 'indicating (y = 0) examples.\n']); + +plotData(X, y); + +% Put some labels +hold on; +% Labels and Legend +xlabel('Exam 1 score') +ylabel('Exam 2 score') + +% Specified in plot order +legend('Admitted', 'Not admitted') +hold off; + +fprintf('\nProgram paused. Press enter to continue.\n'); +pause; + + +%% ============ Part 2: Compute Cost and Gradient ============ +% In this part of the exercise, you will implement the cost and gradient +% for logistic regression. You neeed to complete the code in +% costFunction.m + +% Setup the data matrix appropriately, and add ones for the intercept term +[m, n] = size(X); + +% Add intercept term to x and X_test +X = [ones(m, 1) X]; + +% Initialize fitting parameters +initial_theta = zeros(n + 1, 1); + +% Compute and display initial cost and gradient +[cost, grad] = costFunction(initial_theta, X, y); + +fprintf('Cost at initial theta (zeros): %f\n', cost); +fprintf('Expected cost (approx): 0.693\n'); +fprintf('Gradient at initial theta (zeros): \n'); +fprintf(' %f \n', grad); +fprintf('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628\n'); + +% Compute and display cost and gradient with non-zero theta +test_theta = [-24; 0.2; 0.2]; +[cost, grad] = costFunction(test_theta, X, y); + +fprintf('\nCost at test theta: %f\n', cost); +fprintf('Expected cost (approx): 0.218\n'); +fprintf('Gradient at test theta: \n'); +fprintf(' %f \n', grad); +fprintf('Expected gradients (approx):\n 0.043\n 2.566\n 2.647\n'); + +fprintf('\nProgram paused. Press enter to continue.\n'); +pause; + + +%% ============= Part 3: Optimizing using fminunc ============= +% In this exercise, you will use a built-in function (fminunc) to find the +% optimal parameters theta. + +% Set options for fminunc +options = optimset('GradObj', 'on', 'MaxIter', 400); + +% Run fminunc to obtain the optimal theta +% This function will return theta and the cost +[theta, cost] = ... + fminunc(@(t)(costFunction(t, X, y)), initial_theta, options); + +% Print theta to screen +fprintf('Cost at theta found by fminunc: %f\n', cost); +fprintf('Expected cost (approx): 0.203\n'); +fprintf('theta: \n'); +fprintf(' %f \n', theta); +fprintf('Expected theta (approx):\n'); +fprintf(' -25.161\n 0.206\n 0.201\n'); + +% Plot Boundary +plotDecisionBoundary(theta, X, y); + +% Put some labels +hold on; +% Labels and Legend +xlabel('Exam 1 score') +ylabel('Exam 2 score') + +% Specified in plot order +legend('Admitted', 'Not admitted') +hold off; + +fprintf('\nProgram paused. Press enter to continue.\n'); +pause; + +%% ============== Part 4: Predict and Accuracies ============== +% After learning the parameters, you'll like to use it to predict the outcomes +% on unseen data. In this part, you will use the logistic regression model +% to predict the probability that a student with score 45 on exam 1 and +% score 85 on exam 2 will be admitted. +% +% Furthermore, you will compute the training and test set accuracies of +% our model. +% +% Your task is to complete the code in predict.m + +% Predict probability for a student with score 45 on exam 1 +% and score 85 on exam 2 + +prob = sigmoid([1 45 85] * theta); +fprintf(['For a student with scores 45 and 85, we predict an admission ' ... + 'probability of %f\n'], prob); +fprintf('Expected value: 0.775 +/- 0.002\n\n'); + +% Compute accuracy on our training set +p = predict(theta, X); + +fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100); +fprintf('Expected accuracy (approx): 89.0\n'); +fprintf('\n'); + + diff --git a/Programming Assignment/Week 1/jayakar_week2/ex2_reg.m b/Programming Assignment/Week 1/jayakar_week2/ex2_reg.m new file mode 100644 index 0000000..f363318 --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week2/ex2_reg.m @@ -0,0 +1,136 @@ +%% Machine Learning Online Class - Exercise 2: Logistic Regression +% +% Instructions +% ------------ +% +% This file contains code that helps you get started on the second part +% of the exercise which covers regularization with logistic regression. +% +% You will need to complete the following functions in this exericse: +% +% sigmoid.m +% costFunction.m +% predict.m +% costFunctionReg.m +% +% For this exercise, you will not need to change any code in this file, +% or any other files other than those mentioned above. +% + +%% Initialization +clear ; close all; clc + +%% Load Data +% The first two columns contains the X values and the third column +% contains the label (y). + +data = load('ex2data2.txt'); +X = data(:, [1, 2]); y = data(:, 3); + +plotData(X, y); + +% Put some labels +hold on; + +% Labels and Legend +xlabel('Microchip Test 1') +ylabel('Microchip Test 2') + +% Specified in plot order +legend('y = 1', 'y = 0') +hold off; + + +%% =========== Part 1: Regularized Logistic Regression ============ +% In this part, you are given a dataset with data points that are not +% linearly separable. However, you would still like to use logistic +% regression to classify the data points. +% +% To do so, you introduce more features to use -- in particular, you add +% polynomial features to our data matrix (similar to polynomial +% regression). +% + +% Add Polynomial Features + +% Note that mapFeature also adds a column of ones for us, so the intercept +% term is handled +X = mapFeature(X(:,1), X(:,2)); + +% Initialize fitting parameters +initial_theta = zeros(size(X, 2), 1); + +% Set regularization parameter lambda to 1 +lambda = 1; + +% Compute and display initial cost and gradient for regularized logistic +% regression +[cost, grad] = costFunctionReg(initial_theta, X, y, lambda); + +fprintf('Cost at initial theta (zeros): %f\n', cost); +fprintf('Expected cost (approx): 0.693\n'); +fprintf('Gradient at initial theta (zeros) - first five values only:\n'); +fprintf(' %f \n', grad(1:5)); +fprintf('Expected gradients (approx) - first five values only:\n'); +fprintf(' 0.0085\n 0.0188\n 0.0001\n 0.0503\n 0.0115\n'); + +fprintf('\nProgram paused. Press enter to continue.\n'); +pause; + +% Compute and display cost and gradient +% with all-ones theta and lambda = 10 +test_theta = ones(size(X,2),1); +[cost, grad] = costFunctionReg(test_theta, X, y, 10); + +fprintf('\nCost at test theta (with lambda = 10): %f\n', cost); +fprintf('Expected cost (approx): 3.16\n'); +fprintf('Gradient at test theta - first five values only:\n'); +fprintf(' %f \n', grad(1:5)); +fprintf('Expected gradients (approx) - first five values only:\n'); +fprintf(' 0.3460\n 0.1614\n 0.1948\n 0.2269\n 0.0922\n'); + +fprintf('\nProgram paused. Press enter to continue.\n'); +pause; + +%% ============= Part 2: Regularization and Accuracies ============= +% Optional Exercise: +% In this part, you will get to try different values of lambda and +% see how regularization affects the decision coundart +% +% Try the following values of lambda (0, 1, 10, 100). +% +% How does the decision boundary change when you vary lambda? How does +% the training set accuracy vary? +% + +% Initialize fitting parameters +initial_theta = zeros(size(X, 2), 1); + +% Set regularization parameter lambda to 1 (you should vary this) +lambda = 1; + +% Set Options +options = optimset('GradObj', 'on', 'MaxIter', 400); + +% Optimize +[theta, J, exit_flag] = ... + fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options); + +% Plot Boundary +plotDecisionBoundary(theta, X, y); +hold on; +title(sprintf('lambda = %g', lambda)) + +% Labels and Legend +xlabel('Microchip Test 1') +ylabel('Microchip Test 2') + +legend('y = 1', 'y = 0', 'Decision boundary') +hold off; + +% Compute accuracy on our training set +p = predict(theta, X); + +fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100); +fprintf('Expected accuracy (with lambda = 1): 83.1 (approx)\n'); + diff --git a/Programming Assignment/Week 1/jayakar_week2/mapFeature.m b/Programming Assignment/Week 1/jayakar_week2/mapFeature.m new file mode 100644 index 0000000..d02a72a --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week2/mapFeature.m @@ -0,0 +1,21 @@ +function out = mapFeature(X1, X2) +% MAPFEATURE Feature mapping function to polynomial features +% +% MAPFEATURE(X1, X2) maps the two input features +% to quadratic features used in the regularization exercise. +% +% Returns a new feature array with more features, comprising of +% X1, X2, X1.^2, X2.^2, X1*X2, X1*X2.^2, etc.. +% +% Inputs X1, X2 must be the same size +% + +degree = 6; +out = ones(size(X1(:,1))); +for i = 1:degree + for j = 0:i + out(:, end+1) = (X1.^(i-j)).*(X2.^j); + end +end + +end \ No newline at end of file diff --git a/Programming Assignment/Week 1/jayakar_week2/plotData.m b/Programming Assignment/Week 1/jayakar_week2/plotData.m new file mode 100644 index 0000000..d8e7f9d --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week2/plotData.m @@ -0,0 +1,32 @@ +function plotData(X, y) +%PLOTDATA Plots the data points X and y into a new figure +% PLOTDATA(x,y) plots the data points with + for the positive examples +% and o for the negative examples. X is assumed to be a Mx2 matrix. +figure; +pos = find(y==1); neg = find(y == 0); +plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2,'MarkerSize', 7); + hold on; +plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y','MarkerSize', 7); + + +% ====================== YOUR CODE HERE ====================== +% Instructions: Plot the positive and negative examples on a +% 2D plot, using the option 'k+' for the positive +% examples and 'ko' for the negative examples. +% + + + + + + + + + +% ========================================================================= + + + +hold off; + +end diff --git a/Programming Assignment/Week 1/jayakar_week2/plotDecisionBoundary.m b/Programming Assignment/Week 1/jayakar_week2/plotDecisionBoundary.m new file mode 100644 index 0000000..cd36314 --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week2/plotDecisionBoundary.m @@ -0,0 +1,48 @@ +function plotDecisionBoundary(theta, X, y) +%PLOTDECISIONBOUNDARY Plots the data points X and y into a new figure with +%the decision boundary defined by theta +% PLOTDECISIONBOUNDARY(theta, X,y) plots the data points with + for the +% positive examples and o for the negative examples. X is assumed to be +% a either +% 1) Mx3 matrix, where the first column is an all-ones column for the +% intercept. +% 2) MxN, N>3 matrix, where the first column is all-ones + +% Plot Data +plotData(X(:,2:3), y); +hold on + +if size(X, 2) <= 3 + % Only need 2 points to define a line, so choose two endpoints + plot_x = [min(X(:,2))-2, max(X(:,2))+2]; + + % Calculate the decision boundary line + plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1)); + + % Plot, and adjust axes for better viewing + plot(plot_x, plot_y) + + % Legend, specific for the exercise + legend('Admitted', 'Not admitted', 'Decision Boundary') + axis([30, 100, 30, 100]) +else + % Here is the grid range + u = linspace(-1, 1.5, 50); + v = linspace(-1, 1.5, 50); + + z = zeros(length(u), length(v)); + % Evaluate z = theta*x over the grid + for i = 1:length(u) + for j = 1:length(v) + z(i,j) = mapFeature(u(i), v(j))*theta; + end + end + z = z'; % important to transpose z before calling contour + + % Plot z = 0 + % Notice you need to specify the range [0, 0] + contour(u, v, z, [0, 0], 'LineWidth', 2) +end +hold off + +end diff --git a/Programming Assignment/Week 1/jayakar_week2/predict.m b/Programming Assignment/Week 1/jayakar_week2/predict.m new file mode 100644 index 0000000..c85ac58 --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week2/predict.m @@ -0,0 +1,28 @@ +function p = predict(theta, X) +%PREDICT Predict whether the label is 0 or 1 using learned logistic +%regression parameters theta +% p = PREDICT(theta, X) computes the predictions for X using a +% threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1) + +m = size(X, 1); % Number of training examples + +% You need to return the following variables correctly +p = zeros(m, 1); +% ====================== YOUR CODE HERE ====================== +% Instructions: Complete the following code to make predictions using +% your learned logistic regression parameters. +% You should set p to a vector of 0's and 1's +% +h=sigmoid(X*theta); +for i=1:m + if h(i)>=0.5 + p(i)=1; + end; + if h(i) < 0.5 + p(i)=0; + end; +end; +% ========================================================================= + + +end diff --git a/Programming Assignment/Week 1/jayakar_week2/sigmoid.m b/Programming Assignment/Week 1/jayakar_week2/sigmoid.m new file mode 100644 index 0000000..e36d8d0 --- /dev/null +++ b/Programming Assignment/Week 1/jayakar_week2/sigmoid.m @@ -0,0 +1,20 @@ +function g = sigmoid(z) +%SIGMOID Compute sigmoid function +% g = SIGMOID(z) computes the sigmoid of z. + +% You need to return the following variables correctly +g = zeros(size(z)); +z= -1*z; +g= 1+exp(z); +g=1./g; +% ====================== YOUR CODE HERE ====================== +% Instructions: Compute the sigmoid of each value of z (z can be a matrix, +% vector or scalar). + + + + + +% ============================================================= + +end From d9188afae6d00ced49051b3aff294aff747e9e85 Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:57:52 +0530 Subject: [PATCH 17/27] Add files via upload --- .../Week_2/jayakar_week2/costFunction.m | 35 ++++ .../Week_2/jayakar_week2/costFunctionReg.m | 32 ++++ .../Week_2/jayakar_week2/ex2.m | 151 ++++++++++++++++++ .../Week_2/jayakar_week2/ex2_reg.m | 136 ++++++++++++++++ .../Week_2/jayakar_week2/mapFeature.m | 21 +++ .../Week_2/jayakar_week2/plotData.m | 32 ++++ .../jayakar_week2/plotDecisionBoundary.m | 48 ++++++ .../Week_2/jayakar_week2/predict.m | 28 ++++ .../Week_2/jayakar_week2/sigmoid.m | 20 +++ 9 files changed, 503 insertions(+) create mode 100644 Programming Assignment/Week_2/jayakar_week2/costFunction.m create mode 100644 Programming Assignment/Week_2/jayakar_week2/costFunctionReg.m create mode 100644 Programming Assignment/Week_2/jayakar_week2/ex2.m create mode 100644 Programming Assignment/Week_2/jayakar_week2/ex2_reg.m create mode 100644 Programming Assignment/Week_2/jayakar_week2/mapFeature.m create mode 100644 Programming Assignment/Week_2/jayakar_week2/plotData.m create mode 100644 Programming Assignment/Week_2/jayakar_week2/plotDecisionBoundary.m create mode 100644 Programming Assignment/Week_2/jayakar_week2/predict.m create mode 100644 Programming Assignment/Week_2/jayakar_week2/sigmoid.m diff --git a/Programming Assignment/Week_2/jayakar_week2/costFunction.m b/Programming Assignment/Week_2/jayakar_week2/costFunction.m new file mode 100644 index 0000000..9f7d711 --- /dev/null +++ b/Programming Assignment/Week_2/jayakar_week2/costFunction.m @@ -0,0 +1,35 @@ +function [J, grad] = costFunction(theta, X, y) +%COSTFUNCTION Compute cost and gradient for logistic regression +% J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the +% parameter for logistic regression and the gradient of the cost +% w.r.t. to the parameters. + +% Initialize some useful values +m = length(y); % number of training examples +% You need to return the following variables correctly +h=X*theta; +h=sigmoid(h); +J = 0; +J=(-1)*(((y')*log(h))+((1-y)'*log(1-h))); +J=J/m; +grad = zeros(size(theta)); +grad=(1/m)*(X'*(h-y)); +% ====================== YOUR CODE HERE ====================== +% Instructions: Compute the cost of a particular choice of theta. +% You should set J to the cost. +% Compute the partial derivatives and set grad to the partial +% derivatives of the cost w.r.t. each parameter in theta +% +% Note: grad should have the same dimensions as theta +% + + + + + + + + +% ============================================================= + +end diff --git a/Programming Assignment/Week_2/jayakar_week2/costFunctionReg.m b/Programming Assignment/Week_2/jayakar_week2/costFunctionReg.m new file mode 100644 index 0000000..86e2e21 --- /dev/null +++ b/Programming Assignment/Week_2/jayakar_week2/costFunctionReg.m @@ -0,0 +1,32 @@ +function [J, grad] = costFunctionReg(theta, X, y, lambda) +%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization +% J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using +% theta as the parameter for regularized logistic regression and the +% gradient of the cost w.r.t. to the parameters. + +% Initialize some useful values +m = length(y); % number of training examples + +% You need to return the following variables correctly +J = 0; +grad = zeros(size(theta)); + +% ====================== YOUR CODE HERE ====================== +% Instructions: Compute the cost of a particular choice of theta. +% You should set J to the cost. +% Compute the partial derivatives and set grad to the partial +% derivatives of the cost w.r.t. each parameter in theta +h=X*theta; +h=sigmoid(h); +J=(-1)*(((y')*log(h))+((1-y)'*log(1-h))); +J=J/m; +k=ones(1,size(theta,1)); +l=theta.^2; +J=J+(lambda/(2*m))*(k*l); +J=J-(lambda/(2*m))*l(1,1); +grad=(1/m)*(X'*(h-y)); +grad=grad+(lambda/m)*theta; +grad(1)=grad(1)-(lambda/m)*theta(1); +% ============================================================= + +end diff --git a/Programming Assignment/Week_2/jayakar_week2/ex2.m b/Programming Assignment/Week_2/jayakar_week2/ex2.m new file mode 100644 index 0000000..103fe11 --- /dev/null +++ b/Programming Assignment/Week_2/jayakar_week2/ex2.m @@ -0,0 +1,151 @@ +%% Machine Learning Online Class - Exercise 2: Logistic Regression +% +% Instructions +% ------------ +% +% This file contains code that helps you get started on the logistic +% regression exercise. You will need to complete the following functions +% in this exericse: +% +% sigmoid.m +% costFunction.m +% predict.m +% costFunctionReg.m +% +% For this exercise, you will not need to change any code in this file, +% or any other files other than those mentioned above. +% + +%% Initialization +clear ; close all; clc + +%% Load Data +% The first two columns contains the exam scores and the third column +% contains the label. + +data = load('ex2data1.txt'); +X = data(:, [1, 2]); y = data(:, 3); + +%% ==================== Part 1: Plotting ==================== +% We start the exercise by first plotting the data to understand the +% the problem we are working with. + +fprintf(['Plotting data with + indicating (y = 1) examples and o ' ... + 'indicating (y = 0) examples.\n']); + +plotData(X, y); + +% Put some labels +hold on; +% Labels and Legend +xlabel('Exam 1 score') +ylabel('Exam 2 score') + +% Specified in plot order +legend('Admitted', 'Not admitted') +hold off; + +fprintf('\nProgram paused. Press enter to continue.\n'); +pause; + + +%% ============ Part 2: Compute Cost and Gradient ============ +% In this part of the exercise, you will implement the cost and gradient +% for logistic regression. You neeed to complete the code in +% costFunction.m + +% Setup the data matrix appropriately, and add ones for the intercept term +[m, n] = size(X); + +% Add intercept term to x and X_test +X = [ones(m, 1) X]; + +% Initialize fitting parameters +initial_theta = zeros(n + 1, 1); + +% Compute and display initial cost and gradient +[cost, grad] = costFunction(initial_theta, X, y); + +fprintf('Cost at initial theta (zeros): %f\n', cost); +fprintf('Expected cost (approx): 0.693\n'); +fprintf('Gradient at initial theta (zeros): \n'); +fprintf(' %f \n', grad); +fprintf('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628\n'); + +% Compute and display cost and gradient with non-zero theta +test_theta = [-24; 0.2; 0.2]; +[cost, grad] = costFunction(test_theta, X, y); + +fprintf('\nCost at test theta: %f\n', cost); +fprintf('Expected cost (approx): 0.218\n'); +fprintf('Gradient at test theta: \n'); +fprintf(' %f \n', grad); +fprintf('Expected gradients (approx):\n 0.043\n 2.566\n 2.647\n'); + +fprintf('\nProgram paused. Press enter to continue.\n'); +pause; + + +%% ============= Part 3: Optimizing using fminunc ============= +% In this exercise, you will use a built-in function (fminunc) to find the +% optimal parameters theta. + +% Set options for fminunc +options = optimset('GradObj', 'on', 'MaxIter', 400); + +% Run fminunc to obtain the optimal theta +% This function will return theta and the cost +[theta, cost] = ... + fminunc(@(t)(costFunction(t, X, y)), initial_theta, options); + +% Print theta to screen +fprintf('Cost at theta found by fminunc: %f\n', cost); +fprintf('Expected cost (approx): 0.203\n'); +fprintf('theta: \n'); +fprintf(' %f \n', theta); +fprintf('Expected theta (approx):\n'); +fprintf(' -25.161\n 0.206\n 0.201\n'); + +% Plot Boundary +plotDecisionBoundary(theta, X, y); + +% Put some labels +hold on; +% Labels and Legend +xlabel('Exam 1 score') +ylabel('Exam 2 score') + +% Specified in plot order +legend('Admitted', 'Not admitted') +hold off; + +fprintf('\nProgram paused. Press enter to continue.\n'); +pause; + +%% ============== Part 4: Predict and Accuracies ============== +% After learning the parameters, you'll like to use it to predict the outcomes +% on unseen data. In this part, you will use the logistic regression model +% to predict the probability that a student with score 45 on exam 1 and +% score 85 on exam 2 will be admitted. +% +% Furthermore, you will compute the training and test set accuracies of +% our model. +% +% Your task is to complete the code in predict.m + +% Predict probability for a student with score 45 on exam 1 +% and score 85 on exam 2 + +prob = sigmoid([1 45 85] * theta); +fprintf(['For a student with scores 45 and 85, we predict an admission ' ... + 'probability of %f\n'], prob); +fprintf('Expected value: 0.775 +/- 0.002\n\n'); + +% Compute accuracy on our training set +p = predict(theta, X); + +fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100); +fprintf('Expected accuracy (approx): 89.0\n'); +fprintf('\n'); + + diff --git a/Programming Assignment/Week_2/jayakar_week2/ex2_reg.m b/Programming Assignment/Week_2/jayakar_week2/ex2_reg.m new file mode 100644 index 0000000..f363318 --- /dev/null +++ b/Programming Assignment/Week_2/jayakar_week2/ex2_reg.m @@ -0,0 +1,136 @@ +%% Machine Learning Online Class - Exercise 2: Logistic Regression +% +% Instructions +% ------------ +% +% This file contains code that helps you get started on the second part +% of the exercise which covers regularization with logistic regression. +% +% You will need to complete the following functions in this exericse: +% +% sigmoid.m +% costFunction.m +% predict.m +% costFunctionReg.m +% +% For this exercise, you will not need to change any code in this file, +% or any other files other than those mentioned above. +% + +%% Initialization +clear ; close all; clc + +%% Load Data +% The first two columns contains the X values and the third column +% contains the label (y). + +data = load('ex2data2.txt'); +X = data(:, [1, 2]); y = data(:, 3); + +plotData(X, y); + +% Put some labels +hold on; + +% Labels and Legend +xlabel('Microchip Test 1') +ylabel('Microchip Test 2') + +% Specified in plot order +legend('y = 1', 'y = 0') +hold off; + + +%% =========== Part 1: Regularized Logistic Regression ============ +% In this part, you are given a dataset with data points that are not +% linearly separable. However, you would still like to use logistic +% regression to classify the data points. +% +% To do so, you introduce more features to use -- in particular, you add +% polynomial features to our data matrix (similar to polynomial +% regression). +% + +% Add Polynomial Features + +% Note that mapFeature also adds a column of ones for us, so the intercept +% term is handled +X = mapFeature(X(:,1), X(:,2)); + +% Initialize fitting parameters +initial_theta = zeros(size(X, 2), 1); + +% Set regularization parameter lambda to 1 +lambda = 1; + +% Compute and display initial cost and gradient for regularized logistic +% regression +[cost, grad] = costFunctionReg(initial_theta, X, y, lambda); + +fprintf('Cost at initial theta (zeros): %f\n', cost); +fprintf('Expected cost (approx): 0.693\n'); +fprintf('Gradient at initial theta (zeros) - first five values only:\n'); +fprintf(' %f \n', grad(1:5)); +fprintf('Expected gradients (approx) - first five values only:\n'); +fprintf(' 0.0085\n 0.0188\n 0.0001\n 0.0503\n 0.0115\n'); + +fprintf('\nProgram paused. Press enter to continue.\n'); +pause; + +% Compute and display cost and gradient +% with all-ones theta and lambda = 10 +test_theta = ones(size(X,2),1); +[cost, grad] = costFunctionReg(test_theta, X, y, 10); + +fprintf('\nCost at test theta (with lambda = 10): %f\n', cost); +fprintf('Expected cost (approx): 3.16\n'); +fprintf('Gradient at test theta - first five values only:\n'); +fprintf(' %f \n', grad(1:5)); +fprintf('Expected gradients (approx) - first five values only:\n'); +fprintf(' 0.3460\n 0.1614\n 0.1948\n 0.2269\n 0.0922\n'); + +fprintf('\nProgram paused. Press enter to continue.\n'); +pause; + +%% ============= Part 2: Regularization and Accuracies ============= +% Optional Exercise: +% In this part, you will get to try different values of lambda and +% see how regularization affects the decision coundart +% +% Try the following values of lambda (0, 1, 10, 100). +% +% How does the decision boundary change when you vary lambda? How does +% the training set accuracy vary? +% + +% Initialize fitting parameters +initial_theta = zeros(size(X, 2), 1); + +% Set regularization parameter lambda to 1 (you should vary this) +lambda = 1; + +% Set Options +options = optimset('GradObj', 'on', 'MaxIter', 400); + +% Optimize +[theta, J, exit_flag] = ... + fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options); + +% Plot Boundary +plotDecisionBoundary(theta, X, y); +hold on; +title(sprintf('lambda = %g', lambda)) + +% Labels and Legend +xlabel('Microchip Test 1') +ylabel('Microchip Test 2') + +legend('y = 1', 'y = 0', 'Decision boundary') +hold off; + +% Compute accuracy on our training set +p = predict(theta, X); + +fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100); +fprintf('Expected accuracy (with lambda = 1): 83.1 (approx)\n'); + diff --git a/Programming Assignment/Week_2/jayakar_week2/mapFeature.m b/Programming Assignment/Week_2/jayakar_week2/mapFeature.m new file mode 100644 index 0000000..d02a72a --- /dev/null +++ b/Programming Assignment/Week_2/jayakar_week2/mapFeature.m @@ -0,0 +1,21 @@ +function out = mapFeature(X1, X2) +% MAPFEATURE Feature mapping function to polynomial features +% +% MAPFEATURE(X1, X2) maps the two input features +% to quadratic features used in the regularization exercise. +% +% Returns a new feature array with more features, comprising of +% X1, X2, X1.^2, X2.^2, X1*X2, X1*X2.^2, etc.. +% +% Inputs X1, X2 must be the same size +% + +degree = 6; +out = ones(size(X1(:,1))); +for i = 1:degree + for j = 0:i + out(:, end+1) = (X1.^(i-j)).*(X2.^j); + end +end + +end \ No newline at end of file diff --git a/Programming Assignment/Week_2/jayakar_week2/plotData.m b/Programming Assignment/Week_2/jayakar_week2/plotData.m new file mode 100644 index 0000000..d8e7f9d --- /dev/null +++ b/Programming Assignment/Week_2/jayakar_week2/plotData.m @@ -0,0 +1,32 @@ +function plotData(X, y) +%PLOTDATA Plots the data points X and y into a new figure +% PLOTDATA(x,y) plots the data points with + for the positive examples +% and o for the negative examples. X is assumed to be a Mx2 matrix. +figure; +pos = find(y==1); neg = find(y == 0); +plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2,'MarkerSize', 7); + hold on; +plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y','MarkerSize', 7); + + +% ====================== YOUR CODE HERE ====================== +% Instructions: Plot the positive and negative examples on a +% 2D plot, using the option 'k+' for the positive +% examples and 'ko' for the negative examples. +% + + + + + + + + + +% ========================================================================= + + + +hold off; + +end diff --git a/Programming Assignment/Week_2/jayakar_week2/plotDecisionBoundary.m b/Programming Assignment/Week_2/jayakar_week2/plotDecisionBoundary.m new file mode 100644 index 0000000..cd36314 --- /dev/null +++ b/Programming Assignment/Week_2/jayakar_week2/plotDecisionBoundary.m @@ -0,0 +1,48 @@ +function plotDecisionBoundary(theta, X, y) +%PLOTDECISIONBOUNDARY Plots the data points X and y into a new figure with +%the decision boundary defined by theta +% PLOTDECISIONBOUNDARY(theta, X,y) plots the data points with + for the +% positive examples and o for the negative examples. X is assumed to be +% a either +% 1) Mx3 matrix, where the first column is an all-ones column for the +% intercept. +% 2) MxN, N>3 matrix, where the first column is all-ones + +% Plot Data +plotData(X(:,2:3), y); +hold on + +if size(X, 2) <= 3 + % Only need 2 points to define a line, so choose two endpoints + plot_x = [min(X(:,2))-2, max(X(:,2))+2]; + + % Calculate the decision boundary line + plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1)); + + % Plot, and adjust axes for better viewing + plot(plot_x, plot_y) + + % Legend, specific for the exercise + legend('Admitted', 'Not admitted', 'Decision Boundary') + axis([30, 100, 30, 100]) +else + % Here is the grid range + u = linspace(-1, 1.5, 50); + v = linspace(-1, 1.5, 50); + + z = zeros(length(u), length(v)); + % Evaluate z = theta*x over the grid + for i = 1:length(u) + for j = 1:length(v) + z(i,j) = mapFeature(u(i), v(j))*theta; + end + end + z = z'; % important to transpose z before calling contour + + % Plot z = 0 + % Notice you need to specify the range [0, 0] + contour(u, v, z, [0, 0], 'LineWidth', 2) +end +hold off + +end diff --git a/Programming Assignment/Week_2/jayakar_week2/predict.m b/Programming Assignment/Week_2/jayakar_week2/predict.m new file mode 100644 index 0000000..c85ac58 --- /dev/null +++ b/Programming Assignment/Week_2/jayakar_week2/predict.m @@ -0,0 +1,28 @@ +function p = predict(theta, X) +%PREDICT Predict whether the label is 0 or 1 using learned logistic +%regression parameters theta +% p = PREDICT(theta, X) computes the predictions for X using a +% threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1) + +m = size(X, 1); % Number of training examples + +% You need to return the following variables correctly +p = zeros(m, 1); +% ====================== YOUR CODE HERE ====================== +% Instructions: Complete the following code to make predictions using +% your learned logistic regression parameters. +% You should set p to a vector of 0's and 1's +% +h=sigmoid(X*theta); +for i=1:m + if h(i)>=0.5 + p(i)=1; + end; + if h(i) < 0.5 + p(i)=0; + end; +end; +% ========================================================================= + + +end diff --git a/Programming Assignment/Week_2/jayakar_week2/sigmoid.m b/Programming Assignment/Week_2/jayakar_week2/sigmoid.m new file mode 100644 index 0000000..e36d8d0 --- /dev/null +++ b/Programming Assignment/Week_2/jayakar_week2/sigmoid.m @@ -0,0 +1,20 @@ +function g = sigmoid(z) +%SIGMOID Compute sigmoid function +% g = SIGMOID(z) computes the sigmoid of z. + +% You need to return the following variables correctly +g = zeros(size(z)); +z= -1*z; +g= 1+exp(z); +g=1./g; +% ====================== YOUR CODE HERE ====================== +% Instructions: Compute the sigmoid of each value of z (z can be a matrix, +% vector or scalar). + + + + + +% ============================================================= + +end From 1f47d897659df155e774df7993adf2c54cecdbfc Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 01:08:36 +0530 Subject: [PATCH 18/27] Delete costFunction.m --- .../Week 1/jayakar_week2/costFunction.m | 35 ------------------- 1 file changed, 35 deletions(-) delete mode 100644 Programming Assignment/Week 1/jayakar_week2/costFunction.m diff --git a/Programming Assignment/Week 1/jayakar_week2/costFunction.m b/Programming Assignment/Week 1/jayakar_week2/costFunction.m deleted file mode 100644 index 9f7d711..0000000 --- a/Programming Assignment/Week 1/jayakar_week2/costFunction.m +++ /dev/null @@ -1,35 +0,0 @@ -function [J, grad] = costFunction(theta, X, y) -%COSTFUNCTION Compute cost and gradient for logistic regression -% J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the -% parameter for logistic regression and the gradient of the cost -% w.r.t. to the parameters. - -% Initialize some useful values -m = length(y); % number of training examples -% You need to return the following variables correctly -h=X*theta; -h=sigmoid(h); -J = 0; -J=(-1)*(((y')*log(h))+((1-y)'*log(1-h))); -J=J/m; -grad = zeros(size(theta)); -grad=(1/m)*(X'*(h-y)); -% ====================== YOUR CODE HERE ====================== -% Instructions: Compute the cost of a particular choice of theta. -% You should set J to the cost. -% Compute the partial derivatives and set grad to the partial -% derivatives of the cost w.r.t. each parameter in theta -% -% Note: grad should have the same dimensions as theta -% - - - - - - - - -% ============================================================= - -end From 91ea3e2b2e5a5d0ed21417bf073ec29e34d7b7ed Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 01:08:49 +0530 Subject: [PATCH 19/27] Delete sigmoid.m --- .../Week 1/jayakar_week2/sigmoid.m | 20 ------------------- 1 file changed, 20 deletions(-) delete mode 100644 Programming Assignment/Week 1/jayakar_week2/sigmoid.m diff --git a/Programming Assignment/Week 1/jayakar_week2/sigmoid.m b/Programming Assignment/Week 1/jayakar_week2/sigmoid.m deleted file mode 100644 index e36d8d0..0000000 --- a/Programming Assignment/Week 1/jayakar_week2/sigmoid.m +++ /dev/null @@ -1,20 +0,0 @@ -function g = sigmoid(z) -%SIGMOID Compute sigmoid function -% g = SIGMOID(z) computes the sigmoid of z. - -% You need to return the following variables correctly -g = zeros(size(z)); -z= -1*z; -g= 1+exp(z); -g=1./g; -% ====================== YOUR CODE HERE ====================== -% Instructions: Compute the sigmoid of each value of z (z can be a matrix, -% vector or scalar). - - - - - -% ============================================================= - -end From 9f827be3533ae1bd03c5aaa773f6d9de29277d6d Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 01:08:56 +0530 Subject: [PATCH 20/27] Delete predict.m --- .../Week 1/jayakar_week2/predict.m | 28 ------------------- 1 file changed, 28 deletions(-) delete mode 100644 Programming Assignment/Week 1/jayakar_week2/predict.m diff --git a/Programming Assignment/Week 1/jayakar_week2/predict.m b/Programming Assignment/Week 1/jayakar_week2/predict.m deleted file mode 100644 index c85ac58..0000000 --- a/Programming Assignment/Week 1/jayakar_week2/predict.m +++ /dev/null @@ -1,28 +0,0 @@ -function p = predict(theta, X) -%PREDICT Predict whether the label is 0 or 1 using learned logistic -%regression parameters theta -% p = PREDICT(theta, X) computes the predictions for X using a -% threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1) - -m = size(X, 1); % Number of training examples - -% You need to return the following variables correctly -p = zeros(m, 1); -% ====================== YOUR CODE HERE ====================== -% Instructions: Complete the following code to make predictions using -% your learned logistic regression parameters. -% You should set p to a vector of 0's and 1's -% -h=sigmoid(X*theta); -for i=1:m - if h(i)>=0.5 - p(i)=1; - end; - if h(i) < 0.5 - p(i)=0; - end; -end; -% ========================================================================= - - -end From a312df0d1e1acf8342b930d27bc448a5a1857cc2 Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 01:09:03 +0530 Subject: [PATCH 21/27] Delete plotDecisionBoundary.m --- .../jayakar_week2/plotDecisionBoundary.m | 48 ------------------- 1 file changed, 48 deletions(-) delete mode 100644 Programming Assignment/Week 1/jayakar_week2/plotDecisionBoundary.m diff --git a/Programming Assignment/Week 1/jayakar_week2/plotDecisionBoundary.m b/Programming Assignment/Week 1/jayakar_week2/plotDecisionBoundary.m deleted file mode 100644 index cd36314..0000000 --- a/Programming Assignment/Week 1/jayakar_week2/plotDecisionBoundary.m +++ /dev/null @@ -1,48 +0,0 @@ -function plotDecisionBoundary(theta, X, y) -%PLOTDECISIONBOUNDARY Plots the data points X and y into a new figure with -%the decision boundary defined by theta -% PLOTDECISIONBOUNDARY(theta, X,y) plots the data points with + for the -% positive examples and o for the negative examples. X is assumed to be -% a either -% 1) Mx3 matrix, where the first column is an all-ones column for the -% intercept. -% 2) MxN, N>3 matrix, where the first column is all-ones - -% Plot Data -plotData(X(:,2:3), y); -hold on - -if size(X, 2) <= 3 - % Only need 2 points to define a line, so choose two endpoints - plot_x = [min(X(:,2))-2, max(X(:,2))+2]; - - % Calculate the decision boundary line - plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1)); - - % Plot, and adjust axes for better viewing - plot(plot_x, plot_y) - - % Legend, specific for the exercise - legend('Admitted', 'Not admitted', 'Decision Boundary') - axis([30, 100, 30, 100]) -else - % Here is the grid range - u = linspace(-1, 1.5, 50); - v = linspace(-1, 1.5, 50); - - z = zeros(length(u), length(v)); - % Evaluate z = theta*x over the grid - for i = 1:length(u) - for j = 1:length(v) - z(i,j) = mapFeature(u(i), v(j))*theta; - end - end - z = z'; % important to transpose z before calling contour - - % Plot z = 0 - % Notice you need to specify the range [0, 0] - contour(u, v, z, [0, 0], 'LineWidth', 2) -end -hold off - -end From 3c70f82b4c0a5ffaf2b0e84733a7eafadc659ca5 Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 01:09:10 +0530 Subject: [PATCH 22/27] Delete plotData.m --- .../Week 1/jayakar_week2/plotData.m | 32 ------------------- 1 file changed, 32 deletions(-) delete mode 100644 Programming Assignment/Week 1/jayakar_week2/plotData.m diff --git a/Programming Assignment/Week 1/jayakar_week2/plotData.m b/Programming Assignment/Week 1/jayakar_week2/plotData.m deleted file mode 100644 index d8e7f9d..0000000 --- a/Programming Assignment/Week 1/jayakar_week2/plotData.m +++ /dev/null @@ -1,32 +0,0 @@ -function plotData(X, y) -%PLOTDATA Plots the data points X and y into a new figure -% PLOTDATA(x,y) plots the data points with + for the positive examples -% and o for the negative examples. X is assumed to be a Mx2 matrix. -figure; -pos = find(y==1); neg = find(y == 0); -plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2,'MarkerSize', 7); - hold on; -plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y','MarkerSize', 7); - - -% ====================== YOUR CODE HERE ====================== -% Instructions: Plot the positive and negative examples on a -% 2D plot, using the option 'k+' for the positive -% examples and 'ko' for the negative examples. -% - - - - - - - - - -% ========================================================================= - - - -hold off; - -end From bc8ee4482684dd310d5099bdfe0291dcb5760988 Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 01:09:19 +0530 Subject: [PATCH 23/27] Delete mapFeature.m --- .../Week 1/jayakar_week2/mapFeature.m | 21 ------------------- 1 file changed, 21 deletions(-) delete mode 100644 Programming Assignment/Week 1/jayakar_week2/mapFeature.m diff --git a/Programming Assignment/Week 1/jayakar_week2/mapFeature.m b/Programming Assignment/Week 1/jayakar_week2/mapFeature.m deleted file mode 100644 index d02a72a..0000000 --- a/Programming Assignment/Week 1/jayakar_week2/mapFeature.m +++ /dev/null @@ -1,21 +0,0 @@ -function out = mapFeature(X1, X2) -% MAPFEATURE Feature mapping function to polynomial features -% -% MAPFEATURE(X1, X2) maps the two input features -% to quadratic features used in the regularization exercise. -% -% Returns a new feature array with more features, comprising of -% X1, X2, X1.^2, X2.^2, X1*X2, X1*X2.^2, etc.. -% -% Inputs X1, X2 must be the same size -% - -degree = 6; -out = ones(size(X1(:,1))); -for i = 1:degree - for j = 0:i - out(:, end+1) = (X1.^(i-j)).*(X2.^j); - end -end - -end \ No newline at end of file From 54cd225df5b9b1aa690c58faaf4bc2b60ace0425 Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 01:09:27 +0530 Subject: [PATCH 24/27] Delete ex2_reg.m --- .../Week 1/jayakar_week2/ex2_reg.m | 136 ------------------ 1 file changed, 136 deletions(-) delete mode 100644 Programming Assignment/Week 1/jayakar_week2/ex2_reg.m diff --git a/Programming Assignment/Week 1/jayakar_week2/ex2_reg.m b/Programming Assignment/Week 1/jayakar_week2/ex2_reg.m deleted file mode 100644 index f363318..0000000 --- a/Programming Assignment/Week 1/jayakar_week2/ex2_reg.m +++ /dev/null @@ -1,136 +0,0 @@ -%% Machine Learning Online Class - Exercise 2: Logistic Regression -% -% Instructions -% ------------ -% -% This file contains code that helps you get started on the second part -% of the exercise which covers regularization with logistic regression. -% -% You will need to complete the following functions in this exericse: -% -% sigmoid.m -% costFunction.m -% predict.m -% costFunctionReg.m -% -% For this exercise, you will not need to change any code in this file, -% or any other files other than those mentioned above. -% - -%% Initialization -clear ; close all; clc - -%% Load Data -% The first two columns contains the X values and the third column -% contains the label (y). - -data = load('ex2data2.txt'); -X = data(:, [1, 2]); y = data(:, 3); - -plotData(X, y); - -% Put some labels -hold on; - -% Labels and Legend -xlabel('Microchip Test 1') -ylabel('Microchip Test 2') - -% Specified in plot order -legend('y = 1', 'y = 0') -hold off; - - -%% =========== Part 1: Regularized Logistic Regression ============ -% In this part, you are given a dataset with data points that are not -% linearly separable. However, you would still like to use logistic -% regression to classify the data points. -% -% To do so, you introduce more features to use -- in particular, you add -% polynomial features to our data matrix (similar to polynomial -% regression). -% - -% Add Polynomial Features - -% Note that mapFeature also adds a column of ones for us, so the intercept -% term is handled -X = mapFeature(X(:,1), X(:,2)); - -% Initialize fitting parameters -initial_theta = zeros(size(X, 2), 1); - -% Set regularization parameter lambda to 1 -lambda = 1; - -% Compute and display initial cost and gradient for regularized logistic -% regression -[cost, grad] = costFunctionReg(initial_theta, X, y, lambda); - -fprintf('Cost at initial theta (zeros): %f\n', cost); -fprintf('Expected cost (approx): 0.693\n'); -fprintf('Gradient at initial theta (zeros) - first five values only:\n'); -fprintf(' %f \n', grad(1:5)); -fprintf('Expected gradients (approx) - first five values only:\n'); -fprintf(' 0.0085\n 0.0188\n 0.0001\n 0.0503\n 0.0115\n'); - -fprintf('\nProgram paused. Press enter to continue.\n'); -pause; - -% Compute and display cost and gradient -% with all-ones theta and lambda = 10 -test_theta = ones(size(X,2),1); -[cost, grad] = costFunctionReg(test_theta, X, y, 10); - -fprintf('\nCost at test theta (with lambda = 10): %f\n', cost); -fprintf('Expected cost (approx): 3.16\n'); -fprintf('Gradient at test theta - first five values only:\n'); -fprintf(' %f \n', grad(1:5)); -fprintf('Expected gradients (approx) - first five values only:\n'); -fprintf(' 0.3460\n 0.1614\n 0.1948\n 0.2269\n 0.0922\n'); - -fprintf('\nProgram paused. Press enter to continue.\n'); -pause; - -%% ============= Part 2: Regularization and Accuracies ============= -% Optional Exercise: -% In this part, you will get to try different values of lambda and -% see how regularization affects the decision coundart -% -% Try the following values of lambda (0, 1, 10, 100). -% -% How does the decision boundary change when you vary lambda? How does -% the training set accuracy vary? -% - -% Initialize fitting parameters -initial_theta = zeros(size(X, 2), 1); - -% Set regularization parameter lambda to 1 (you should vary this) -lambda = 1; - -% Set Options -options = optimset('GradObj', 'on', 'MaxIter', 400); - -% Optimize -[theta, J, exit_flag] = ... - fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options); - -% Plot Boundary -plotDecisionBoundary(theta, X, y); -hold on; -title(sprintf('lambda = %g', lambda)) - -% Labels and Legend -xlabel('Microchip Test 1') -ylabel('Microchip Test 2') - -legend('y = 1', 'y = 0', 'Decision boundary') -hold off; - -% Compute accuracy on our training set -p = predict(theta, X); - -fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100); -fprintf('Expected accuracy (with lambda = 1): 83.1 (approx)\n'); - From 6f2243393973bf12c77e8ae6417ac93b10064c56 Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 01:09:38 +0530 Subject: [PATCH 25/27] Delete ex2.m --- .../Week 1/jayakar_week2/ex2.m | 151 ------------------ 1 file changed, 151 deletions(-) delete mode 100644 Programming Assignment/Week 1/jayakar_week2/ex2.m diff --git a/Programming Assignment/Week 1/jayakar_week2/ex2.m b/Programming Assignment/Week 1/jayakar_week2/ex2.m deleted file mode 100644 index 103fe11..0000000 --- a/Programming Assignment/Week 1/jayakar_week2/ex2.m +++ /dev/null @@ -1,151 +0,0 @@ -%% Machine Learning Online Class - Exercise 2: Logistic Regression -% -% Instructions -% ------------ -% -% This file contains code that helps you get started on the logistic -% regression exercise. You will need to complete the following functions -% in this exericse: -% -% sigmoid.m -% costFunction.m -% predict.m -% costFunctionReg.m -% -% For this exercise, you will not need to change any code in this file, -% or any other files other than those mentioned above. -% - -%% Initialization -clear ; close all; clc - -%% Load Data -% The first two columns contains the exam scores and the third column -% contains the label. - -data = load('ex2data1.txt'); -X = data(:, [1, 2]); y = data(:, 3); - -%% ==================== Part 1: Plotting ==================== -% We start the exercise by first plotting the data to understand the -% the problem we are working with. - -fprintf(['Plotting data with + indicating (y = 1) examples and o ' ... - 'indicating (y = 0) examples.\n']); - -plotData(X, y); - -% Put some labels -hold on; -% Labels and Legend -xlabel('Exam 1 score') -ylabel('Exam 2 score') - -% Specified in plot order -legend('Admitted', 'Not admitted') -hold off; - -fprintf('\nProgram paused. Press enter to continue.\n'); -pause; - - -%% ============ Part 2: Compute Cost and Gradient ============ -% In this part of the exercise, you will implement the cost and gradient -% for logistic regression. You neeed to complete the code in -% costFunction.m - -% Setup the data matrix appropriately, and add ones for the intercept term -[m, n] = size(X); - -% Add intercept term to x and X_test -X = [ones(m, 1) X]; - -% Initialize fitting parameters -initial_theta = zeros(n + 1, 1); - -% Compute and display initial cost and gradient -[cost, grad] = costFunction(initial_theta, X, y); - -fprintf('Cost at initial theta (zeros): %f\n', cost); -fprintf('Expected cost (approx): 0.693\n'); -fprintf('Gradient at initial theta (zeros): \n'); -fprintf(' %f \n', grad); -fprintf('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628\n'); - -% Compute and display cost and gradient with non-zero theta -test_theta = [-24; 0.2; 0.2]; -[cost, grad] = costFunction(test_theta, X, y); - -fprintf('\nCost at test theta: %f\n', cost); -fprintf('Expected cost (approx): 0.218\n'); -fprintf('Gradient at test theta: \n'); -fprintf(' %f \n', grad); -fprintf('Expected gradients (approx):\n 0.043\n 2.566\n 2.647\n'); - -fprintf('\nProgram paused. Press enter to continue.\n'); -pause; - - -%% ============= Part 3: Optimizing using fminunc ============= -% In this exercise, you will use a built-in function (fminunc) to find the -% optimal parameters theta. - -% Set options for fminunc -options = optimset('GradObj', 'on', 'MaxIter', 400); - -% Run fminunc to obtain the optimal theta -% This function will return theta and the cost -[theta, cost] = ... - fminunc(@(t)(costFunction(t, X, y)), initial_theta, options); - -% Print theta to screen -fprintf('Cost at theta found by fminunc: %f\n', cost); -fprintf('Expected cost (approx): 0.203\n'); -fprintf('theta: \n'); -fprintf(' %f \n', theta); -fprintf('Expected theta (approx):\n'); -fprintf(' -25.161\n 0.206\n 0.201\n'); - -% Plot Boundary -plotDecisionBoundary(theta, X, y); - -% Put some labels -hold on; -% Labels and Legend -xlabel('Exam 1 score') -ylabel('Exam 2 score') - -% Specified in plot order -legend('Admitted', 'Not admitted') -hold off; - -fprintf('\nProgram paused. Press enter to continue.\n'); -pause; - -%% ============== Part 4: Predict and Accuracies ============== -% After learning the parameters, you'll like to use it to predict the outcomes -% on unseen data. In this part, you will use the logistic regression model -% to predict the probability that a student with score 45 on exam 1 and -% score 85 on exam 2 will be admitted. -% -% Furthermore, you will compute the training and test set accuracies of -% our model. -% -% Your task is to complete the code in predict.m - -% Predict probability for a student with score 45 on exam 1 -% and score 85 on exam 2 - -prob = sigmoid([1 45 85] * theta); -fprintf(['For a student with scores 45 and 85, we predict an admission ' ... - 'probability of %f\n'], prob); -fprintf('Expected value: 0.775 +/- 0.002\n\n'); - -% Compute accuracy on our training set -p = predict(theta, X); - -fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100); -fprintf('Expected accuracy (approx): 89.0\n'); -fprintf('\n'); - - From 281fbdaa63cb71e68a1e2fd37d6477401768dcaf Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Mon, 1 Jul 2019 01:09:46 +0530 Subject: [PATCH 26/27] Delete costFunctionReg.m --- .../Week 1/jayakar_week2/costFunctionReg.m | 32 ------------------- 1 file changed, 32 deletions(-) delete mode 100644 Programming Assignment/Week 1/jayakar_week2/costFunctionReg.m diff --git a/Programming Assignment/Week 1/jayakar_week2/costFunctionReg.m b/Programming Assignment/Week 1/jayakar_week2/costFunctionReg.m deleted file mode 100644 index 86e2e21..0000000 --- a/Programming Assignment/Week 1/jayakar_week2/costFunctionReg.m +++ /dev/null @@ -1,32 +0,0 @@ -function [J, grad] = costFunctionReg(theta, X, y, lambda) -%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization -% J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using -% theta as the parameter for regularized logistic regression and the -% gradient of the cost w.r.t. to the parameters. - -% Initialize some useful values -m = length(y); % number of training examples - -% You need to return the following variables correctly -J = 0; -grad = zeros(size(theta)); - -% ====================== YOUR CODE HERE ====================== -% Instructions: Compute the cost of a particular choice of theta. -% You should set J to the cost. -% Compute the partial derivatives and set grad to the partial -% derivatives of the cost w.r.t. each parameter in theta -h=X*theta; -h=sigmoid(h); -J=(-1)*(((y')*log(h))+((1-y)'*log(1-h))); -J=J/m; -k=ones(1,size(theta,1)); -l=theta.^2; -J=J+(lambda/(2*m))*(k*l); -J=J-(lambda/(2*m))*l(1,1); -grad=(1/m)*(X'*(h-y)); -grad=grad+(lambda/m)*theta; -grad(1)=grad(1)-(lambda/m)*theta(1); -% ============================================================= - -end From 80ef8b66804e484b587b2d3940be8ca5031eba19 Mon Sep 17 00:00:00 2001 From: jayakar01 <49501623+jayakar01@users.noreply.github.com> Date: Tue, 29 Oct 2019 14:31:35 +0530 Subject: [PATCH 27/27] Checking out a reame feature. --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index f1919c0..a8bdd9d 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,9 @@ ### This is an attempt at teaching our juniors at IIT Bhubaneswar Machine Learning. +![Student Community](https://www.google.com/imgres?imgurl=https%3A%2F%2Fdataqualitycampaign.org%2Fwp-content%2Fuploads%2F2016%2F03%2Fwhy-education-data-illustration-3.png&imgrefurl=https%3A%2F%2Fdataqualitycampaign.org%2Fwhy-education-data%2F&docid=qxzQaiCVfbwnXM&tbnid=4SzoAgqZUdpTbM%3A&vet=10ahUKEwiBubLeisHlAhVafisKHQGyBrMQMwhVKAkwCQ..i&w=1134&h=920&bih=669&biw=1299&q=students%20helping%20each%20other%20animated&ved=0ahUKEwiBubLeisHlAhVafisKHQGyBrMQMwhVKAkwCQ&iact=mrc&uact=8) + + * This repository includes their project codes, programming assignments -----