且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用keras预报_proba输出2列概率?

更新时间:2023-12-02 14:52:40

首先,您需要通过

y_train转换为单编码

First, you need to convert y_train to one-hot encoding by

from sklearn.preprocessing import LabelEncoder
from keras.utils import np_utils

encoder = LabelEncoder()
encoder.fit(y_train)
encoded_y = encoder.transform(y_train)
y_train = np_utils.to_categorical(encoded_y)

运行此代码,y_train将变为

array([[1., 0.],
       [1., 0.],
       [1., 0.],
       [1., 0.],
       [1., 0.],
       [1., 0.],
       [1., 0.],
       [0., 1.],
       [0., 1.],
       [0., 1.],
       [0., 1.]], dtype=float32)

第二,您需要将输出层更改为

Secondly, you need to change the output layer to

model.add(Dense(2, activation='softmax'))

通过这两个修改,您将获得所需的输出.

with these two modifications, you will get the desired output.