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

Viewed 79

I have my coding lines such as :

import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from sklearn.metrics import roc_curve, roc_auc_score

X = dataset_df
Y = dataset_df

'X_train, X_test, y_train, y_test\
= train_test_split(X, y, test_size = 0.3, random_state=1)'

X_train, X_validation, y_train, y_validation\
= train_test_split(X_train, y_train, test_size = 0.3, random_state=1)

sc = StandardScaler()
sc.fit(X_train)
X_train_Std = sc.transform(X_train)

lr_classifier = LogisticRegression(C = 1000, random_state= 1)
rf_classifier = RandomForestClassifier(max_depth=5, random_state= 1)
from sklearn.ensemble import RandomForestClassifier
fit 

rf_classifier.predict_proba(sc.transform(X_validation))

And there, I get an error saying that the RandomForestClassifier isn't fitted. This RandomForestClassifier instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator. Would someone know how to help me on this?

1 Answers

LogisticRegression as well as RandomForestClassifier are both classes and lr_classifier and rf_classifier are objects of the these classes.

You need to call the fit method in a similar way as you did with the object sc, something like lr_classifier.fit(X_train_Std, y_train) and not just fit.

I would recommend reading the sklearn Getting Started guide to better understand how to use the library.

Related