Perceptron model fitting code intuition

Sir, the below code is from the ‘Perceptron’ Class’s ‘Fit’ fn. :

I understand it as below :

If y=1 & y_pred=0 then add the current wt. to x & increment ‘b’ by 1
If y=0 & y_pred=1 then deduct the current wt. from x & decrement ‘b’ by 1

My doubts wrt the above code Sir :

  • Is my understanding correct… Can you please explain the intuition behind it
  • In either case since we’re dealing with y ≠ y_pred then shouldn’t the logic be the same for both?
  • What’s the intuition behind :
    • adding/deducting the current wt. to/from x &
    • incrementing/decrementing 1 to ‘b’ value

Sir, can you please explain the intuition behind the code & the concept.

Thank you.

Here is a bit of intuition.
W and b are basically parameters of a plane/line (blue line in figure below) which separates points with y=1 (marked +) from points with y=0 (marked -). Notice that two of the points in circles are on the wrong side of separating line. These represent the two type of errors which the code is handling.
To correct any of the error the line will need to be moved/shifted. For one type of error it will need to be moved towards left, while for other it will need to be moved towards right. Adding and subtracting x from w produces the effect of shifting the line towards left or right depending on the error (you can verify this by adding and subtracting two vectors and see how the resultant vectors moves in different directions)

Screenshot 2020-12-09 at 1.08.27 PM

Thank you so much for such a detailed explanation Sir.

Makes sense now!!