我正在使用sklearnLabelEncoder。我知道如何将其用于一维阵列,但我的用例如下:
我有多个这样的dict数组(这实际上是在分类器中分配每个文本标签u'a'
,u'b'
等的开销),所有这些都在dict中:
{'open_model':
[
{u'a': 47502.125, u'c': 45.3, u'd': 2.3, u'e': 0.45},
{u'b': 121, u'a': 1580.5625, u'c': 12, u'e': 62,u'd':0.343},
{u'e': 12321, u'b': 4, u'a': 0.1112}
],
'closed_model':
[
{u'a': 1231.22, u'c': 43.1},
{u'b': 342.2, u'a': 121.1, u'c': 343},
{u'b': 14.2, u'a': 53.2}
]
}
我需要能够将其编码为数字标签,然后解码回所有标签,例如:
^{pr2}$我可以有效地为每一行生成最佳标签的预测,因此:
[5, 4, 1] perhaps in this case.
我需要做的是把这个解码回:
[u'e',u'd', u'a'] perhaps in this case.
如何在dict键是我的标签的dict数组上获得相同的LabelEncoder
功能?在
注意,dict数组中的dict是一个不同的长度,但是我有所有潜在标签的列表,即对于开放的模型标签,set([u'a',u'b',u'c',u'd',u'e'])
,对于封闭的模型标签:set([u'a',u'b',u'c'])
。在
似乎你总是有“a”、“b”、“c”、“d”、“e”。如果是这样的话,你为什么不使用熊猫数据帧而忘记编码器呢?你有点需要重写你使用的字典的键,所以它无论如何都会很混乱!在
这不是直截了当的,但我想它会比使用编码器花费更少的时间:)
虽然使用已经实现的功能是一个很好的实践,但是您可以通过几行代码轻松地实现这一点。根据您的列表输入:
您可以通过以下方法获得标签集:
^{pr2}$使用
mapping
和inv_mapping
可以通过以下方式更改数据的表示:这将给您
[{1: 47502.125, ...}]
,然后如果需要:以获得初始版本。在
另外,可能与您的问题更密切相关,您可以通过以下方式轻松转换输出
[5, 4, 1]
:相关问题 更多 >
编程相关推荐