python 复制代码 import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error, r2_score import matplotlib.pyplot as plt
生成模拟数据
np.random.seed(0) X = np.random.rand(100, 1) * 10 y = 2.5 * X.squeeze() + np.random.randn(100) * 2
数据划分
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
创建并训练模型
model = LinearRegression() model.fit(X_train, y_train)
预测
y_pred = model.predict(X_test)
评估模型
mse = mean_squared_error(y_test, y_pred) r2 = r2_score(y_test, y_pred) print(f'Mean Squared Error: {mse}') print(f'R^2 Score: {r2}')
可视化
plt.scatter(X_test, y_test, color='black', label='Actual data') plt.plot(X_test, y_pred, color='blue', linewidth=3, label='Predicted data') plt.xlabel('Feature') plt.ylabel('Target') plt.title('Linear Regression') plt.legend() plt.show()
可视化残差
residuals = y_test - y_pred plt.scatter(X_test, residuals) plt.axhline(y=0, color='r', linestyle='-') plt.xlabel('Feature') plt.ylabel('Residual') plt.title('Residuals Plot') plt.show()