【代码模版】以MAPE为指标的评估函数模版

该评估函数可用于回归类算法模型评估

# 定义评估函数
# 注意:传入的x_test, y_test参数必须是经过np.array()转化过的
# 所以为了保持一致性,如果要使用该评估方法,最好在划分训练集测试集时就将训练集和测试集np.array()转换。确保模型训练以及后面的评估都使用array格式。
def mape_evaluate(model, x_test, y_test):
    predictions = model.predict(x_test)
    errors = abs(predictions - y_test)
    mape = 100 * np.mean(errors / y_test)
    accuracy = 100 - mape  # 该准确率是减去mape误差得到的

    print('平均绝对误差.',np.mean(errors))
    print('基于mape的准确率 = {:0.2f}%.'.format(accuracy))
发布了22 篇原创文章 · 获赞 0 · 访问量 921

猜你喜欢

转载自blog.csdn.net/weixin_44680262/article/details/104630323