Random Forest Classification Accuracy Calculator

  • Share this:

Code introduction


This function generates a random dataset, splits it into training and test sets, trains a random forest classifier, makes predictions on the test set, and calculates the accuracy of the predictions.


Technology Stack : Scikit-learn, NumPy

Code Type : Function

Code Difficulty : Intermediate


                
                    
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

def classify_data(n_samples=1000, n_features=20):
    # Generate a random dataset
    X, y = make_classification(n_samples=n_samples, n_features=n_features, random_state=42)
    
    # Split the dataset into training and test sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    
    # Create a random forest classifier
    clf = RandomForestClassifier(n_estimators=100, random_state=42)
    
    # Train the classifier
    clf.fit(X_train, y_train)
    
    # Make predictions
    y_pred = clf.predict(X_test)
    
    # Calculate the accuracy of the classifier
    accuracy = accuracy_score(y_test, y_pred)
    
    return accuracy