MAPE(Mean Absolute Percentage Error),又称平均绝对百分比误差,是一种常用的评价指标,用来衡量预测值与实际值之间的误差程度。在Python中计算MAPE的方法和步骤如下:
import numpy as np import pandas as pd from sklearn.metrics import mean_absolute_error from sklearn.metrics import mean_squared_error from sklearn.metrics import r2_score from sklearn.metrics import mean_squared_log_error
# 加载数据集 data = pd.read_csv('data.csv') # 分割数据集为训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(data[['x1','x2','x3']], data['y'], test_size=0.2, random_state=0)
# 使用线性回归模型训练模型 model = LinearRegression() model.fit(X_train, y_train) # 使用模型预测测试集 y_pred = model.predict(X_test)
# 计算实际值与预测值之间的误差 error = abs(y_pred - y_test) # 计算实际值与预测值之间的百分比误差 mape = 100 * (error / y_test) # 计算MAPE mape = np.mean(mape)
# 输出MAPE print("MAPE:", mape)
本文链接:http://task.lmcjl.com/news/9681.html