Description
The Mean squared error cost function produces a non-convex graph with the local and global minimums when applied to a sigmoid function. If a weight value is close to a local minimum, gradient descent minimizes the cost function by the local (not global) minimum. This presents grave limitations to the Mean squared error cost function if we apply it to binary classification tasks. The Log-loss cost function may help to overcome this issue.
We can represent it in the following way:
where
In the previous stage, you've implemented the Stochastic gradient descent with the Mean squared error loss function and obtained the coef_ values. The procedure of applying the Stochastic gradient descent to the Log-loss cost function is similar. The only differences are the first-order differentials with which we will update the weights.
The bias is updated with:
While the coefficients are updated with:
where is the observation (row) index. is the independent variable (column) index. is the number of rows in the training set.
Similar to the fit_mse method, the log-loss can be fitted without a bias term.
The attributes and methods in the CustomLogisticRegression class are:
class CustomLogisticRegression:
def __init__(self, fit_intercept=True, l_rate=0.01, n_epoch=100):
self.fit_intercept = ...
self.l_rate = ...
self.n_epoch = ...
def sigmoid(self, t):
return ...
def predict_proba(self, row, coef_):
t = ...
return self.sigmoid(t)
def fit_mse(self, X_train, y_train):
self.coef_ = ... # initialized weights
for _ in range(self.n_epoch):
for i, row in enumerate(X_train):
y_hat = self.predict_proba(row, coef_)
# update all weights
def fit_log_loss(self, X_train, y_train):
# stochastic gradient descent implementation
def predict(X_test, cut_off=0.5):
...
for row in X_test:
y_hat = self.predict_proba(row, self.coef_)
return predictions # predictions are binary values — 0 or 1Objectives
Implement
fit_log_loss;Load the dataset and select the same independent and target variables as in the previous stage;
Standardize
X;Instantiate the
CustomLogisticRegressionclass with the following attributes:lr = CustomLogisticRegression(fit_intercept=True, l_rate=0.01, n_epoch=1000)Fit the model with the training set from Stage 1 using
fit_log_loss;Predict
y_hatvalues for the test set;Calculate the accuracy score for the test set;
Print
coef_array and accuracy score as a Python dictionary in the format shown in the Examples section.
Examples
The training set below remains the same as in the Objectives. Only the test set and the CustomLogisticRegression class attributes change.
Example test set:
Standardized X_test and y_test data | |||
|
|
|
|
0.320904 | 0.230304 | -0.171560 | 1.0 |
-1.743529 | -0.954428 | -0.899849 | 1.0 |
1.014627 | 0.780857 | 0.773975 | 0.0 |
1.432990 | -0.132764 | -0.123973 | 0.0 |
Example 1: processing the CustomLogisticRegression class with the following attributes
lr = CustomLogisticRegression(fit_intercept=True, l_rate=0.01, n_epoch=100)Output (a Python dict):
{'coef_': [ 0.10644459, -0.2961112 , -0.27592773, -0.27338684], 'accuracy': 0.75}Example 2: processing the CustomLogisticRegression class with the following attributes
lr = CustomLogisticRegression(fit_intercept=False, l_rate=0.01, n_epoch=100)Output (a Python dict):
{'coef_': [-0.29627229, -0.27640283, -0.27384802], 'accuracy': 0.75}