-
-
Save Amitdb123/1e191a36d1f36b7bdfcf13375e63694b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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