Part 17 (15 points, coding task)
Construct a class called My_Log_Reg whose objects are logistic regression models.
-
Method
__init__-
Inputs
-
solver: The value must beGDorNewton. Otherwise, it raises an error messageInvalid solver. -
lr: The learning rate. -
num_iter: The total number of iterations.
-
-
Attributes
-
solver -
lr -
num_iter -
coef_: \mathbf{\beta} in our theoretical model. It shall be a 1-dim numpy array with shape(d,).
-
-
-
Method
fit-
Inputs
-
X: Features in a training dataset. The shape is(N_train,d). -
y: Ground-truth labels in a training dataset. The shape is(N_train,)
-
-
In the body of this method
-
Use the configured solver to compute
coef_. -
Do whole-batch iteration.
-
After finishing training
coef_, generate a plot about the loss function vs iteration.-
The x-label is
iter. -
The y-label is
loss. -
The title is the optimization method: either
GDorNetwon.
-
-
The only loop that you can use is the whole-batch iteration. Within each iteration, when you update
coef_by applying eitherGDorNewton, you are not allowed to use any loop.
-
-
Output
- None
-
-
Method
predict-
Input
X: Features in a test dataset. The shape is(N_test,d).
-
Output
y_pred: Predicted labels in the test dataset. The shape is(N_test,).
-
-
Method
score-
Input
-
X: Features in a test dataset. The shape is(N_test,d). -
y: Ground-truth labels in the test dataset. The shape is(N_test,).
-
-
Output
accuracy_score: The accuracy score of the prediction ofy.
-