将三个data = pd.read_csv('/home/w123/Documents/fatigue_detecting-master/TXT-data/5.14/2/Eye aspect ratio.txt') y = data.iloc[:, :-1].values.reshape(-1, 1) X = data.iloc[:, -1].values.reshape(-1, 1) regressor = LinearRegression() regressor.fit(X, y) y_pred = regressor.predict(X) print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor.intercept_[0], regressor.coef_[0][0])) plt.scatter(X, y, color='blue') plt.plot(X, y_pred, color='red') plt.title('Linear Regression') plt.xlabel('Independent Variable') plt.ylabel('Dependent Variable') plt.legend(['Regression Line', 'Observations']) plt.show()画在同一张图上
时间: 2023-06-18 19:07:35 浏览: 108
可以使用如下代码画在同一张图上:
```python
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
data = pd.read_csv('/home/w123/Documents/fatigue_detecting-master/TXT-data/5.14/2/Eye aspect ratio.txt')
y = data.iloc[:, :-1].values.reshape(-1, 1)
X = data.iloc[:, -1].values.reshape(-1, 1)
regressor = LinearRegression()
regressor.fit(X, y)
y_pred = regressor.predict(X)
print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor.intercept_[0], regressor.coef_[0][0]))
plt.scatter(X, y, color='blue')
plt.plot(X, y_pred, color='red')
plt.title('Linear Regression')
plt.xlabel('Independent Variable')
plt.ylabel('Dependent Variable')
plt.legend(['Regression Line', 'Observations'])
# 画第二个图
data2 = pd.read_csv('/home/w123/Documents/fatigue_detecting-master/TXT-data/5.14/2/Another file.txt')
y2 = data2.iloc[:, :-1].values.reshape(-1, 1)
X2 = data2.iloc[:, -1].values.reshape(-1, 1)
regressor2 = LinearRegression()
regressor2.fit(X2, y2)
y_pred2 = regressor2.predict(X2)
print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor2.intercept_[0], regressor2.coef_[0][0]))
plt.scatter(X2, y2, color='green')
plt.plot(X2, y_pred2, color='orange')
plt.legend(['Regression Line 2', 'Observations 2'])
plt.show()
```
这段代码可以读取两个文件,分别进行线性回归,并将两个回归结果画在同一张图上。你需要将代码中的文件路径改为你自己的路径。
阅读全文