改成三分类代码n_trees = 100 max_depth = 10 forest = [] for i in range(n_trees): idx = np.random.choice(X_train.shape[0], size=X_train.shape[0], replace=True) X_sampled = X_train[idx, :] y_sampled = y_train[idx] X_fuzzy = [] for j in range(X_sampled.shape[1]): if np.median(X_sampled[:, j])> np.mean(X_sampled[:, j]): fuzzy_vals = fuzz.trapmf(X_sampled[:, j], [np.min(X_sampled[:, j]), np.mean(X_sampled[:, j]), np.median(X_sampled[:, j]), np.max(X_sampled[:, j])]) else: fuzzy_vals = fuzz.trapmf(X_sampled[:, j], [np.min(X_sampled[:, j]), np.median(X_sampled[:, j]), np.mean(X_sampled[:, j]), np.max(X_sampled[:, j])]) X_fuzzy.append(fuzzy_vals) X_fuzzy = np.array(X_fuzzy).T tree = RandomForestClassifier(n_estimators=1, max_depth=max_depth) tree.fit(X_fuzzy, y_sampled) forest.append(tree) inputs = keras.Input(shape=(X_train.shape[1],)) x = keras.layers.Dense(64, activation="relu")(inputs) x = keras.layers.Dense(32, activation="relu")(x) outputs = keras.layers.Dense(1, activation="sigmoid")(x) model = keras.Model(inputs=inputs, outputs=outputs) model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"]) y_pred = np.zeros(y_train.shape) 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)[:, 1] y_pred /= n_trees model.fit(X_train, y_pred, epochs=10, batch_size=32) y_pred = model.predict(X_test) mse = mean_squared_error(y_test, y_pred) rmse = math.sqrt(mse) print('RMSE:', rmse) print('Accuracy:', accuracy_score(y_test, y_pred))
时间: 2023-06-13 20:02:03 浏览: 79
idx_brch.rar_idx_brch_power quality_runpf
这段代码实现了一个使用模糊隶属度将原始特征转换为模糊特征的随机森林分类器,并将其与一个神经网络模型进行集成。与之前不同的是,这里使用的是三分类问题,即目标变量有三个可能的取值。
具体来说,代码首先定义了一个包含100个树且最大深度为10的随机森林分类器,然后对每个树进行如下操作:从训练集中随机选择一个样本集,然后使用模糊隶属度将特征转换为模糊特征,并用转换后的特征和目标变量训练一个决策树。所有树训练完毕后,使用模糊隶属度将训练集特征转换为模糊特征,并对每个树的预测结果取平均。这样得到的预测结果作为神经网络的输入,再用标准的二分类交叉熵损失和Adam优化器训练神经网络。
最终,代码输出了测试集上的均方根误差和准确率。注意,这里使用的是均方根误差而不是分类准确率,因为模型输出的是概率值而不是类别标签。
阅读全文