完成后的代码如下: ```python import pandas as pd from sklearn.linear_model import LinearRegression #填空(1) from sklearn.model_selection import train_test_split #填空(2) from sklearn.metrics import r2_score import matplotlib.pyplot as plt plt.rcParams['font.family'] = 'fangsong' data = pd.read_csv("health_data.csv") #填空(3) X = data[['age', 'weight', 'bp']] y = data["cholesterol"] #填空(4) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) #填空(5)和(6) model = LinearRegression() #填空(7) model.fit(X_train, y_train) #填空(8) y_pred = model.predict(X_test) #填空(9)和 填空(10) print("系数:", model.coef_) print("截距:", model.intercept_) #填空(11) print("R²分数:", r2_score(y_test, y_pred)) #填空(12) plt.scatter(y_test, y_pred, color='green') plt.xlabel("实际胆固醇") plt.ylabel("预测胆固醇") plt.title('预测值与实际值对比图' ) plt.show() ``` 额外需要注意的逻辑点: 1. 确保`health_data.csv`文件位于当前工作目录中,否则需提供完整路径。 2. 数据集中不能含有缺失值,否则在训练模型时会出错,建议提前做`data.isnull().sum()`检查。 3. 使用`LinearRegression`默认参数即可完成线性拟合,但如需更高精度或正则化,可改用`Ridge`或`Lasso`回归。 [2025-06-29 17:53:11 | AI写代码神器 | 404点数解答]