如何在 Keras Regressor 中解释 MSE [英] How to interpret MSE in Keras Regressor
问题描述
我正在尝试建立一个模型来预测房价.
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 对其进行了标准化.
I have used sklearn's Standard Scaler to standardize Y before fitting it to the model.
这是我的 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
- 求结果的平方根
为了获得在您的问题的业务环境中有意义的模型性能指标(例如此处的美元).
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 regression 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 scaling
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屋!