Random Forest Classification Accuracy Calculator

  • Share this:

Code introduction


This function uses the Random Forest algorithm to train the given training set and perform classification predictions on the test set, finally returning the accuracy of the model.


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()

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

    # Make predictions using the test set
    predictions = clf.predict(X_test)

    # Calculate the accuracy of the model
    accuracy = accuracy_score(y_test, predictions)

    return accuracy                
              
Tags: