Random Forest Classification Accuracy Calculation

  • Share this:

Code introduction


This function uses the RandomForestClassifier from the scikit-learn library to perform classification on the given feature set X and label set y, and returns the accuracy on the test set.


Technology Stack : scikit-learn

Code Type : Machine learning classification function

Code Difficulty : Intermediate


                
                    
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

def random_forest_classification(X, y, test_size=0.2):
    # Split the data into training and test sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=42)
    
    # Initialize the RandomForestClassifier
    model = RandomForestClassifier(n_estimators=100, random_state=42)
    
    # Train the model
    model.fit(X_train, y_train)
    
    # Make predictions on the test set
    y_pred = model.predict(X_test)
    
    # Calculate the accuracy of the model
    accuracy = accuracy_score(y_test, y_pred)
    
    return accuracy                
              
Tags: