# [Solved] ValueError: Data cardinality is ambiguous

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ValueError: Data cardinality is ambiguous in Python. So Here I am Explain to you all the possible solutions here.

## How ValueError: Data cardinality is ambiguous Error Occurs?

e. Today I get the following error ValueError: Data cardinality is ambiguous in Python.

## How To Solve tValueError: Data cardinality is ambiguous Error ?

1. How To Solve tValueError: Data cardinality is ambiguous Error ?

To Solve tValueError: Data cardinality is ambiguous Error This Error can be fixed by uncommenting the Line, `y = y.reshape(1,-1)`, which makes the `First Dimension` (`Batch_Size`) equal (`1`) for both `X` and `y`.

2. ValueError: Data cardinality is ambiguous

To Solve tValueError: Data cardinality is ambiguous Error This Error can be fixed by uncommenting the Line, `y = y.reshape(1,-1)`, which makes the `First Dimension` (`Batch_Size`) equal (`1`) for both `X` and `y`.

## Solution 1

As the `Error` suggests, the `First Dimension` of `X` and `y` is different. `First Dimension` indicates the `Batch Size` and it should be same.

Please ensure that `Y` also has the `shape``(1, something)`.

I could reproduce your error with the Code shown below:

```from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
import tensorflow as tf
import numpy as np

# define sequences
sequences = [
[1, 2, 3, 4],
[1, 2, 3],

]

X = np.expand_dims(padded, axis = 0)
print(X.shape) # (1, 3, 4)

y = np.array([1,0,1])
#y = y.reshape(1,-1)
print(y.shape) # (3,)

model = Sequential()

model.compile (
loss='mean_squared_error',

model.fit(x = X, y = y)
```

If we observe the `Print` Statements,

```Shape of X is  (1, 3, 4)
Shape of y is (3,)
```

This Error can be fixed by uncommenting the Line, `y = y.reshape(1,-1)`, which makes the `First Dimension` (`Batch_Size`) equal (`1`) for both `X` and `y`.

Now, the working code is shown below, along with the Output:

```from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
import tensorflow as tf
import numpy as np

# define sequences
sequences = [
[1, 2, 3, 4],
[1, 2, 3],

]

X = np.expand_dims(padded, axis = 0)
print('Shape of X is ', X.shape) # (1, 3, 4)

y = np.array([1,0,1])
y = y.reshape(1,-1)
print('Shape of y is', y.shape) # (1, 3)

model = Sequential()

model.compile (
loss='mean_squared_error',

model.fit(x = X, y = y)
```

The Output of above code is :

```Shape of X is  (1, 3, 4)
Shape of y is (1, 3)
1/1 [==============================] - 0s 1ms/step - loss: 0.2588
<tensorflow.python.keras.callbacks.History at 0x7f5b0d78f4a8>
```

Hope this helps. Happy Learning!

## Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.