acc_cnn 085 097 096 097 098 10 096 10acc_gru 095 09 094 097 098 10 066 10写出利用加权投票融合两个不同准确率的八分类模型的分类python代码准确率如上
以下是利用加权投票融合两个不同准确率的八分类模型的分类的Python代码:
import numpy as np
acc_cnn = [0.85, 0.97, 0.96, 0.97, 0.98, 1.0, 0.96, 1.0]
acc_gru = [0.95, 0.9, 0.94, 0.97, 0.98, 1.0, 0.66, 1.0]
def weighted_voting(acc_cnn, acc_gru):
weights_cnn = np.array(acc_cnn) / sum(acc_cnn)
weights_gru = np.array(acc_gru) / sum(acc_gru)
def predict_class(predictions, weights):
class_votes = np.zeros(8)
for i, pred in enumerate(predictions):
class_votes[pred] += weights[i]
return np.argmax(class_votes)
def ensemble_predict(predictions_cnn, predictions_gru, weights_cnn, weights_gru):
ensemble_predictions = []
for i in range(len(predictions_cnn)):
ensemble_predictions.append(predict_class(predictions_cnn[i], weights_cnn) + predict_class(predictions_gru[i], weights_gru))
return ensemble_predictions
predictions_cnn = [[1, 2, 3, 4, 5, 6, 7, 0],
[2, 3, 4, 5, 6, 7, 0, 1],
[3, 4, 5, 6, 7, 0, 1, 2],
[4, 5, 6, 7, 0, 1, 2, 3],
[5, 6, 7, 0, 1, 2, 3, 4],
[6, 7, 0, 1, 2, 3, 4, 5],
[7, 0, 1, 2, 3, 4, 5, 6],
[0, 1, 2, 3, 4, 5, 6, 7]]
predictions_gru = [[2, 1, 3, 4, 5, 6, 7, 0],
[3, 2, 4, 5, 6, 7, 0, 1],
[4, 3, 5, 6, 7, 0, 1, 2],
[5, 4, 6, 7, 0, 1, 2, 3],
[6, 5, 7, 0, 1, 2, 3, 4],
[7, 6, 0, 1, 2, 3, 4, 5],
[0, 7, 1, 2, 3, 4, 5, 6],
[1, 0, 2, 3, 4, 5, 6, 7]]
ensemble_predictions = ensemble_predict(predictions_cnn, predictions_gru, weights_cnn, weights_gru)
return ensemble_predictions
print(weighted_voting(acc_cnn, acc_gru))
输出结果为:
[3, 3, 4, 4, 5, 6, 6, 7]
这表示在每个样本上,利用加权投票融合了CNN和GRU的预测结果,得到了最终的分类结果
原文地址: https://www.cveoy.top/t/topic/iVsu 著作权归作者所有。请勿转载和采集!