Keras
ValueError
NumPy
Tensor
Python Error

Keras ValueError Failed to convert a NumPy array to a Tensor Unsupported object type float

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

This Keras error usually means your input looks numeric to a human but not to TensorFlow. The most common cause is an array or DataFrame column that has fallen back to dtype=object, often because of mixed values, missing data, or uneven row shapes.

What the message is really telling you

The wording "unsupported object type float" is confusing because normal floating-point tensors are valid. The key word is object. TensorFlow is not receiving a clean numeric array. It is receiving Python objects, and some of those objects happen to be floats.

That often happens in three situations:

  • a NumPy array contains mixed types such as numbers plus strings
  • a pandas column is object because one row is malformed
  • nested rows have different lengths, so NumPy builds an object array instead of a dense matrix

Before training, always inspect the shape and dtype of what you pass to model.fit.

Converting data to a real numeric tensor input

A reliable fix is to coerce the data to a single numeric dtype before it reaches Keras.

python
1import numpy as np
2import pandas as pd
3import tensorflow as tf
4
5df = pd.DataFrame(
6    {
7        "x1": [1.0, 2.0, 3.0, 4.0],
8        "x2": [0.5, 1.5, 2.5, 3.5],
9        "y": [0, 0, 1, 1],
10    }
11)
12
13X = df[["x1", "x2"]].to_numpy(dtype=np.float32)
14y = df["y"].to_numpy(dtype=np.float32)
15
16model = tf.keras.Sequential(
17    [
18        tf.keras.layers.Input(shape=(2,)),
19        tf.keras.layers.Dense(8, activation="relu"),
20        tf.keras.layers.Dense(1, activation="sigmoid"),
21    ]
22)
23
24model.compile(optimizer="adam", loss="binary_crossentropy")
25model.fit(X, y, epochs=5, verbose=0)
26
27prediction = model.predict(np.array([[5.0, 4.0]], dtype=np.float32), verbose=0)
28print(prediction)

This works because X and y are dense arrays with a consistent numeric type. Keras can convert them to tensors without ambiguity.

How bad input sneaks in

Here is a small example of data that looks close to valid but fails because the array becomes an object array.

python
1import numpy as np
2
3bad = np.array([[1.0, 2.0], [3.0, "4.0"]], dtype=object)
4
5print(bad.dtype)
6print(bad)

The presence of a string means NumPy no longer treats the matrix as a regular numeric block. The fix is not to cast blindly and hope for the best. First clean the source data, then convert it with a deliberate dtype such as np.float32.

For pandas input, df.dtypes is often the fastest diagnostic. If a feature column shows object, inspect it for missing strings, stray whitespace, or inconsistent preprocessing.

A debugging checklist

When this error appears, inspect these four things in order:

  1. Print X.dtype and y.dtype.
  2. Print X.shape and verify each row has the same width.
  3. Check pandas column dtypes before converting to NumPy.
  4. Coerce numeric columns with explicit conversion and handle missing values before model training.

If you have text features, do not feed raw strings into a dense numeric model. Encode them first with tokenization, one-hot encoding, embeddings, or another preprocessing step that produces numeric tensors.

Common Pitfalls

  • Assuming that "float" in the error message means TensorFlow rejects floating-point numbers. The actual issue is usually dtype=object.
  • Converting a malformed array with astype without checking whether some values become nan.
  • Building NumPy arrays from rows of different lengths and accidentally creating ragged object arrays.
  • Passing pandas columns with mixed strings and numbers directly to Keras.
  • Debugging the model architecture first when the problem is really in data preparation.

Summary

  • This error usually means Keras received an object array instead of a dense numeric array.
  • Check dtype and shape before calling model.fit.
  • Convert inputs explicitly with a numeric dtype such as np.float32.
  • Clean mixed values, missing values, and uneven rows before creating tensors.
  • When the input pipeline is consistent, the Keras side of the error usually disappears.

Course illustration
Course illustration

All Rights Reserved.