二进制值1和0的预测概率(数组中的每个值都是互斥的)

时间:2019-05-05 16:46:21

标签: keras deep-learning

我已经做出了模型预测,但是(y_pred)值显示为概率,而不是0和1。

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],
       ...

Keras中是否存在一种自动输出0和1的方法,还是应该手动使用0.5阈值将每个概率输出转换为0和1的概率?

用于生成预测的测试数据实际上被标记为值<0,0,1,0,1,0>(例如)。

每个数组值都是互斥/独立的。

1 个答案:

答案 0 :(得分:0)

你是这个意思吗?

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

结合使用Lambda层和阈值功能。