我一直致力于创建一个模型来基于包含 16 个特征的数据集来预测 0 或 1 的结果 - 一切似乎都运行良好(准确性评估、纪元完成等)。
如上所述,我的训练特征包括 16 个不同的变量,但是当我传入一个包含 16 个独立于训练数据集的唯一值的列表以尝试进行单独预测(0 或 1)时,我收到此错误:
ValueError: Layer sequential_11 expects 1 input(s), but it received 16 input tensors.
这是我的代码:
y = datas.Result
X = datas.drop(columns = ['Date', 'home_team', 'away_team', 'home_pitcher', 'away_pitcher', 'Result'])
X = X.values.astype('float32')
y = y.values.astype('float32')
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.2)
X_train, X_validation, y_train, y_validation = train_test_split(X, y, test_size = 0.2)
model=keras.Sequential([
keras.layers.Dense(32, input_shape = (16,)),
keras.layers.Dense(20,activation=tf.nn.relu),
keras.layers.Dense(2,activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['acc'])
history = model.fit(X_train,y_train,epochs=20, validation_data=(X_validation, y_validation))
#all variables within features list are single values, ex: .351, 11, .991, etc.
features = [t1_pqm,t2_pqm,t1_elo,t2_elo,t1_era,t2_era,t1_bb9,t2_bb9,t1_fip,t2_fip,t1_ba,t2_ba,t1_ops,t2_ops,t1_so,t2_so]
prediction = model.predict(features)
模型需要形状为
(None,16)
的输入,但特征具有形状 (16,)
(一维列表)。最简单的解决方案是使其成为具有正确形状的 numpy 数组 (1, 16)
:
features = np.array([[t1_pqm,t2_pqm,t1_elo,t2_elo,t1_era,t2_era,t1_bb9,t2_bb9,t1_fip,t2_fip,t1_ba,t2_ba,t1_ops,t2_ops,t1_so,t2_so]])