Skip to content

Instantly share code, notes, and snippets.

@Amitdb123
Created February 18, 2021 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Amitdb123/1e191a36d1f36b7bdfcf13375e63694b to your computer and use it in GitHub Desktop.
Save Amitdb123/1e191a36d1f36b7bdfcf13375e63694b to your computer and use it in GitHub Desktop.
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