y_pred = np.zeros((y_train.shape[0], 3)) for tree in forest: a = [] for j in range(X_train.shape[1]): if np.median(X_train[:, j]) > np.mean(X_train[:, j]): fuzzy_vals = fuzz.trapmf(X_train[:, j], [np.min(X_train[:, j]), np.mean(X_train[:, j]), np.median(X_train[:, j]), np.max(X_train[:, j])]) else: fuzzy_vals = fuzz.trapmf(X_train[:, j], [np.min(X_train[:, j]), np.median(X_train[:, j]), np.mean(X_train[:, j]), np.max(X_train[:, j])]) a.append(fuzzy_vals) fuzzy_vals = np.array(a).T y_pred += tree.predict_proba(fuzzy_vals) y_pred /= n_trees改成三分类预测代码
时间: 2023-06-10 13:08:59 浏览: 196
如果你的随机森林模型是二分类的,你需要做以下更改来进行三分类预测:
1. 修改 `y_pred` 的形状为 `(y_train.shape[0], 3)`,即有三个类别。
2. 对于每个决策树,预测的输出应该是每个类别的概率,而不是二分类的概率。所以你需要将 `tree.predict_proba(fuzzy_vals)` 改为 `tree.predict_proba(fuzzy_vals.reshape(-1, fuzzy_vals.shape[1], 1)).reshape(-1, 3)`。
3. 对于每个样本的预测值,你需要将它除以 `n_trees`,而不是 `n_trees` 乘以它。
以下是修改后的代码:
```
y_pred = np.zeros((y_train.shape[0], 3))
for tree in forest:
a = []
for j in range(X_train.shape[1]):
if np.median(X_train[:, j]) > np.mean(X_train[:, j]):
fuzzy_vals = fuzz.trapmf(X_train[:, j], [np.min(X_train[:, j]), np.mean(X_train[:, j]), np.median(X_train[:, j]), np.max(X_train[:, j])])
else:
fuzzy_vals = fuzz.trapmf(X_train[:, j], [np.min(X_train[:, j]), np.median(X_train[:, j]), np.mean(X_train[:, j]), np.max(X_train[:, j])])
a.append(fuzzy_vals)
fuzzy_vals = np.array(a).T
y_pred += tree.predict_proba(fuzzy_vals.reshape(-1, fuzzy_vals.shape[1], 1)).reshape(-1, 3)
y_pred /= n_trees
```
阅读全文