线性回归斜率与数据点的距离

2024-05-03 09:42:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个LinearRegression()模型。我现在要做的基本上是计算一些数据点和回归线之间的距离。在

我的数据点是二维点(x,y)

我的问题是:如何从LinearRegression()模型中得到直线方程?在


Tags: 数据模型距离直线方程linearregression回归线
2条回答

documentation中,使用clf.coef_作为权重向量,使用clf.intercept_作为偏移:

coef_ : array, shape (n_features, ) or (n_targets, n_features)
Estimated coefficients for the linear regression problem. If multiple targets are passed during the fit (y 2D), this is a 2D array of shape (n_targets, n_features), while if only one target is passed, this is a 1D array of length n_features.

intercept_ : array Independent term in the linear model.

一旦你有了这些,请看here。在

拟合模型后,可以调用coefintercept_属性来分别查看系数和截距。在

但这需要为模型编写一个构造好的公式。我的建议是,一旦你建立了你的模型,做出预测并根据真实的y值对其进行评分-

from sklearn.metrics import mean_squared_error
mean_squared_error(y_test, y_pred) # y_test are true values, y_pred are the predictions that you get by calling regression.predict()

如果目标是计算距离,您可以使用sklearn.metrics方便函数,而不是自己寻找方程并手工计算。手动方式是-

^{pr2}$

相关问题 更多 >