You can download this code by clicking the button below.
This code is now available for download.
This function uses the RandomForestClassifier to fit the training data and predict labels on the test data, returning the model's accuracy.
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 on the training data
clf.fit(X_train, y_train)
# Predict the labels 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