Change this code from matlab language to Python language : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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)); [m,n]=size(z); % Instructions: Compute the sigmoid of each value of z (z can be a matrix, %               vector or scalar). for i=1:m         for j=1:n             g(i,j)=1/(1+exp(-z(i,j)));         end end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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  J = 0; grad = zeros(size(theta)); % 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 %     h=sigmoid(X*theta);     J=(-1/m)*sum(y.*log(h)+(1-y).*log(1-h));           for j=1:size(theta)         grad(j)=(1/m)*sum((h-y).*X(:,j));        end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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); p=sigmoid(X*theta); for i=1:m         if (p(i) >= 0.5)             p(i) =1;         else             p(i)=0;         end end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Change this code from matlab language to Python language :

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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));
[m,n]=size(z);

% Instructions: Compute the sigmoid of each value of z (z can be a matrix,
%               vector or scalar).
for i=1:m
        for j=1:n
            g(i,j)=1/(1+exp(-z(i,j)));
        end
end

end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 
J = 0;
grad = zeros(size(theta));

% 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
%
    h=sigmoid(X*theta);
    J=(-1/m)*sum(y.*log(h)+(1-y).*log(1-h));
     
    for j=1:size(theta)
        grad(j)=(1/m)*sum((h-y).*X(:,j));   
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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);
p=sigmoid(X*theta);
for i=1:m
        if (p(i) >= 0.5)
            p(i) =1;
        else
            p(i)=0;

        end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

 

Expert Solution
steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Computational Systems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education