You can download this code by clicking the button below.
This code is now available for download.
This function uses the RandomForestClassifier to train a model and evaluate its accuracy on test data. It first initializes a classifier using the RandomForestClassifier from the Scikit-learn library. Then, it fits the model on the training data. Next, it makes predictions on the test data and calculates the accuracy of the model.
Technology Stack : Scikit-learn, RandomForestClassifier, accuracy_score
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)
# Make predictions on the test data
predictions = clf.predict(X_test)
# Calculate the accuracy of the model
accuracy = accuracy_score(y_test, predictions)
return accuracy