使用Keras添加新特征进行预测时出错

我之前仅使用床位数量进行了预测(运行正常),现在,我想通过添加第二个输入(平方英尺)来改进房价预测。

我添加了如下代码:

import tensorflow as tfimport numpy as npfrom tensorflow import kerasmodel = keras.Sequential([keras.layers.Dense(units=1, input_shape=[2])])xs = np.stack([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [100, 150, 200, 250, 300, 350]], axis=1)ys = np.array([100000, 150000, 200000, 250000, 300000, 350000], dtype=float)model.fit(xs, ys, epochs=100)print(model.predict([[7.0], [400.0]]))  # [7.0] number of beds / [400] square feet 

但我收到了以下错误:

ValueError: Input 0 of layer sequential_57 is incompatible with the layer: expected axis -1 of input shape to have value 2 but received input with shape [None, 1]

请支持我修复这个问题并使其正常工作。

此致,


回答:

我对你的代码做了以下更改:

  1. 在训练模型之前,你需要编译模型(请看我下面的代码中的model.compile('adam', 'mae')
  2. 你想要预测的输入数组维度错误。它的维度是(2,1),我将其改为(1,2)

如果我更改了这两点,代码对我来说是可以工作的。

model = keras.Sequential([keras.layers.Dense(units=1, input_shape=[2])])xs = np.stack([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [100, 150, 200, 250, 300, 350]], axis=1)ys = np.array([100000, 150000, 200000, 250000, 300000, 350000], dtype = float)model.compile('adam', 'mae')model.fit(xs, ys, epochs=100)print(model.predict(np.array([[7.0, 400.0]]))) # [7.0] number of beds / [400] square feet #

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注