Reputation: 816
I created a best fit line for a collection of data points. Now I want to transpose each data point onto the best fit line. Each point needs to be the shortest distance from the best fit line when it is transposed. All I have are the slope and intercept of the best fit line too.
Upvotes: 1
Views: 372
Reputation: 637
try doing something like this
# this module will be useful
import math
# your point to move
point = [x, y]
# use your slope and intercept
m = slope
b = intercept
# get two points from the line
x1, y1 = 0, b
x2 = 1
y2 = m*x2+b
# get line in vector form
line = [x2-x1, y2-y1]
# normalize
norm = math.hypot(line[0], line[1])
norm_line = [line[0]/norm, line[1]/norm]
# project point onto norm_line
comp = (norm_line[0]*point[0]+norm_line[1]*point[1])
proj = [norm_line[0]*comp, norm_line[1]*comp]
# this should be your new point
new_point = [proj[0]+x1, proj[1]+y1]
Upvotes: 1