File size: 1,472 Bytes
94b723c
 
 
 
 
b944499
94b723c
 
 
 
 
 
 
 
 
 
b944499
94b723c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b944499
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from DecisionTreeRegressor import DecisionTreeRegressor
from sklearn.tree import DecisionTreeRegressor as SKLearnDecisionTreeRegressor
import numpy as np

class RandomForestRegressor:
    def __init__(self, n_estimators, max_depth, min_samples_split, custom=True):
        self.n_estimators = n_estimators
        self.max_depth = max_depth
        self.min_samples_split = min_samples_split
        self.trees = []
        self.custom = custom

    def fit(self, X, y, tree_params=None):
        if tree_params is None:
            tree_params = {
                'max_depth': self.max_depth,
                'min_samples_split': self.min_samples_split
            }

        # Convert X and y to NumPy arrays
        X = np.array(X)
        y = np.array(y)

        for _ in range(self.n_estimators):
            if self.custom:
                tree = DecisionTreeRegressor(**tree_params)
            else:
                tree = SKLearnDecisionTreeRegressor(**tree_params)

            # Bootstrap sampling
            indices = np.random.choice(len(X), len(X), replace=True)
            X_bootstrap = X[indices]
            y_bootstrap = y[indices]

            tree.fit(X_bootstrap, y_bootstrap)
            self.trees.append(tree)

    def predict(self, X):
        predictions = np.zeros((X.shape[0], len(self.trees)))

        for i, tree in enumerate(self.trees):
            predictions[:, i] = tree.predict(X.values)

        return np.mean(predictions, axis=1)