使用.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.
答案 0 :(得分:0)
尝试这样:
# 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)