Skip to content

Instantly share code, notes, and snippets.

@rlopc
Last active April 9, 2021 03:36
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rlopc/8094962 to your computer and use it in GitHub Desktop.
Save rlopc/8094962 to your computer and use it in GitHub Desktop.
Compute sigmoid function, the hypothesis function in Logistic Regression
function g = sigmoidFunction(z)
% Compute sigmoid function
% You need to return the following variables correctly
g = zeros(size(z));
% Instructions: z can be a matrix, vector or scalar
g = 1.0 ./ ( 1.0 + exp(-z)); % For Matlab
% g = 1.0 ./ ( 1.0 + e.^(-z)); % For Octave, it can use 'exp(1)' or 'e'
end
@Prathiksha01
Copy link

This function applies for both Matlab and Octave. exp() is a predefined function in Octave also.

@benja28
Copy link

benja28 commented Jan 31, 2017

hello,
please I have 3 paramete an I want to use the sigmoid function to define and maximize my fnction which contains three prarameters in Matlab.

the parameters are the vectors:

deg=1:49

LQ=1:49

Rank=1:49

and my function is : Strength = f(deg,LQ,Rank)

I have execute : Strength= 1./1+exp (-deg(LQ-Rank))

but it not corect and I have not results.

so please help me I need it in urgentrs : the deg, LQ, and Rank which I will used the sigmoide function for this
how can I do it
thank you in advance
regards

@rcr1994
Copy link

rcr1994 commented Sep 8, 2017

Can you explain, why are we using a '.' after '1.0' while computing 'g'?

@digitarise
Copy link

Even i am waiting to know why '.' is used while computing g. Please help.

@aaronlelevier
Copy link

aaronlelevier commented Sep 29, 2017

This returns the same answer as above. The final parentheses around -z can be removed for Octave

g = 1 ./ (1 + e .^ -z);

@Depro20
Copy link

Depro20 commented Nov 18, 2017

'.' is used to make sure if z is a vector (a matrix) then each element undergoes the same operation simultaneously. Take a look at Matlab's docs for this. For instance : https://in.mathworks.com/help/fixedpoint/ref/rdivide.html

@schlerp
Copy link

schlerp commented Mar 18, 2018

to rephrase the last answer: the dot in front of operators make sit the elementwise version of that operator

@jamayz
Copy link

jamayz commented May 26, 2018

i have done this so many times but i keep getting that my function 'z' is not defined

@BehrouzGit
Copy link

BehrouzGit commented Mar 15, 2019

@jamayz since it is a function, you need to specify 'z' yourself. You can try sigmoidFunction(0) for example in the command line and you should get 0.5.
Also please note that 'z' can be a vector or matrix. so you can specify z=[1 2 3], as a vector and try the code.

@mtrudelle
Copy link

Why does the expression g = 1`/ (1 + exp(-y)) does not work in Octave if v is a 100 x 1 vector ??
-=-=-=-=-=-=-=-=-=
octave:70> size(y)
ans =
100 1

octave:71> y=zeros([100,1]);
octave:72> g=1/(1+exp(-y))
g =
Columns 1 through 16:
0.0050000 0.0050000 0.0050000 0.0050000

@NiteshSigar
Copy link

Hey!
May you explain the use of g = zeros(size(z)); here!

I tried and ran it into octave for every scalar like:
a=2;
size(2);
it always gave out of
1 1

can you explain it!

@surbhi-jain-30-99
Copy link

Can you explain the use of g = zeros(size(z));
I am not able to get how to use it

@HI-241
Copy link

HI-241 commented Jul 24, 2020

It allows you to catch the number of elements whether its a matrix/vector/scalar, and apply properly the elementwize option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment