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()再加一个文件
时间: 2023-06-18 15:07:19 浏览: 148
你需要将第三个文件的路径传递给程序,然后对其进行类似的处理。以下是修改后的代码:
```
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 1: 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')
# 读取第二个文件
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 2: 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')
# 读取第三个文件
data3 = pd.read_csv('/path/to/third/file.txt')
y3 = data3.iloc[:, :-1].values.reshape(-1, 1)
X3 = data3.iloc[:, -1].values.reshape(-1, 1)
regressor3 = LinearRegression()
regressor3.fit(X3, y3)
y_pred3 = regressor3.predict(X3)
print("Regression Function 3: y = {:.2f} + {:.2f}x".format(regressor3.intercept_[0], regressor3.coef_[0][0]))
# 画第三个图
plt.scatter(X3, y3, color='purple')
plt.plot(X3, y_pred3, color='yellow')
plt.title('Linear Regression')
plt.xlabel('Independent Variable')
plt.ylabel('Dependent Variable')
plt.legend(['Regression Line 1', 'Observations 1', 'Regression Line 2', 'Observations 2', 'Regression Line 3', 'Observations 3'])
plt.show()
```
请将`/path/to/third/file.txt`替换为实际的第三个文件路径。
阅读全文