将一个热编码结果转换回 Python 中的单列 [英] Coverting Back One Hot Encoded Results back to single Column in Python

查看:15
本文介绍了将一个热编码结果转换回 Python 中的单列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Keras 进行多类分类.它包含 5 个输出类.我使用一种热编码将单个类向量转换为矩阵并制作了一个模型.现在要评估模型,我想将 5 类概率结果转换回单列.

I was doing Multi-class Classification using Keras.It contained 5 classes of Output. I converted the single class vector to matrix using one hot encoding and made a model. Now to evaluate the model I want to convert back the 5 class probabilistic result back to Single Column.

我将其作为 numpy 数组格式的输出

I am getting this as output in numpy array format

..................0..................1............................2.......................3.............................4

 5.35433665e-02   1.72592481e-05   1.49291719e-03   9.44392741e-01
    5.53713820e-04  
   1.97096306e-05   2.08907949e-08   3.11666554e-07   1.40611945e-07
    9.99979794e-01  
   9.99999225e-01   2.42999278e-07   1.58917388e-07   7.84497018e-08
    2.85837785e-07  
   7.09977685e-05   1.02068476e-09   1.38186664e-07   9.99928594e-01
    2.73126261e-07  
   1.29937407e-05   2.49388819e-07   9.99986231e-01   4.76015231e-07
    7.39421040e-08 

想把这个矩阵转换成

[3,4,0,3,2]

推荐答案

您似乎在寻找 np.argmax:

It seems like you are looking for np.argmax:

import numpy as np
class_labels = np.argmax(class_prob, axis=1) # assuming you have n-by-5 class_prob

这篇关于将一个热编码结果转换回 Python 中的单列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆