def _train(self): # Initialize model with zeros y_pred = np.zeros(self.n_samples, np.float32) # Or mean # y_pred = np.full(self.n_samples, self.y_mean) for n in range(self.n_estimators): residuals = self.loss.grad(self.y, y_pred) tree = Tree(regression=True, criterion=mse_criterion) # Pass multiple target values to the tree learner targets = { # Residual values 'y': residuals, # Actual target values 'actual': self.y, # Predictions from previous step 'y_pred': y_pred } tree.train(self.X, targets, max_features=self.max_features, min_samples_split=self.min_samples_split, max_depth=self.max_depth, loss=self.loss) predictions = tree.predict(self.X) y_pred += self.learning_rate * predictions self.trees.append(tree)
def __init__(self, n_estimators=10, max_features=None, min_samples_split=10, max_depth=None, criterion='mse'): super(RandomForestRegressor, self).__init__(n_estimators=n_estimators, max_features=max_features, min_samples_split=min_samples_split, max_depth=max_depth) if criterion == 'mse': self.criterion = mse_criterion else: raise ValueError() # Initialize empty regression trees for _ in range(self.n_estimators): self.trees.append(Tree(regression=True, criterion=self.criterion))
def __init__(self, n_estimators=10, max_features=None, min_samples_split=10, max_depth=None, criterion='entropy'): super(RandomForestClassifier, self).__init__(n_estimators=n_estimators, max_features=max_features, min_samples_split=min_samples_split, max_depth=max_depth, criterion=criterion) if criterion == 'entropy': self.criterion = information_gain else: raise ValueError() # Initialize empty trees for _ in range(self.n_estimators): self.trees.append(Tree(criterion=self.criterion))