我正在尝试编写一个Python代码,计算实验列表和几个模拟列表的均方根误差。最简单的解决方案是为每个不同的模拟多次定义RMSE方程:
import numpy as np
import math
experimental = [1,2,3,4,5]
simulated = [1.6,2.5,2.9,3,4.1]
MSE = np.square(np.subtract(experimental,simulated)).mean()
RMSE = math.sqrt(MSE)
print("Root Mean Square Error:n")
print(RMSE)
#Second RMSE
simulated1 = [5,4,3,2,1]
MSE1 = np.square(np.subtract(experimental,simulated1)).mean()
RMSE1 = math.sqrt(MSE1)
print("Root Mean Square Error1:n")
print(RMSE1)
但这是低效的。我将模拟列表尝试到另一个列表中,然后使用for循环返回RMSE值。
import numpy as np
import math
experimental = [1,2,3,4,5]
simulated1 = [1.6,2.5,2.9,3,4.1]
simulated2 = [5,4,3,2,1]
MSE[i] = np.square(np.subtract(experimental,simulated[i])).mean()
RMSE[i] = math.sqrt(MSE[i])
sim_list = [simulated1, simulated2]
for i in sim_list:
print RMSE[i](experimental, simulated[i])
有办法让这个工作吗?
应该可以。我只是按照你的想法:遍历模拟列表,并计算每个列表的均方根误差。正如@Martian Coder所说,您可以使用mean_squared_error
函数来获得更清晰的实现。
import numpy as np
import math
experimental = [1,2,3,4,5]
simulated1 = [1.6,2.5,2.9,3,4.1]
simulated2 = [5,4,3,2,1]
def RMSE(experimental, simulated):
return math.sqrt(np.square(np.subtract(experimental, simulated)).mean())
sim_list = [simulated1, simulated2]
for sim in sim_list:
print(RMSE(experimental, sim))