title: How do I implement multilabel classification neural network with keras
body: <p>I am attempting to implement a neural network using Keras with a problem that involves multilabel classification. I understand that one way to tackle the problem is to transform it to several binary classification problems. I have implemented one of these, but am not sure how to proceed with the others, mostly how do I go about combining them? My data set has 5 input variables and 5 labels. Generally a single sample of data would have 1-2 labels. It is rare to have more than two labels.</p>
  <p>Here is my code (thanks to machinelearningmastery.com):</p>
  <pre><code>import numpy
  import pandas
  from keras.models import Sequential
  from keras.layers import Dense
  from keras.wrappers.scikit_learn import KerasClassifier
  from sklearn.model_selection import cross_val_score
  from sklearn.preprocessing import LabelEncoder
  from sklearn.model_selection import StratifiedKFold
  from sklearn.preprocessing import StandardScaler
  from sklearn.pipeline import Pipeline
  
  
  # fix random seed for reproducibility
  seed = 7
  numpy.random.seed(seed)
  
  
  # load dataset
  dataframe = pandas.read_csv(&quot;Realdata.csv&quot;, header=None)
  dataset = dataframe.values
  # split into input (X) and output (Y) variables
  X = dataset[:,0:5].astype(float)
  Y = dataset[:,5]
  
  
  # encode class values as integers
  encoder = LabelEncoder()
  encoder.fit(Y)
  encoded_Y = encoder.transform(Y)
  
  
  # baseline model
  def create_baseline():
      # create model
      model = Sequential()
      model.add(Dense(5, input_dim=5, kernel_initializer='normal', activation='relu'))
      model.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))
      # Compile model
      model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
      scores = model.evaluate(X, encoded_Y)
      print(&quot;\n%s: %.2f%%&quot; % (model.metrics_names[1], scores[1]*100))
      #Make predictions....change the model.predict to whatever you want instead of X
      predictions = model.predict(X)
      # round predictions
      rounded = [round(x[0]) for x in predictions]
      print(rounded)
      return model
      
  
  # evaluate model with standardized dataset
  estimator = KerasClassifier(build_fn=create_baseline, epochs=100, batch_size=5, verbose=0)
  kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed)
  results = cross_val_score(estimator, X, encoded_Y, cv=kfold)
  print(&quot;Results: %.2f%% (%.2f%%)&quot; % (results.mean()*100, results.std()*100))
  </code></pre>