You can download this code by clicking the button below.
This code is now available for download.
This function trains a Random Forest classifier on the training data, predicts labels on the test data, and returns 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 Random Forest Classifier
clf = RandomForestClassifier(n_estimators=100, random_state=0)
# Fit the model on the training data
clf.fit(X_train, y_train)
# Predict the labels for the test data
y_pred = clf.predict(X_test)
# Calculate the accuracy of the model
accuracy = accuracy_score(y_test, y_pred)
return accuracy