title: Keras binary classification probabilities to labels
body: <p>Keras predicted output for binary classification is probabilities. Not classes, i.e., <code>1</code> or <code>0</code>.
  for example the following code generates probabilities.</p>
  
  <pre><code>import numpy as np
  from keras.models import Sequential
  from keras.layers import Dense, Dropout
  
  # Generate dummy data
  x_train = np.random.random((100, 20))
  y_train = np.random.randint(2, size=(100, 1))
  x_test = np.random.random((10, 20))
  y_test = np.random.randint(2, size=(10, 1))
  
  model = Sequential()
  model.add(Dense(64, input_dim=20, activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(64, activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(1, activation='sigmoid'))
  
  model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
  
  model.fit(x_train, y_train,  epochs=20,    batch_size=128)
  y_predicted = model.predict(x_test)
  print(y_predicted)
  </code></pre>
  
  <p>and the output is:</p>
  
  <pre><code>Epoch 1/20
  100/100 [==============================] - 1s 5ms/step - loss: 0.8134 - acc: 0.4300
  Epoch 2/20
  100/100 [==============================] - 0s 17us/step - loss: 0.7429 - acc: 0.4600
  Epoch 3/20
  100/100 [==============================] - 0s 20us/step - loss: 0.7511 - acc: 0.4300
  Epoch 4/20
  100/100 [==============================] - 0s 18us/step - loss: 0.7408 - acc: 0.5000
  Epoch 5/20
  100/100 [==============================] - 0s 21us/step - loss: 0.6922 - acc: 0.5700
  Epoch 6/20
  100/100 [==============================] - 0s 31us/step - loss: 0.6874 - acc: 0.5600
  Epoch 7/20
  100/100 [==============================] - 0s 29us/step - loss: 0.7005 - acc: 0.5600
  Epoch 8/20
  100/100 [==============================] - 0s 23us/step - loss: 0.6960 - acc: 0.5200
  Epoch 9/20
  100/100 [==============================] - 0s 24us/step - loss: 0.6988 - acc: 0.5200
  Epoch 10/20
  100/100 [==============================] - 0s 26us/step - loss: 0.7276 - acc: 0.4000
  Epoch 11/20
  100/100 [==============================] - 0s 20us/step - loss: 0.6967 - acc: 0.5000
  Epoch 12/20
  100/100 [==============================] - 0s 30us/step - loss: 0.7085 - acc: 0.5000
  Epoch 13/20
  100/100 [==============================] - 0s 24us/step - loss: 0.6993 - acc: 0.5500
  Epoch 14/20
  100/100 [==============================] - 0s 26us/step - loss: 0.7278 - acc: 0.4600
  Epoch 15/20
  100/100 [==============================] - 0s 27us/step - loss: 0.6665 - acc: 0.5500
  Epoch 16/20
  100/100 [==============================] - 0s 24us/step - loss: 0.6784 - acc: 0.5500
  Epoch 17/20
  100/100 [==============================] - 0s 24us/step - loss: 0.7259 - acc: 0.4800
  Epoch 18/20
  100/100 [==============================] - 0s 26us/step - loss: 0.7093 - acc: 0.5500
  Epoch 19/20
  100/100 [==============================] - 0s 28us/step - loss: 0.6911 - acc: 0.5700
  Epoch 20/20
  100/100 [==============================] - 0s 34us/step - loss: 0.6771 - acc: 0.5500
  [[0.4875336 ]
   [0.47847825]
   [0.4808622 ]
   [0.5032022 ]
   [0.4556646 ]
   [0.48644704]
   [0.4600153 ]
   [0.47782585]
   [0.49664593]
   [0.5001673 ]]
  </code></pre>
  
  <p>Now, how can I get the classes from that probabilities? I tried manually setting a threshold like this:</p>
  
  <pre><code>print([1 if x &gt;0.4 else 0 for x in y_predicted])
  </code></pre>
  
  <p>Is there any other method to do that from Keras API? I could not find any.</p>