简体   繁体   English

使用python将数据点移动到最佳拟合线上

[英]Moving data points onto best fit line using python

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. 我所拥有的也是最佳拟合线的斜率和截距。

在此处输入图片说明

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM