Interview Query

Logistic Regression from Scratch

0
Have you seen this question before?

Build a logistic regression model from scratch, with the following conditions:

  • Return the parameters of the regression
  • Do not include an intercept term
  • Use basic gradient descent as your optimization method and the log-likelihood as your loss function.
  • Don’t include a penalty term.
  • If the next iteration of the gradient descent has a lesser loss function than the current iteration, end the optimization and return the current parameters.
  • The iteration step is 0.02 and the maximum number of iterations is 100
  • You may use numpy and pandas but NOT scikit-learn

Example:

Input:

step_size = 0.02
max_steps = 100
starting_point = np.asarray(data.drop('Target').mean())
print(data)
...
        Var1      Var2      Var3  Target
0   0.439326 -0.662463  0.027418       1
1  -1.363880 -1.303938 -1.595875       1
2  -1.710828 -2.473115 -0.968141       1
3  -1.620589  1.321073  1.608265       0
4  -0.315593 -1.655797  0.126860       0
..       ...       ...       ...     ...
95 -0.663844  0.225382  0.650974       0
96  1.479807  0.322281  0.501116       0
97  0.510666 -0.745397 -0.239914       0
98 -0.769290  1.348070 -0.059427       0
99  2.018888 -0.945268 -0.851626       0

[100 rows x 4 columns]

Output:

def logistic_regression(data,starting_point,step_size,steps) -> [0.35155765,-0.30863254,-0.13125343]
Next question: Twenty Variants
.....
Python 3.9.6
Loading editor
Use Shift + Enter to run code