如何找到回归线与OY轴相交的点?

时间:2019-04-01 13:17:06

标签: python-3.x matplotlib linear-regression

我有一个文件,其中提供了一些数据,即x和y值。我的程序绘制了这些点的回归线,但是我现在需要的是在OY轴上找到值,如果将其拉长,我的线将与之相交。

What my program does now:

我只需要延长线,使其与OY轴相交,然后找到该点的确切坐标即可。

到目前为止,我的代码:

import numpy as np

import matplotlib.pyplot as plt  # To visualize

import pandas as pd  # To read data

from sklearn.linear_model import LinearRegression
data = pd.read_csv('data.csv')  # load data set

X = data.iloc[:, 0].values.reshape(-1, 1)  # values converts it into a numpy array

Y = data.iloc[:, 1].values.reshape(-1, 1)  # -1 means that calculate the dimension of rows, but have 1 column

linear_regressor = LinearRegression()  # create object for the class

linear_regressor.fit(X, Y)  # perform linear regression

Y_pred = linear_regressor.predict(X)  # make predictions

plt.scatter(X, Y)

plt.plot(X, Y_pred, color='red')

plt.show()

我的代码需要一个名为“ data.csv”的文件,其中包含给定值的坐标。我的示例具有以下值:

5,0.8
10,0.7
15,0.66
20,0.493
25,0.5
30,0.21

1 个答案:

答案 0 :(得分:2)

您是否想要这样的东西,您可以在其中使用LinearRegressor对象的intercept_属性来获得x等于零的y截距:

import numpy as np
import matplotlib.pyplot as plt  # To visualize
import pandas as pd  # To read data
from io import StringIO
from sklearn.linear_model import LinearRegression
txtfile = StringIO("""5,0.8
10,0.7
15,0.66
20,0.493
25,0.5
30,0.21""")
data = pd.read_csv(txtfile, header=None)  # load data set

X = data.iloc[:, 0].values.reshape(-1, 1)  # values converts it into a numpy array

Y = data.iloc[:, 1].values.reshape(-1, 1)  # -1 means that calculate the dimension of rows, but have 1 column

linear_regressor = LinearRegression()  # create object for the class

linear_regressor.fit(X, Y)  # perform linear regression

Y_pred = linear_regressor.predict(X)  # make predictions

plt.scatter(X, Y)

plt.plot(X, Y_pred, color='red')

plt.plot([0, X[0]], [linear_regressor.intercept_, Y_pred[0]], c="green",  linestyle='--')

ax = plt.gcf().gca()
ax.spines['left'].set_position('zero')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

plt.show()

输出:

enter image description here