如何在Keras Regressor中解释MSE [英] How to interpret MSE in Keras Regressor
问题描述
我是Keras/TF/深度学习的新手,我正在尝试建立一个模型来预测房价.
I am new to Keras/TF/Deep Learning and I am trying to build a model to predict house prices.
我有一些功能X(浴室数量等)和目标Y(范围在$ 300,000到$ 800,000之间)
I have some features X (no. of bathrooms , etc.) and target Y (ranging around $300,000 to $800,000)
在将Y拟合到模型之前,我已经使用sklearn的Standard Scaler对其进行了标准化.
这是我的Keras模型:
Here is my Keras model:
def build_model():
model = Sequential()
model.add(Dense(36, input_dim=36, activation='relu'))
model.add(Dense(18, input_dim=36, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='mse', optimizer='sgd', metrics=['mae','mse'])
return model
我难以解释结果-0.617454319755的MSE是什么意思?
I am having trouble trying to interpret the results -- what does a MSE of 0.617454319755 mean?
我是否必须对这个数字进行逆变换,然后对结果求平方,得到741.55美元的错误率?
Do I have to inverse transform this number, and square root the results, getting an error rate of 741.55 in dollars?
math.sqrt(sc.inverse_transform([mse]))
我为刚开始听起来很傻而道歉!
I apologise for sounding silly as I am starting out!
推荐答案
我为刚开始听起来很傻而道歉!
I apologise for sounding silly as I am starting out!
不要;这是一个非常重要的细微问题,在教程和介绍性介绍中通常(并且很遗憾)被忽略.
Do not; this is a subtle issue of great importance, which is usually (and regrettably) omitted in tutorials and introductory expositions.
不幸的是,它不像取逆变换的MSE的平方根那么简单,但是也没有那么复杂.本质上,您要做的是:
Unfortunately, it is not as simple as taking the square root of the inverse-transformed MSE, but it is not that complicated either; essentially what you have to do is:
- 将您的预测转换回原始数据的初始比例
- 获取这些逆变换的预测与原始数据之间的MSE
- 取结果的平方根
为了获得模型的性能指标,该模型在问题的 business 情境中将是有意义的(例如此处的美元).
in order to get a performance indicator of your model that will be meaningful in the business context of your problem (e.g. US dollars here).
让我们看一个包含玩具数据的简单示例,省略模型本身(这里无关紧要,实际上可以是任何模型-不仅是Keras模型):
Let's see a quick example with toy data, omitting the model itself (which is irrelevant here, and in fact can be any model - not only a Keras one):
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error
import numpy as np
# toy data
X = np.array([[1,2], [3,4], [5,6], [7,8], [9,10]])
Y = np.array([3, 4, 5, 6, 7])
# feature dcaling
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X)
# outcome scaling:
sc_Y = StandardScaler()
Y_train = sc_Y.fit_transform(Y.reshape(-1, 1))
Y_train
# array([[-1.41421356],
# [-0.70710678],
# [ 0. ],
# [ 0.70710678],
# [ 1.41421356]])
现在,假设我们使用缩放集X_train
和Y_train
拟合了Keras模型(此处未显示),并获得了对训练集的预测:
Now, let's say that we fit our Keras model (not shown here) using the scaled sets X_train
and Y_train
, and get predictions on the training set:
prediction = model.predict(X_train) # scaled inputs here
print(prediction)
# [-1.4687586 -0.6596055 0.14954728 0.95870024 1.001172 ]
Keras报告的MSE实际上是按比例缩放的MSE,即:
The MSE reported by Keras is actually the scaled MSE, i.e.:
MSE_scaled = mean_squared_error(Y_train, prediction)
MSE_scaled
# 0.052299712818541934
我上面描述的3个步骤很简单:
while the 3 steps I have described above are simply:
MSE = mean_squared_error(Y, sc_Y.inverse_transform(prediction)) # first 2 steps, combined
MSE
# 0.10459946572909758
np.sqrt(MSE) # 3rd step
# 0.323418406602187
因此,在本例中,如果我们的初始Y为美元,则相同单位(美元)的实际误差为0.32(美元).
So, in our case, if our initial Y were US dollars, the actual error in the same units (dollars) would be 0.32 (dollars).
请注意,对规模化的MSE进行逆变换的天真方法将如何产生非常不同(且不正确)的结果:
Notice how the naive approach of inverse-transforming the scaled MSE would give a very different (and incorrect) result:
np.sqrt(sc_Y.inverse_transform([MSE_scaled]))
# array([2.25254588])
这篇关于如何在Keras Regressor中解释MSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!