Random Forest Classification Accuracy Evaluation

  • Share this:

Code introduction


This function uses the Random Forest algorithm to classify the training data and evaluates the model's accuracy on the test data.


Technology Stack : scikit-learn

Code Type : Function

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, random_state=42)
    
    # Train the model
    clf.fit(X_train, y_train)
    
    # Make predictions on the test set
    y_pred = clf.predict(X_test)
    
    # Calculate the accuracy of the model
    accuracy = accuracy_score(y_test, y_pred)
    
    return accuracy                
              
Tags: