Random Forest Classification Accuracy Calculation

  • Share this:

Code introduction


This function uses the Random Forest algorithm to classify the given training set and make predictions on the test set, returning the accuracy of the predictions.


Technology Stack : scikit-learn

Code Type : Machine learning

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)

    # Train the model
    clf.fit(X_train, y_train)

    # Make predictions
    predictions = clf.predict(X_test)

    # Calculate accuracy
    accuracy = accuracy_score(y_test, predictions)

    return accuracy                
              
Tags: