1
import pandas as pd
import numpy as np

df_train=pd.read_csv('train_titanic.csv')
df_test=pd.read_csv('test_titanic.csv')

df_train.drop(['PassengerId','Name','Ticket','Cabin'],inplace=True,axis=1)
df_test.drop(['PassengerId','Name','Ticket','Cabin'],inplace=True,axis=1)

df_train=pd.concat([df_train,pd.get_dummies(df_train.Sex),pd.get_dummies(df_train.Pclass),pd.get_dummies(df_train.Embarked)],axis=1)
df_test=pd.concat([df_test,pd.get_dummies(df_test.Sex),pd.get_dummies(df_test.Pclass),pd.get_dummies(df_test.Embarked)],axis=1)

df_train.drop(['Sex','Pclass','Embarked','male',3,'S'],axis=1,inplace=True)
df_test.drop(['Sex','Pclass','Embarked','male',3,'S'],axis=1,inplace=True)

df_train.fillna(df_train.mean(),inplace=True)
df_test.fillna(df_test.mean(),inplace=True)

y_train=df_train.iloc[:,0]
X_train=df_train.iloc[:,1:]

def make_model():
    from keras.models import Sequential
    from keras.layers import Dense

    model=Sequential()
    model.add(Dense(10,activation='relu',input_shape=(9,)))
    model.add((Dense(5,activation='relu')))
    model.add((Dense(1,activation='sigmoid')))
    model.compile('adam',loss='binary_crossentropy',metrics=['accuracy'])
    return model

model=make_model()
model.fit(X_train,y_train,batch_size=32,epochs=10)

I'm getting the following error:

~\Anaconda3\lib\site-packages\keras\models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
    891                               class_weight=class_weight,
    892                               sample_weight=sample_weight,
--> 893                               initial_epoch=initial_epoch)
    894 
    895     def evaluate(self, x, y, batch_size=32, verbose=1,

~\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1553             class_weight=class_weight,
   1554             check_batch_axis=False,
-> 1555             batch_size=batch_size)
   1556         # Prepare validation data.
   1557         do_validation = False

~\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size)
   1407                                     self._feed_input_shapes,
   1408                                     check_batch_axis=False,
-> 1409                                     exception_prefix='input')
   1410         y = _standardize_input_data(y, self._feed_output_names,
   1411                                     output_shapes,

~\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    124     # Make arrays at least 2D.
    125     for i in range(len(names)):
--> 126         array = arrays[i]
    127         if len(array.shape) == 1:
    128             array = np.expand_dims(array, 1)

UnboundLocalError: local variable 'arrays' referenced before assignment

None of the previous questions were of any help. Is it a bug? How can I get around it? Which array is it referring to in this error?

2 Answers 2

1

I had to change this

model.fit(X_train,y_train,batch_size=32,epochs=10)

to this

model.fit(X_train.values,y_train.values,batch_size=32,epochs=10)
Sign up to request clarification or add additional context in comments.

2 Comments

You are required to pass an numpy array. In first case it's a data frame while in case 2 it's a numpy array.
Ok.but in SkLearn we can send either of them ryt?
0

I had the same error on a docker container with an Ubuntu:16.04 image. The error raises when I upgrade pip

RUN pip install --upgrade pip

when i comment this line, tensorflow and keras runs nice :D

This is my Dockerfile

FROM ubuntu:16.04
RUN \
    apt-get update && \
    apt-get install -y python python-dev python-pip python-virtualenv && \
    rm -rf /var/lib/apt/lists/*
RUN mkdir /src
WORKDIR /src
# RUN pip install --upgrade pip
RUN pip install Werkzeug Flask numpy Keras gevent pillow h5py tensorflow
ADD requirements.txt ./
RUN pip install -r requirements.txt

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.