일부 데이터, x 및 y 값을 제공하는 파일이 있습니다. 내 프로그램은 해당 포인트의 회귀선을 그립니다.하지만 지금 필요한 것은 OY 축에서 값을 찾는 것입니다.이 값은 길어지면 선이 교차합니다.
선을 더 길게 만들고 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
intercept_
LinearRegressor 객체 의 속성을 사용하여 x에서 0과 같은 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()
산출:
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다