python之MSE、MAE、RMSE

来源:互联网 发布:海量数据为什么大跌 编辑:程序博客网 时间:2024/05/18 00:14
target = [1.5, 2.1, 3.3, -4.7, -2.3, 0.75]prediction = [0.5, 1.5, 2.1, -2.2, 0.1, -0.5]error = []for i in range(len(target)):    error.append(target[i] - prediction[i])print("Errors: ", error)print(error)squaredError = []absError = []for val in error:    squaredError.append(val * val)#target-prediction之差平方     absError.append(abs(val))#误差绝对值print("Square Error: ", squaredError)print("Absolute Value of Error: ", absError)print("MSE = ", sum(squaredError) / len(squaredError))#均方误差MSEfrom math import sqrtprint("RMSE = ", sqrt(sum(squaredError) / len(squaredError)))#均方根误差RMSEprint("MAE = ", sum(absError) / len(absError))#平均绝对误差MAEtargetDeviation = []targetMean = sum(target) / len(target)#target平均值for val in target:    targetDeviation.append((val - targetMean) * (val - targetMean))print("Target Variance = ", sum(targetDeviation) / len(targetDeviation))#方差print("Target Standard Deviation = ", sqrt(sum(targetDeviation) / len(targetDeviation)))#标准差