#MAPE SVR
####### K-fold cross validation #######
# Defining a custom function to calculate accuracy
# Make sure there are no zeros in the Target variable if you are using MAPE
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler
sc_input = StandardScaler() # StandardScaler ile sadece giris verisi olceklenecek
sc_output = StandardScaler() # StandardScaler ile sadece giris verisi olceklenecek
Y = Y.reshape(-1,1)
y_train = y_train.reshape(-1,1)
sc_input.fit(X)
sc_output.fit(Y)
X_train = sc_input.transform(X_train)
y_train = sc_output.transform(y_train)
X_test = sc_input.transform(X_test)
y_train = y_train.reshape(1,-1)
regressor = SVR(kernel='rbf')
regressor.fit(X_train, y_train[0]) # Dizi kılıfında eğitim yapıyoruz
out = regressor.predict(X_test)
out = sc_output.inverse_transform(out.reshape(-1,1))
def MAPE_Loss_Score(true,pred):
MAPE = mean_absolute_percentage_error(true, pred)*100
#print('#'*70,'Loss:', 100-MAPE)
return(MAPE)
# Custom Scoring MAPE calculation
from sklearn.metrics import make_scorer
SVRcustom_Scoring=make_scorer(MAPE_Loss_Score, greater_is_better=True)
cv = KFold(n_splits=15, random_state=1, shuffle=True)
regressor = SVR(kernel = 'rbf')
SVRMAPE_Loss_Values=cross_val_score(regressor, X_train , y_train, cv=cv, scoring=SVRcustom_Scoring)
print('\nLoss values for 15-kfold Cross Validation:\n',SVRMAPE_Loss_Values)
print('\nMAPE Final Average Accuracy of the model:', round(SVRMAPE_Loss_Values.mean(),2))
print('\nStandard Deviation:', round(SVRMAPE_Loss_Values.std(),2))
plt.boxplot(SVRMAPE_Loss_Values, showmeans=True)
plt.show()
ValueError: Found input variables with inconsistent numbers of samples: [143, 1]
Hello
I want to extract mape values using SVR algorithm. but i am getting this error
When I change the value of y_train = y_train.reshape(1,-1) to y_train = y_train.reshape(143,-1), the problem is fixed, but the mape values are high?
What should I do to calculate these values correctly? ........................................................................................................