CatBoost Random Forest Classification Accuracy Evaluation

  • Share this:

Code introduction


This function uses the CatBoost library's Random Forest classifier for classification. It first splits the dataset into training and testing sets, then trains the model, makes predictions on the test set, and finally evaluates the model's accuracy.


Technology Stack : CatBoost, NumPy, Pandas, Scikit-learn

Code Type : Machine learning classification

Code Difficulty : Intermediate


                
                    
import catboost as cb
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split

def random_forest_classification(X, y):
    # Splitting the dataset into training and testing sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    
    # Creating a CatBoost classifier with Random Forest
    model = cb.CatBoostClassifier(
        iterations=100,
        depth=6,
        learning_rate=0.1,
        loss_function='Logloss',
        random_seed=42
    )
    
    # Training the model
    model.fit(X_train, y_train)
    
    # Predicting on the test set
    y_pred = model.predict(X_test)
    
    # Evaluating the model
    accuracy = model.evaluate(X_test, y_test, show_accuracy=True)
    
    return accuracy