0

I have produced a model prediction, but the (y_pred) values are shown as a probability rather than 0's and 1's.

from sklearn.metrics import confusion_matrix, precision_score
from sklearn.model_selection import train_test_split
from keras.layers import Dense,Dropout
from keras.models import Sequential
from keras.regularizers import l2
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

data = pd.read_csv('train.csv', header=None)
X = data.iloc[:,0:294]
y = data.iloc[:,294:300]

X_test = pd.read_csv('test.csv', header=None)

# define a sequential Model
model = Sequential()

# Hidden Layer-1
model.add(Dense(100, activation='relu',input_dim=294, kernel_regularizer=l2(0.01)))
model.add(Dropout(0.3, noise_shape=None, seed=None))

# Hidden Layer-2
model.add(Dense(100, activation = 'relu', kernel_regularizer=l2(0.01)))
model.add(Dropout(0.3, noise_shape=None, seed=None))

# Output layer
model.add(Dense(6, activation='sigmoid'))

# Compile model
model.compile(optimizer = 'adam', loss='binary_crossentropy', metrics=['accuracy'])

model_output = model.fit(X, y, epochs=20, batch_size=20, verbose=1)

y_pred = model.predict(X_test)

#########

y_pred1
array([[0.1668182 , 0.00139472, 0.0607101 , 0.83703804, 0.20101124,
        0.01134452],
       [0.5119093 , 0.00456575, 0.0413985 , 0.18643114, 0.24617025,
        0.14039314],
       [0.2648082 , 0.00091198, 0.03806886, 0.7853936 , 0.19942024,
        0.01565605],
       ...

Is there a method in Keras to output 0's and 1's automatically or should I manually use a 0.5 threshold to convert the probability outputs for each to 0's and 1's ?

The test data that was used to produce the prediction is actually labelled as values <0,0,1,0,1,0> (as an example).

Each of the array values are mutually exclusive/independent.

1 Answers1

0

You mean this?

Keras set output of intermediate layer to 0 or 1 based on threshold

Using Lambda layer combined with a threshold function.

Natthaphon Hongcharoen
  • 2,244
  • 1
  • 9
  • 23
  • Hi, is there a working example of this ?model.add(Lambda(func,output_shape=yourOtputShapeIfUsingTheano)) For example, should I replace my final output layer with this ? – Dinesh Muniandy May 05 '19 at 18:02
  • I also keep getting the error: NameError: name 'Lambda' is not defined I have already (imported Keras as K) to use the function. – Dinesh Muniandy May 05 '19 at 22:27