data2 = pd.read_csv('/home/w123/Documents/2.txt') y2 = data.iloc[:, :-1].values.reshape(-1, 1) X2 = data.iloc[:, -1].values.reshape(-1, 1) regressor2 = LinearRegression() regressor2.fit(X2, y2) y2_pred = 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, y2_pred, color='orange') plt.legend(['Regression Line 2', 'Observations 2']) #3 data3 = pd.read_csv('/home/w123/Documents/3.txt') y3 = data.iloc[:, :-1].values.reshape(-1, 1) X3 = data.iloc[:, -1].values.reshape(-1, 1) regressor3 = LinearRegression() regressor3.fit(X, y) y3_pred = regressor3.predict(X) print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor3.intercept_[0], regressor.coef_[0][0])) plt.scatter(X3, y3, color='purple') plt.plot(X3, y3_pred, 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()哪里不对
时间: 2023-06-19 09:05:54 浏览: 127
Serial_python.zip
首先,第一行中的文件路径 '/home/w123/Documents/2.txt' 和第三行中的文件路径 '/home/w123/Documents/3.txt' 并不一致,需要保持一致才能正确读取数据。
其次,在第二段代码中,变量名使用了 'y2' 和 'X2',但是在第四行中却使用了 'y' 和 'X',这也需要保持一致性才能正确执行代码。
修改后的代码如下所示:
```python
import pandas as pd
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
# read data from files
data1 = pd.read_csv('/home/w123/Documents/1.txt')
data2 = pd.read_csv('/home/w123/Documents/2.txt')
data3 = pd.read_csv('/home/w123/Documents/3.txt')
# extract data for first regression
y1 = data1.iloc[:, :-1].values.reshape(-1, 1)
X1 = data1.iloc[:, -1].values.reshape(-1, 1)
# train first regression model
regressor1 = LinearRegression()
regressor1.fit(X1, y1)
# predict values and print regression equation for first regression
y1_pred = regressor1.predict(X1)
print("Regression Function 1: y = {:.2f} + {:.2f}x".format(regressor1.intercept_[0], regressor1.coef_[0][0]))
# extract data for second regression
y2 = data2.iloc[:, :-1].values.reshape(-1, 1)
X2 = data2.iloc[:, -1].values.reshape(-1, 1)
# train second regression model
regressor2 = LinearRegression()
regressor2.fit(X2, y2)
# predict values and print regression equation for second regression
y2_pred = regressor2.predict(X2)
print("Regression Function 2: y = {:.2f} + {:.2f}x".format(regressor2.intercept_[0], regressor2.coef_[0][0]))
# extract data for third regression
y3 = data3.iloc[:, :-1].values.reshape(-1, 1)
X3 = data3.iloc[:, -1].values.reshape(-1, 1)
# train third regression model
regressor3 = LinearRegression()
regressor3.fit(X3, y3)
# predict values and print regression equation for third regression
y3_pred = regressor3.predict(X3)
print("Regression Function 3: y = {:.2f} + {:.2f}x".format(regressor3.intercept_[0], regressor3.coef_[0][0]))
# plot all observations and regression lines
plt.scatter(X1, y1, color='blue')
plt.plot(X1, y1_pred, color='red')
plt.scatter(X2, y2, color='green')
plt.plot(X2, y2_pred, color='orange')
plt.scatter(X3, y3, color='purple')
plt.plot(X3, y3_pred, 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()
```
阅读全文