如何让 Keras 预测成为一个热编码?
How do I get Keras predictions to be one hot encoded?
嗨,我写了这些代码,它完全没问题,但不知道如何反转 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 谢谢你的帮助。
您可以使用 np.argmax
和 keras.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
将 return 值最高的索引(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)
虽然完全公开,但我还没有尝试过最后一个解决方案(我没有一个有效的例子)。
嗨,我写了这些代码,它完全没问题,但不知道如何反转 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 谢谢你的帮助。
您可以使用 np.argmax
和 keras.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
将 return 值最高的索引(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)
虽然完全公开,但我还没有尝试过最后一个解决方案(我没有一个有效的例子)。