11

I am trying to fit my model (neural networks) using keras but I got ValueError error.

import keras

from keras.models import Sequential

from keras.layers import Dense


classificador_rede_neural = Sequential()


# # Camadas Ocultas e de Saída

# camadas ocultas = (entradas + saídas)/2 #estimando o numero de neurônios em camada oculta
# 
# temos:len(train.columns) - 1   atributos previsores
# 
# 1 classe


#len(train.columns)


camadas_ocultas = round(len(train.columns)/2)



print(camadas_ocultas)


classificador_rede_neural.add(Dense(units=camadas_ocultas, activation='relu',input_dim =len(train.columns) ))#primeira camada


classificador_rede_neural.add(Dense(units=camadas_ocultas, activation='relu' ))#segunda camada


classificador_rede_neural.add(Dense(units=1, activation='sigmoid' ))#camada de saída. a saída é binária, logo units=1

classificador_rede_neural.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])

classificador_rede_neural.fit(X_train2,y_train2,batch_size=10,epochs =100)

I am getting the error:

 ValueError: Please provide as model inputs either a single array or a list of arrays. You passed: x=              sload        dload  spkts  dpkts  swin  dwin  smean  dmean  \
    0      1.803636e+08     0.000000      2      0     0     0    248      0   
    1      8.810000e+08     0.000000      2      0     0     0    881      0   
    2      8.544000e+08     0.000000      2      0     0     0    534      0   
    3      6.000000e+08     0.000000      2      0     0     0    450      0   
    4      8.504000e+08     0.000000      2      0     0     0   1063      0   
    5      1.045333e+09     0.000000      2      0     0     0    392      0   
    6      1.306667e+09     0.000000      2      0     0     0    980      0   
    7      1.977143e+08     0.000000      2      0     0     0    692      0   

[82332 rows x 22 columns]

How could I fit the model? What is wrong with my data?

The complete code > https://pastebin.com/jE7erEJs

5

1 Answer 1

28

I think the problem is that you passed to your model an entire pandas dataset together with the columns headers and an index column. In order to train your model on your data, convert it to a numpy array first with X_train2.values and y_train2.values since a Keras model accepts as input a numpy array and not a pandas dataset

Similar question
Pandas DataFrame and Keras

Documentation on Keras' Sequential model
https://keras.io/models/sequential/

Edit to answer the comments
Don't convert each column separately, this is pointless. Assuming that you have a general dataset df with a column called labels what you have to do is

labels = df.pop("labels")
model.fit(x=df.values, y=labels.values)
5
  • How could I convert it? Commented Jun 16, 2018 at 22:51
  • Look at the answer. Either use X_train2.values or X_train2.as_matrix()
    – Colonder
    Commented Jun 16, 2018 at 22:52
  • X_train2,y_train2 =train.drop('label',axis=1),train['label'] X_test2,y_test2 =test,classe1_test Commented Jun 16, 2018 at 23:06
  • I am trying to use your answer but I am stuck! Could you look at the complete code > pastebin.com/jE7erEJs Commented Jun 16, 2018 at 23:20
  • Let us continue this discussion in chat.
    – Colonder
    Commented Jun 16, 2018 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.