每个人我都是数据科学的新手。我正在使用支持向量回归来解决回归问题。使用网格搜索调整SVM参数后,我得到了2.6%的MAPE,但我的MAE和MSE仍然很高。
我已经使用了用户自定义的mape函数。
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import Normalizer
import matplotlib.pyplot as plt
def mean_absolute_percentage_error(y_true, y_pred):
y_true, y_pred = np.array(y_true), np.array(y_pred)
return np.mean(np.abs((y_true - y_pred) / y_true)) * 100
import pandas as pd
from sklearn import preprocessing
features=pd.read_csv('selectedData.csv')
import numpy as np
from scipy import stats
print(features.shape)
features=features[(np.abs(stats.zscore(features)) < 3).all(axis=1)]
target = features['SYSLoad']
features= features.drop('SYSLoad', axis = 1)
names=list(features)
for i in names:
x=features[[i]].values.astype(float)
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
features[i]=x_scaled
发现功能展示
import numpy as np
from sklearn.model_selection import train_test_split
train_input, test_input, train_target, test_target =
train_test_split(features, target, test_size = 0.25, random_state = 42)
trans=Normalizer().fit(train_input);
train_input=Normalizer().fit_transform(train_input);
test_input=trans.fit_transform(test_input);
n=test_target.values;
test_targ=pd.DataFrame(n);
from sklearn.svm import SVR
svr_rbf = SVR(kernel='poly', C=10, epsilon=10,gamma=10)
y_rbf = svr_rbf.fit(train_input, train_target);
predicted=y_rbf.predict(test_input);
plt.figure
plt.xlim(20,100);
print('Total Days For training',len(train_input)); print('Total Days For
Testing',len(test_input))
plt.ylabel('Load(MW) Prediction 3 '); plt.xlabel('Days');
plt.plot(test_targ,'-b',label='Actual'); plt.plot(predicted,'-r',label='RBF
kernel ');
plt.gca().legend(('Actual','RBF'))
plt.title('SVM')
plt.show();
MAPE=mean_absolute_percentage_error(test_target,predicted);
print(MAPE);
mae=mean_absolute_error(test_targ,predicted)
mse=mean_squared_error(test_targ, predicted)
print(mae);
print(mse);
我得到MAPE = 2.56,MAE = 400,MSE = 437696。阿伦·梅(Arent Mae)和摩西(MSE)都很庞大。为什么会这样?我的目标变量sysload包含1万个值
答案 0 :(得分:2)
由于您尚未提供数据,因此我们无法复制您的示例。卜看看这个
y_true = [3, -0.5, 2, 7]
y_pred = [2.5, 0.0, 2, 8]
您的代码
def mean_absolute_percentage_error(y_true, y_pred):
y_true, y_pred = np.array(y_true), np.array(y_pred)
return np.mean(np.abs((y_true - y_pred) / y_true)) * 100
输出
32.73809523809524
让我们比较
mean_squared_error(y_true, y_pred)
0.375
非常接近。功能选择可能有些问题。