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 

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[0]
    y = arr[1]
    weights = arr[2]
    bias = arr[3]
    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)


2.900000098664297 4.700000044847409

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)
  • $\begingroup$ 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. $\endgroup$ Apr 23 at 9:24
  • $\begingroup$ I think that the sign of the learning rate is wrong in the parameter update in your code. $\endgroup$
    – noe
    Apr 23 at 9:41
  • $\begingroup$ Please, consider marking the answer as correct if deemed so. $\endgroup$
    – noe
    Apr 26 at 8:00
  • $\begingroup$ Hi Noe, yeah of course. Just getting used to using the Stack platform. I tried to upvote earlier, but I couldn't as I don't have enough reputation and missed the accept mark below. I appreciate your help. Cheers $\endgroup$ Apr 26 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.