【问题标题】:How do I get Keras predictions to be one hot encoded?如何让 Keras 预测成为一种热编码?
【发布时间】:2020-08-28 13:24:18
【问题描述】:

嗨,我写了这些代码,它完全没问题,但不知道如何反转 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】:

    您可以使用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 将对这个新数组进行 one-hot,因此预测最高的位置为 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)
    

    虽然完全公开,但我还没有尝试过最后一个解决方案(我没有一个工作示例)。

    【讨论】:

      猜你喜欢
      • 2019-07-17
      • 1970-01-01
      • 2020-05-01
      • 2016-03-19
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 2020-01-16
      • 2019-02-18
      相关资源
      最近更新 更多