# Preparing for interview - Logistic regression question

So I'm doing some exercises to prepare for a interview test. However there's one of the assignments I don't understand. Maybe some of you can explain what they want me to do? It would help me to understand the underlying questionframes I may come across.

This is the assignment:

Logit Regression Have the function LogitRegression(arr) read the input array of 4 numbers x, y, a, b, separated by space, and return an output of two numbers for updated a and b (assume the learning rate is 1). Save up to 3 digits after the decimal points for a and b. The output should be a string in the format: a, b

  def LogitRegression(arr):

# code goes here
return arr

# keep this function call here
print(LogitRegression(input()))


Logistic regression is a simple approach to do classification, and the same formula is also commonly used as the output layer in neural networks. We assume both the input and output variables are scalars, and the logistic regression can be written as:
y = 1.0 / (1.0 + exp(-ax - b))
After observing a data example (x, y), the parameter a and b can be updated using gradient descent with a learning rate. Examples:
Input: [1, 1, 1, 1]
Output: 0.881, 0.881
Input: [2.2, 0.0, 5.1, 5.7]
Output: 7.3, 6.7

What I don't understand is, do they only give me 4 scalars and want me to train on x, y and then predict a and b. Or is a and be supposed to be weights and bias and I need to return the trained ones? The output numbers in the second example doesn't match probabilities so it can't be that. I might be overthinking this and it's just a simple thing I need to do?

This is the code I've tried:

arr = np.array([2.2, 0.0, 5.1, 5.7])
arr2 = np.array([1.0, 1.0, 1.0,1.0])

def Logit(arr):
learnrate = 1
X = arr
y = arr
weights = arr
bias = arr

y_hat = 1/(1+np.exp(np.dot(X, -weights) - bias))
new_weights = weights + learnrate * (y - y_hat) * X
new_bias = bias + learnrate*(y - y_hat)
print(new_weights, new_bias)

Logit(arr)


Output:

2.900000098664297 4.700000044847409

• Why did you multply X with (y - y_hat) while calculating new weight. Thanks Aug 1, 2022 at 20:57

They give you the current values of the model parameters $$a$$ and $$b$$, and a new data point $$(x,y)$$, and they request to perform one training step with gradient descent using the data point, and returning the updated values for $$a$$ and $$b$$.

The problem with your code is that the sign of the learning rate is wrong in the parameter update. If you change it to minus, the result is as expected:

new_weights = weights - learnrate * (y - y_hat) * X
new_bias = bias - learnrate*(y - y_hat)

• Yeah that was also what I thought, and tried. But my result doesn't match theirs. I have updated the question with the code I've used. Apr 23, 2021 at 9:24
• I think that the sign of the learning rate is wrong in the parameter update in your code.
– noe
Apr 23, 2021 at 9:41