使用神经网络预测新观察的输出
Using a Neural Network to Predict Output From a New Observation
我使用 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
...
假设我有一些新房子要分析。例如,我想用
预测房子的价格
- 4 张床
- 2500 平方英尺
- 2001 年建成
- 3 个全套卫浴
- 1 个半浴室
- 3452 平方英尺
如何将这些值输入我的网络以接收预测价格。另外,有没有办法让网络报告某种信心指标?
作为参考,这是我的网络目前的样子。
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]
提前致谢!!
为此,您需要使用 model.predict()
,如文档所述 here。
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
我使用 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
...
假设我有一些新房子要分析。例如,我想用
预测房子的价格- 4 张床
- 2500 平方英尺
- 2001 年建成
- 3 个全套卫浴
- 1 个半浴室
- 3452 平方英尺
如何将这些值输入我的网络以接收预测价格。另外,有没有办法让网络报告某种信心指标?
作为参考,这是我的网络目前的样子。
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]
提前致谢!!
为此,您需要使用 model.predict()
,如文档所述 here。
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