(self, X)
| 336 | self.trees[i].fit(X_subset, y_subset) |
| 337 | |
| 338 | def predict(self, X): |
| 339 | y_preds = np.empty((X.shape[0], len(self.trees))) |
| 340 | # 每棵决策树都在数据上预测 |
| 341 | for i, tree in enumerate(self.trees): |
| 342 | # 基于特征做出预测 |
| 343 | prediction = tree.predict(X) |
| 344 | y_preds[:, i] = prediction |
| 345 | |
| 346 | y_pred = [] |
| 347 | # 对每个样本,选择最常见的类别作为预测 |
| 348 | for sample_predictions in y_preds: |
| 349 | y_pred.append(np.bincount(sample_predictions.astype('int')).argmax()) |
| 350 | return y_pred |
| 351 | |
| 352 | def score(self, X, y): |
| 353 | y_pred = self.predict(X) |