Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
def rfc_test_accuracy(X, y):
"""
Function which takes the predictor and target variables and returns the test accuracy of the model.
"""
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
RFC = RandomForestClassifier(random_state=123)
RFC.fit(X_train,y_train)
test_accuracy = accuracy_score(y_test, RFC.predict(X_test))
return test_accuracy
def rfc_mean(X,y,trails=20):
"""
Print the mean value of Random forest classifier for n trails.
"""
result = [rfc_test_accuracy(X,y) for i in range(trails)]
mean = np.array(result).mean()
return mean
print("Predictive accuracy of base random forrest classifier ",round(rfc_mean(df.drop('Component', axis=1),df['Component']),3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment