首页 > 解决方案 > 使用神经网络预测新观察的输出

问题描述

我已经使用 Python 的 Keras 包构建了一个神经网络。我的网络的目标是预测房价。这是我的训练数据集的样子。

    Price   Beds    SqFt    Built   Garage  FullBaths   HalfBaths   LotSqFt
    485000  3       2336    2004    2       2.0          1.0        2178.0
    430000  4       2106    2005    2       2.0          1.0        2178.0
    445000  3       1410    1999    1       2.0          0.0        3049.0

...

假设我有一些要分析的新房子。例如,我想预测一所房子的价格

如何将这些值输入到我的网络中以接收预测价格。另外,有没有办法让网络报告某种置信度指标?

作为参考,这是我的网络目前的样子。

from keras.models import Sequential
from keras.layers import Dense

N = 16

model = Sequential([
    Dense(N, activation='relu', input_shape=(7,)),
    Dense(1, activation='relu'),
])

model.compile(optimizer='sgd',
              loss='mse',
              metrics=['mean_squared_error'])

hist = model.fit(X_train, Y_train,
          batch_size=32, epochs=100,
          validation_data=(X_val, Y_val))

model.evaluate(X_test, Y_test)[1]

提前致谢!!

标签: pythontensorflowkerasneural-networkprediction

解决方案


为此,您将要使用model.predict(),如此处所述

model.predict()将一批输入作为参数x。在您的情况下,您只有 1 个输入,因此您可以将其写为:

x = [[4, 2500, 2001, 0, 3, 1, 3452]] # Assumes 0 garages
print(model.predict(x)[0]) # Print the first (only) result

推荐阅读