# model.predict in Keras, Python error

I trained a model in Keras with input dimension 15 and output dimension 1. Then I tried to predict the output for a single input np.array, which I chose to be a toy example np.arange(15). However, the input is not accepted. Can someone tell me where the problem is? Here is the code for a simplified problem:

import numpy as np
from keras.models import Sequential
from keras.layers import Dense
X = np.arange(15)
Y = 0
model = Sequential()
model.fit(X, Y, epochs=10, verbose=1, batch_size=batch_size)
model.predict(X)


The following error occurs: ValueError: Error when checking input: expected dense_4_input to have shape (15,) but got array with shape (1,). But then again, the input clearly has the correct shape. What is going on here? Thanks for your help!

But then again, the input clearly has the correct shape.

>>> import numpy as np
>>> X = np.arange(15)
>>> X.shape
(15,)


In Keras, input_dim represents the number of input parameters, in your case that would be the number of columns of X or its second dimension (sometimes also referred to as number of features). It is clearly not 15. It is the first dimension that is 15. That means: X consists of 15 rows, also called samples (of one and the same feature).

So in that case, input_dim=1.

However, you will then run into the problem of having specified Y = 0. First, Keras will throw an error because it is an integer. You could do Y = [0], but then you will get

ValueError: Input arrays should have the same number of samples as target arrays. Found 15 input samples and 1 target samples.


So you have to turn this into an array-like object containing 15 samples, e.g. a list of length 15.

However, in case you meant to feed one single sample X to your model, that maps to one single output Y = [0], then you need to reshape X accordingly, for example via

X = np.arange(15).reshape(n_samples, n_features)


Can you figure out now what n_samples, n_features needs to be?