I know this problem has been answered previously in the link below,but it does not apply to my situation.(Tensorflow - ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float))
Both my predictor (X) and target variables (y) are <class 'numpy.ndarray'>
and their shapes are
X: (8981, 25)
y: (8981, 1)
Yet, I am still getting the error message. ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float).
Please refer to the following code:
import tensorflow as tf
ndim = X.shape[1]
model = tf.keras.models.Sequential()
# model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(36, activation = tf.nn.relu, input_dim=ndim))
model.add(tf.keras.layers.Dense(36, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(2, activation = tf.nn.softmax))
model.compile(optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
model.fit(X.values, y, epochs = 5)
y_pred = model.predict([X_2019])
Any help will be really appreciated! Thanks!!!
Try inserting dtype=np.float
when creating the np
array:
np.array(*your list*, dtype=np.float)
Some of my columns were categorical. Try printing X.dtypes
and checking if any of the entries are as type 'object'. Another helpful command: X[X.dtypes=='object']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With