【问题标题】:Does performing cross-validation train the model? [duplicate]执行交叉验证会训练模型吗? [复制]
【发布时间】:2022-01-07 10:49:01
【问题描述】:
import pandas as pd
import numpy as np

from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.linear_model import LinearRegression


boston = pd.read_csv('boston.csv')

x = boston.drop('medv', axis=1).values
y = boston['medv'].values


reg = LinearRegression()

cross_val_score(reg, x, y, cv=5)

reg.predict(x)

在上面的代码中,我计算了我的线性回归回归器的 5 折交叉验证分数。但是当我尝试使用predict() 方法时,我收到一条错误消息:

This LinearRegression instance is not fitted yet. Call 'fit' with 
appropriate arguments before using this estimator.

我认为回归器在执行交叉验证时是合适的。

【问题讨论】:

    标签: python machine-learning scikit-learn linear-regression cross-validation


    【解决方案1】:

    您需要先拟合数据:

    reg = LinearRegression()
    reg.fit(x,y)
    

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 2015-12-18
      • 2018-09-01
      • 1970-01-01
      • 2020-08-06
      • 2019-11-27
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      相关资源
      最近更新 更多