如何让Keras的预测是一热编码?

问题描述 投票:0回答:1

嗨,我写了这些代码,它是完全正常的,但不知道如何反转ypred与ytest比较。

import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn import datasets

from keras.utils import to_categorical


data=datasets.load_iris()

x=data.data
y=to_categorical(data.target)

xtrain, xtest, ytrain, ytest=train_test_split(x, y,test_size=1/3)

sc=StandardScaler()
xtrain=sc.fit_transform(xtrain)
xtest=sc.transform(xtest)

ann_model=Sequential()

ann_model.add(Dense(units=4,activation='relu', kernel_initializer='uniform', input_dim=4))
ann_model.add(Dense(units=4, activation='relu', kernel_initializer='uniform'))
ann_model.add(Dense(units=3, activation='softmax', kernel_initializer='uniform'))

ann_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

ann_model.fit(xtrain, ytrain,batch_size=8,epochs=800)

ypred=ann_model.predict(xtest)

在这之后,我得到了一个标准化的ypred,像这样。

   [9.9993205e-01, 6.7994297e-05, 1.4203579e-19],
   [5.3556296e-12, 4.2303108e-02, 9.5769691e-01],
   [3.1650116e-04, 9.9964631e-01, 3.7194797e-05],
   [1.4751430e-05, 9.9975187e-01, 2.3338773e-04],
   [9.9994361e-01, 5.6439614e-05, 6.4687055e-20],
   [2.6651847e-04, 9.9968839e-01, 4.5110301e-05],
   [1.6542191e-06, 9.9968910e-01, 3.0929857e-04],
   [9.9991632e-01, 8.3733095e-05, 3.4217699e-19],
   [5.8562500e-07, 9.9891603e-01, 1.0833564e-03],
   [2.7507697e-06, 9.9960250e-01, 3.9476002e-04],
   [9.9997449e-01, 2.5457492e-05, 2.2423828e-21],
   [7.1067189e-14, 5.0079697e-03, 9.9499208e-01],

但我希望我的ypred像ytest一样是1和0:

 [0., 1., 0.],
   [0., 0., 1.],
   [1., 0., 0.],
   [0., 0., 1.],
   [0., 1., 0.],
   [1., 0., 0.],
   [0., 0., 1.],
   [0., 0., 1.],
   [0., 0., 1.],

我怎么能逆转我的ypred谢谢你的帮助。

python keras scale perceptron
1个回答
0
投票

你可以使用 np.argmaxkeras.utils.to_categorical:

import numpy as np
from tensorflow.keras.utils import to_categorical

arr = np.array([[9.9993205e-01, 6.7994297e-05, 1.4203579e-19],
   [5.3556296e-12, 4.2303108e-02, 9.5769691e-01],
   [3.1650116e-04, 9.9964631e-01, 3.7194797e-05],
   [1.4751430e-05, 9.9975187e-01, 2.3338773e-04],
   [9.9994361e-01, 5.6439614e-05, 6.4687055e-20],
   [2.6651847e-04, 9.9968839e-01, 4.5110301e-05],
   [1.6542191e-06, 9.9968910e-01, 3.0929857e-04],
   [9.9991632e-01, 8.3733095e-05, 3.4217699e-19],
   [5.8562500e-07, 9.9891603e-01, 1.0833564e-03],
   [2.7507697e-06, 9.9960250e-01, 3.9476002e-04],
   [9.9997449e-01, 2.5457492e-05, 2.2423828e-21],
   [7.1067189e-14, 5.0079697e-03, 9.9499208e-01]])
new_array = to_categorical(np.argmax(arr, axis=1), 3)

np.argmax 将返回值最大的索引(0、1或2),以及 to_categorical 将一热这个新的数组,所以在预测最高的地方有一个1。结果。

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

要跳过一步,你也可以用

ypred=ann_model.predict_classes(xtest)

这将预测0,1或2,然后你只需要做我建议的最后一步。

new_array = to_categorical(y_pred, 3)

虽然完全公开,我还没有试过最后的解决方案(我没有一个可行的例子)。

© www.soinside.com 2019 - 2024. All rights reserved.