Math behind the plot in "Visualizing tweets and Logistic Regression models"

Hi everyone!

Is there anyone who knows the math behind this?
In the lab task, “Visualizing tweets and Logistic Regression models”, there is a section called “Plot the model alongside the data”. There, we have to do some stuff to visualize the data but I don’t know the mechanism of that, such as how to calculate the green&red lines and the reason why the green&red lines are perpendicular to the gray line, etc.
If you know any references for this, I’d really appreciate it if you share them! :slight_smile:

Hi Cossy,

This confused me for a moment too, but I will try to explain it and I hope it helps.

The red and green lines determine the direction of classification along the sides of the grey separation line, anything to the right of that line is going to be considered positive, while anything to its left will be considered negative this explains why it should be perpendicular, and hence we need it to be perpendicular to our line then the slope of this two direction arrows has the following equation :
slope(direction) of arrows = -1 / slope of the grey separation line
and hence the equation of our grey separation line is 𝑛𝑒𝑔=(−𝜃0−𝜃1∗𝑝𝑜𝑠)/𝜃2
then its slope is −𝜃1/𝜃2
so the slope or direction of the arrows is 𝜃2/𝜃1
and we only care about the direction of the arrows, not the magnitude ( it doesn’t matter what is the length of the arrow), so the final equation of the arrows is 𝑑𝑖𝑟𝑒𝑐𝑡𝑖𝑜𝑛=𝑝𝑜𝑠∗𝜃2/𝜃1.

finally, when plotting the arrows we wanted to adjust the directions being once to the right (positive) and once to the opposite direction (we added a negative).

you can check that for more explanation: Perpendicular Line Formula with Solved Examples.

3 Likes

Hi,

perpendicular of y = a + bx is y = a' -1/bx. Similarly, perpendicular of neg = \frac{-\theta_0}{\theta_2} - \frac{\theta_1*pos}{\theta_2} is perp_{neg} = c + \frac{\theta_2*pos}{\theta_1}. So, it has a direction of \frac{\theta_2*pos}{\theta_1}. Now, we want to make sure these green and red perpendicular arrows can be seen in the plot so we start from an offset (horizontal axe of 5000) but you can try with different values and play with it.

1 Like