Random Forest

import numpy as np

import pandas as pd

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split

from sklearn.tree import DecisionTreeClassifier

from sklearn.metrics import accuracy_score, classification_report

from sklearn.utils import resample

class CustomRandomForest:

    def __init__(self, n_estimators=100, max_features='sqrt', random_state=None):

        self.n_estimators = n_estimators

        self.max_features = max_features

        self.random_state = random_state

        self.trees = []

        np.random.seed(self.random_state)

    def fit(self, X, y):

        # Create multiple bootstrapped datasets and train decision trees

        for _ in range(self.n_estimators):

            # Create a bootstrapped sample

            X_bootstrap, y_bootstrap = resample(X, y, random_state=self.random_state)

            tree = DecisionTreeClassifier(max_features=self.max_features, random_state=self.random_state)

            tree.fit(X_bootstrap, y_bootstrap)

            self.trees.append(tree)

    def predict(self, X):

        # Aggregate predictions from all trees

        predictions = np.array([tree.predict(X) for tree in self.trees])

        # Take the majority vote

        return np.array([np.bincount(pred).argmax() for pred in predictions.T])

# Load the iris dataset

iris = load_iris()

X = iris.data

y = iris.target

# Split 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)

# Create a Custom Random Forest Classifier

custom_rf_model = CustomRandomForest(n_estimators=100, random_state=42)

# Train the model

custom_rf_model.fit(X_train, y_train)

# Make predictions

y_pred = custom_rf_model.predict(X_test)

# Calculate accuracy

accuracy = accuracy_score(y_test, y_pred)

print(f'Accuracy: {accuracy:.2f}')

# Print classification report

print(classification_report(y_test, y_pred))


Comments

Popular posts from this blog

About me

A set of documents that need to be classified, use the Naive Bayesian Classifier

Keras