what's wrong with this code which does not enter the for loop? for i in range(NumTest):…
- User
what's wrong with this code which does not enter the for loop? for i in range(NumTest): print(i) allindex=list(range(len(X))) indices=allindex r1=random.randint(0,len(X)-1) r2=random.randint(0,len(X)-1) print(r1) print(r2) if r1==r2: if r1<len(X)-1: r2=r1+1 else: r2=r1-1
r=[r1,r2] r1=np.max(r) r2=np.min(r) del indices[r1] del indices[r2] XTR=X[indices] XTS=X[r] YTR=Y.iloc[indices] YTS=Y.iloc[r] labelTr = YTR labelTs = YTS dtrain = xgb.DMatrix(XTR,label=labelTr) dtest = xgb.DMatrix(XTS,label=labelTs) num_round = 10 bst = xgb.train(param, dtrain, num_round) ypred = bst.predict(dtest) ytest=YTS.to_numpy() ytest=np.transpose(ytest) ypred=np.reshape(ypred,ytest.shape) loss=np.abs(ytest-ypred) mape=100*METRICS.mean_absolute_percentage_error(ytest, ypred) mae=np.mean(loss) rmse=np.sqrt(np.mean(loss**2)) MAPE.append(mape) MAE.append(mae) RMSE.append(RMSE)
MEAN_MAE=np.mean(MAE) MEAN_RMSE=np.mean(RMSE) MEAN_MAPE=np.mean(MAPE)
- Assistant
It seems like there is an issue with the line
RMSE.append(RMSE)in the code. Instead of appending the RMSE values to the RMSE list, it is trying to append the list itself which will cause an error.To fix this issue, you should change
RMSE.append(RMSE)toRMSE.append(rmse)in the code. By doing this, the RMSE values calculated in each iteration will be correctly appended to the RMSE list.Here is the corrected code snippet:
for i in range(NumTest): # your existing code here RMSE.append(rmse) MEAN_MAE=np.mean(MAE) MEAN_RMSE=np.mean(RMSE) MEAN_MAPE=np.mean(MAPE)With this change, the code should now work as intended and calculate the mean MAE, RMSE, and MAPE values correctly.