Random Forest Classification Accuracy Calculation

  • Share this:

Code introduction


This function uses a RandomForestClassifier to train the data, make predictions on the test set, and finally return the accuracy of the predictions.


Technology Stack : Scikit-learn

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
def random_forest_classification(X_train, y_train, X_test):
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.metrics import accuracy_score
    
    # Create a RandomForestClassifier instance
    clf = RandomForestClassifier(n_estimators=100)
    
    # Train the classifier
    clf.fit(X_train, y_train)
    
    # Make predictions on the test set
    predictions = clf.predict(X_test)
    
    # Calculate the accuracy of the classifier
    accuracy = accuracy_score(y_test, predictions)
    
    return accuracy                
              
Tags: