| 399 | self.trees[i].fit(X_subset, y_subset) |
| 400 | |
| 401 | def predict(self, X): |
| 402 | y_preds = np.empty((X.shape[0], len(self.trees))) |
| 403 | # 每棵决策树都在数据上预测 |
| 404 | for i, tree in enumerate(self.trees): |
| 405 | # 使用该决策树训练使用的特征 |
| 406 | idx = tree.feature_indices |
| 407 | # 基于特征做出预测 |
| 408 | prediction = tree.predict(X[:, idx]) |
| 409 | y_preds[:, i] = prediction |
| 410 | |
| 411 | y_pred = [] |
| 412 | # 对每个样本,选择最常见的类别作为预测 |
| 413 | for sample_predictions in y_preds: |
| 414 | y_pred.append(np.bincount(sample_predictions.astype('int')).argmax()) |
| 415 | return y_pred |
| 416 | |
| 417 | def score(self, X, y): |
| 418 | y_pred = self.predict(X) |