Reputation: 41
Not fitted error coming up when using .predict,during fit there is no error
tried to convert dataframe into arrays still same error
Input:
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])
Output:
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.
Upvotes: 0
Views: 179
Reputation: 1
Try like this :
# Define X and y
X=data_withoutnull1.iloc[:,1:8].values
y=data_withoutnull1['LotFrontage']
You can use train test split to split the data into training set and testing set then pass the testing set into predict.
#pass X_train to fit -- training the model, fit(X_train)
#pass X_test to predict -- can be used for prediction, predict(X_test )
or Fitting Random Forest Regression to the dataset
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)
Upvotes: 0