将字符串标签编码为整数以进行分层多类分类

发布于 2025-01-14 06:42:43 字数 680 浏览 2 评论 0原文

目前正在处理分层分类任务,我给模型提供了一些文本输入,它告诉我它属于哪 3 个类别(主类别、子类别、叶类别)。

我的标签当前是字符串列表,但对于我的模型,我需要它们作为 0 到 N-1 之间的整数(N 是此类别级别中的类数)。

我设法通过使用字典并给每个字符串一个数字(编码)找到解决方案,然后另一个字典执行相反的操作(解码)以检查预测是什么。

它有效,但我想一旦我使用另一个数据集,我就必须再次生成两个字典。 我环顾四周,从 sklearn 预处理库中找到了这两个类: MultiLabelBinarizer()LabelEncoder()。它看起来像我需要的,但我不知道如何将它与层次结构结合使用。

对于如何使用它们或任何其他解决方案的任何提示,我们将不胜感激!

Currently working on a hierarchical classification task, where I give my model some text input and it tells me, which 3 categories it belongs to (main category, sub category, leaf category).

My labels are currently a list of strings, but for my model I need them as integers between 0 to N-1 (N being the number of classes in this category level).

I managed to find a solution by using a dictionary and giving each string a number (encoding) and then another dictionary doing the opposite (decoding) to check what the predictions are.

It works, but I figured once I use another dataset, I would have to generate two dictionaries again.
I looked around and found these two classes from the sklearn preprocessing library: MultiLabelBinarizer() and LabelEncoder(). It looks like what I would need, but I have no idea how to use it in combination with the hierarchies.

Would appreciate any kind of hints on how to use them or any other solution!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文