#C13885. Implementing Logistic Regression from Scratch
Implementing Logistic Regression from Scratch
Implementing Logistic Regression from Scratch
In this problem, you are required to implement a binary Logistic Regression classifier from scratch using gradient descent. The classifier should perform feature scaling and optimize the following cross‐entropy loss function:
( L = -\frac{1}{N}\sum_{i=1}^{N} \Bigl[y_i \ln(p_i) + (1-y_i) \ln(1-p_i)\Bigr] )
where (p_i) is the predicted probability for sample (i) computed using the sigmoid function: ( \sigma(z) = \frac{1}{1+e^{-z}} ).
You will be given a training dataset and a test dataset via standard input. Your task is to train the logistic regression model on the training data and then predict the labels for the test data. Use a learning rate of 0.1 and 1000 iterations for gradient descent. A prediction is 1 if the predicted probability is greater than 0.5, and 0 otherwise.
Note: The feature scaling must be based on statistics (mean and standard deviation) computed on the training set and then applied to both training and test data.
inputFormat
The input is given via standard input (stdin) with the following format:
The first line contains two integers: (N) and (M), where (N) is the number of training samples and (M) is the number of features.
The next (N) lines each contain (M) floating-point numbers followed by an integer label (0 or 1), separated by spaces.
The next line contains a single integer (K) representing the number of test samples.
The following (K) lines each contain (M) floating-point numbers representing the features of each test sample.
outputFormat
For each test sample, output the predicted label (0 or 1) to standard output (stdout), one per line.## sample
4 2
0.0 0.0 0
0.0 1.0 0
1.0 0.0 1
1.0 1.0 1
2
0.2 0.2
0.8 0.8
0
1
</p>