改写代码:def RMSE_MAPE(ydata,y_hatdata,indexlist): y = ydata.loc[indexlist].values.T y_hat = y_hatdata.loc[indexlist].values.T x1 = np.power(y - y_hat, 2).mean() x2 = np.abs((y - y_hat) / y).mean() RMSE = np.sqrt(x1) MAPE = x2*100 #输出结果单位为% print(f'RMSE:{RMSE} MAPE:{MAPE}') return RMSE, MAPE
时间: 2023-06-09 22:07:17 浏览: 187
修改代码如下:
def RMSE_MAPE(ydata, y_hatdata, indexlist):
y = ydata.loc[indexlist].values.T
y_hat = y_hatdata.loc[indexlist].values.T
diff = y - y_hat
x1 = np.mean(diff**2)
x2 = np.mean(np.abs(diff/y))
RMSE = np.sqrt(x1)
MAPE = x2 * 100
相关问题
用pandas函数代替下列语句:def RMSE_MAPE(ydata,y_hatdata,indexlist): y=[] y_hat=[] for i in indexlist: y.append(ydata.iat[i,0]) y.append(ydata.iat[i,1]) y.append(ydata.iat[i,2]) y_hat.append(y_hatdata.iat[i,0]) y_hat.append(y_hatdata.iat[i,1]) y_hat.append(y_hatdata.iat[i,2]) x1=0 for i in range(len(indexlist)): x1=x1+np.square(y[i] - y_hat[i]) x2=0 for i in range(len(indexlist)): x2=x2+np.abs((y[i] - y_hat[i]) / y[i]) RMSE = np.sqrt(x1/len(indexlist)) MAPE = x2*100/len(indexlist) print('RMSE:'+str(RMSE)+' MAPE:'+str(MAPE)) return
import pandas as pd
def RMSE_MAPE(ydata, y_hatdata, indexlist):
y = pd.concat([ydata.loc[indexlist, 'col1'], ydata.loc[indexlist, 'col2'], ydata.loc[indexlist, 'col3']], ignore_index=True)
y_hat = pd.concat([y_hatdata.loc[indexlist, 'col1'], y_hatdata.loc[indexlist, 'col2'], y_hatdata.loc[indexlist, 'col3']], ignore_index=True)
x1 = np.square(y - y_hat).sum()
x2 = np.abs((y - y_hat) / y).sum()
RMSE = np.sqrt(x1 / len(y))
MAPE = x2 * 100 / len(y)
print('RMSE:', RMSE, 'MAPE:', MAPE)
return
用pandas函数替代该函数:def RMSE_MAPE(ydata,y_hatdata,indexlist): y=[] y_hat=[] for i in indexlist: y.append(ydata.iat[i,0]) y.append(ydata.iat[i,1]) y.append(ydata.iat[i,2]) y_hat.append(y_hatdata.iat[i,0]) y_hat.append(y_hatdata.iat[i,1]) y_hat.append(y_hatdata.iat[i,2]) x1=0 for i in range(len(indexlist)): x1=x1+np.square(y[i] - y_hat[i]) x2=0 for i in range(len(indexlist)): x2=x2+np.abs((y[i] - y_hat[i]) / y[i]) RMSE = np.sqrt(x1/len(indexlist)) MAPE = x2*100/len(indexlist) print('RMSE:'+str(RMSE)+' MAPE:'+str(MAPE)) return
可以使用以下的pandas函数替代该函数:
def RMSE_MAPE(ydata, y_hatdata, indexlist):
y = ydata.iloc[indexlist, :].values.flatten()
y_hat = y_hatdata.iloc[indexlist, :].values.flatten()
x1 = np.sum(np.square(y - y_hat))
x2 = np.sum(np.abs((y - y_hat) / y)) * 100
RMSE = np.sqrt(x1/len(y))
MAPE = x2/len(y)
print('RMSE:', RMSE, 'MAPE:', MAPE)
return RMSE, MAPE
这个函数首先使用iloc函数来筛选出在indexlist中的行,并将其展开为一维数组y和y_hat。然后求出x1和x2,并根据它们的值计算RMSE和MAPE。最后返回RMSE和MAPE。
阅读全文