gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
线性回归,波士顿房价预测中如何输出训练集的模型得分
- User
线性回归,波士顿房价预测中如何输出训练集的模型得分
- Assistant
在使用线性回归模型预测波士顿房价时,可以使用模型的score方法来输出训练集的模型得分。score方法用于计算模型对给定数据集的拟合程度,返回值为R2决定系数,其范围在0到1之间,值越接近1表示模型的拟合程度越好。
以下是一个示例代码,演示了如何使用score方法输出训练集的模型得分:
from sklearn.datasets import load_boston from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split # 加载波士顿房价数据集 boston = load_boston() X = boston.data y = boston.target # 将数据集分割为训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 训练线性回归模型 lr = LinearRegression() lr.fit(X_train, y_train) # 输出训练集模型得分 train_score = lr.score(X_train, y_train) print('训练集模型得分:', train_score)运行以上代码,即可输出训练集的模型得分。