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/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 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 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 -----