You can download this code by clicking the button below.
This code is now available for download.
This function uses the RandomForestClassifier from Scikit-learn to fit a model on the training data, make predictions on the test data, and then calculate and return the accuracy of the predictions.
Technology Stack : Scikit-learn
Code Type : Machine learning classification
Code Difficulty : Intermediate
def random_forest_classification(X_train, y_train, X_test):
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# Initialize the RandomForestClassifier
clf = RandomForestClassifier(n_estimators=100, random_state=42)
# Fit the model to the training data
clf.fit(X_train, y_train)
# Make predictions on the test data
y_pred = clf.predict(X_test)
# Calculate the accuracy of the model
accuracy = accuracy_score(y_test, y_pred)
return accuracy