Classification
Logistic Regression
2023-12-15
Logistic regression is the first algorithm that is often selected to handle classification problems, such as determining the truth or falsehood, color, size, and so on. It is similar to multivariate regression problems, but the difference is that the response is represented in binary form. In other words, while regression takes on values between 0 and 1, classification is represented only as 0 or 1 without any intermediate values.
The following plot represents the data of iris flowers, where the X-axis represents one of the source variables from the dataset, and the Y-axis represents the binary classified measurement results.

When applying a linear regression to the same data, it would result in a dashed line as shown below. However, if we apply a certain transformation to the regression output such as to produce a step function (as shown in sold red line) to output only 0 and 1, we can call it the solution of Logistic regression. The threshold in X near 5.5 becomes a decision boundary that separates 0 and 1.

In reality, a differentiable sigmoid function is used for optimization purposes instead of the step function.
The sigmoid function is a continuous function that spans between 0 and 1 as shown in the plot below. It represents the probability or chance of X happening. It is important to note that 0 represents the absence of any measured value and also signifies that there is no chance of it becoming 1. Conversely, 1 represents the probability of being 1 itself. In other words, the sigmoid function, ϕ, represents the probability of becoming 1 given some value z, and 1-P represents the probability of becoming 0.

Then what should the cost function would be? In regression process, we minimize the squared difference between the observed value Y and the predicted value y. Similarly, in logistic regression, we can define the cost function to maximize the probability of predicting 0 as 0 and 1 as 1. Here, we use the log likelihood (log(P)) as the predicted probability.
If we summarize all these steps in a similar way to multivariate regression, for the given two source parameters (x1, x2) and response parameter (y),
Step 1. Set a linear regression for y' as a function of X
\(y'=m_1x_1+m_2x_2+c\)
Here, m1 and m2 are weights, and c is the threshold or bias.
Step 2. Apply the sigmoid function to the y'
\(\phi (y')=P(y')=\dfrac{1}{1+e^{-y'}}\)
Step 3. Define the cost function, L
\(L=\sum _{y=1}^{\ }\log P(y')+\sum _{y=0}^{\ }\log (1-P(y'))\)
It can be also presented as a combined form
\(L=\sum _{for all y}^{\ }y\log P(y')+(1-y)\log (1-P(y'))\)
The cost function is defined as follows: for y' with all measurements equal to 1, we add log(P), and for y' with all measurements equal to 0, we add log(1-P).
Step 4. Find m1, m2 , and c using Excel's Solver feature, and obtain the decision boundary where P=0.5 or y'=0
Followings are the illustration steps in Excel using the Iris dataset.
Step 1. Perform a binary labeling of Iris classes. Set Iris-setosa=0, Iris-versicolor=1.
Step 2. Perform an ordinary linear regression in the form of y'=m1x1+m2x2+c to find proper initial conditions for m1, m2, & c for Excel Solver. Here, we use the LINEST as explained in the previous blog.
Please note that the LINEST reports the coefficients in a specific order for the case of multiple variable regression.

Step 3. Take the coefficients from the LINEST as the initial guess of the coefficients

Step 4. Calculate y' at Column 'K' , Compute P=1/(1+e-y') at Column 'L' , and Compute log(P) at Column 'M'
Column 'K' : =SUMPRODUCT(B2:C2, G$4:H$4)+I$4
Column 'L' : =1/(1+EXP(-K2))
Column 'M': =if(E2=0, LOG(1-L2), LOG(L2))
Step 5. Calculate the Cost function, L, to maximize as a function of the weights and bias.
To-Maximize cost = SUM(M2:M101)

Step 6. Run Excel Solver as below settings.

Step 7. Optimal coefficients from the Solver which updates the cost from -23.44 to near zero.

(step 8) Following plot is the comparison between the observed and the predicted.

Step 9. The decision boundary between two classes.
The decision boundary is the line where y'=0. The red line below is calculated from the following equation.
\(x_2 = \dfrac {-c-m_1x_1}{m_2}\)
