import numpy as np import matplotlib.pyplotas plt from sklearn.model_selectionimport train_test_split from sklearn.linear_modelimport LinearRegression from sklearn.metricsimport mean_absolute_error, mean_squared_error, r2_score
# 1. 创建模拟数据
np.random.seed(42)# 确保每次运行结果一致
X =2 * np.random.rand(100,1)# 100个样本,1个特征,范围[0,2)
y =4 + 3 * X + np.random.randn(100,1)# 真实关系:y = 4 + 3x + 噪声