【问题标题】:no error while fitting the model over train data but NotFittedError while predicting over test set在训练数据上拟合模型时没有错误,但在预测测试集时出现 NotFittedError
【发布时间】:2019-06-23 10:16:45
【问题描述】:

使用.predict时出现未拟合错误,拟合期间没有错误

试图将数据帧转换成数组还是一样的错误

输入:

rfg(n_estimators=500,random_state=42).fit(X=data_withoutnull1.iloc[:,1:8],y=data_withoutnull1['LotFrontage'])
rfg(n_estimators=500,random_state=42).predict(datawithnull1.iloc[:,1:8])

输出:

Traceback (most recent call last):

  File "<ipython-input-477-10c6d72bcc12>", line 2, in <module>
    rfg(n_estimators=500,random_state=42).predict(datawithnull1.iloc[:,1:8])

  File "/home/sinikoibra/miniconda3/envs/pv36/lib/python3.6/site-packages/sklearn/ensemble/forest.py", line 691, in predict
    check_is_fitted(self, 'estimators_')

  File "/home/sinikoibra/miniconda3/envs/pv36/lib/python3.6/site-packages/sklearn/utils/validation.py", line 914, in check_is_fitted
    raise NotFittedError(msg % {'name': type(estimator).__name__})

NotFittedError: This RandomForestRegressor instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

【问题讨论】:

    标签: machine-learning scikit-learn random-forest predict


    【解决方案1】:

    试试这样:

    # Define X and y
    X=data_withoutnull1.iloc[:,1:8].values
    y=data_withoutnull1['LotFrontage']
    

    您可以使用训练测试拆分将数据拆分为训练集和测试集,然后将测试集传递给预测。

    #pass X_train to fit -- training the model, fit(X_train)
    #pass X_test to predict -- can be used for prediction, predict(X_test )
    

    或将随机森林回归拟合到数据集

    from sklearn.ensemble import RandomForestRegressor
    rfg= RandomForestRegressor(n_estimators = 500, random_state = 42)
    rfg.fit(X, y)
    
    # Predicting a new result
    y_pred = rfg.predict([[some value here]] or testing set or dataset to be predicted)
    

    【讨论】:

    • 在训练数据上拟合模型时没有错误,但在预测训练集时出现 NotFittedError - 这里的“训练数据”和“训练集”是相同的还是不同的?您说的是使用“训练数据”进行拟合,然后使用“训练集”进行预测,但看起来您使用的是两种不同类型的数据集??data_withoutnull1.iloc[:,1:8] 和 datawithnull1.iloc[:,1 :8] 是两种不同类型的数据集?
    • 不,我只是将数据集分为两部分,一部分具有特征的 null 值,另一部分没有 null ,顺便说一句,由于某种原因 rfg。适合(X,Y)。预测(数据集)计算出来
    猜你喜欢
    • 2021-03-04
    • 2020-03-09
    • 2021-07-07
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 2022-07-31
    • 2019-10-20
    相关资源
    最近更新 更多