Consider a model with features . Let the binary output be denoted by , that can take the values 0 or 1. Let be the probability of , we can denote it as . The mathematical relationship between these variables can be denoted as:
Here the term is known as the odds and denotes the likelihood of the event taking place. Thus is known as the log odds and is simply used to map the probability that lies between 0 and 1 to a range between (−∞, +∞). The terms are parameters (or weights) that we will estimate during training.
Now we will be using the above equation to make our predictions. Before that we will train our model to obtain the values of our parameters that result in least error.
A L2 Loss function such as Least Squared Error will do the job.
You might know that the partial derivative of a function at its minimum value is equal to 0. So gradient descent basically uses this concept to estimate the parameters or weights of our model by minimizing the loss function.
Initialize the weights, and .
Calculate the partial derivative with respect to and
Update the weights - values of and
# Importing librariesimport numpy as npimport pandas as pdfrom sklearn.model_selection import train_test_splitfrom math import exp# Preparing the datasetdata = pd.DataFrame({'feature' : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], 'label' : [0,0,0,0,0,0,0,1,1,1,1,1,1,1,1]})# Divide the data to training set and test setX_train, X_test, y_train, y_test = train_test_split(data['feature'], data['label'], test_size=0.30)## Logistic Regression Model# Helper function to normalize datadef normalize(X):return X - X.mean()# Method to make predictionsdef predict(X, theta0, theta1):# Here the predict function is: 1/(1+e^(-x))return np.array([1 / (1 + exp(-(theta0 + theta1*x))) for x in X])# Method to train the modeldef logistic_regression(X, Y):# Normalizing the dataX = normalize(X)# Initializing variablestheta0 = 0theta1 = 0learning_rate = 0.001epochs = 300# Training iterationfor epoch in range(epochs):y_pred = predict(X, theta0, theta1)## Here the loss function is: sum(y-y_pred)^2 a.k.a least squared error (LSE)# Derivative of loss w.r.t. theta0theta0_d = -2 * sum((Y - y_pred) * y_pred * (1 - y_pred))# Derivative of loss w.r.t. theta1theta1_d = -2 * sum(X * (Y - y_pred) * y_pred * (1 - y_pred))theta0 = theta0 - learning_rate * theta0_dtheta1 = theta1 - learning_rate * theta1_dreturn theta0, theta1# Training the modeltheta0, theta1 = logistic_regression(X_train, y_train)# Making predictionsX_test_norm = normalize(X_test)y_pred = predict(X_test_norm, theta0, theta1)y_pred = [1 if p >= 0.5 else 0 for p in y_pred]# Evaluating the modelprint(list(y_test))print(y_pred)