I have a Pandas DataFrame which consists of a body of text in 1 row. Each body of text is assigned labels for Category (in 1 row) and a Topic (in a separate row). Example of DataFrame below:

Text Category Topic
cognitive training ... Category A Topic 2
correlation between ... Category F Topic 56

There are 8 Categories with a total of 60 unique Topics. These Topics are not equally shared amongst the Categories, for example Category A can have Topics 1-18 and Category B can have Topics 19-27. A basic diagram has been added below demonstrating the levels.

           /            |        \          \ ...
        Cat A         Cat B      Cat C
      / / | \ \        |  \        | 
     1 2  3  4 5 ...  19  20 ...   28

I would like to know the best approach(s) for generating a Hierarchical Classification where both the Category and Topic are generated in the prediction. So if a new body of text is received both levels can be predicted e.g. Category and Topic.

I have been using TfIdf on the body of text and how next to proceed is troubling me.

from sklearn_hierarchical_classification.classifier import HierarchicalClassifier

Attempting to use this library isnt providing me with both levels as a prediction.

Any help or ideas in implementing such a model would be really appreciated :) Many thanks!

  • $\begingroup$ In other words, do you want to classify according to the topic's meaning? For instance: "insect" or "bug" goes to "Insect category" and "keyboard" or "computer bug" go to "IT category"? $\endgroup$ Oct 27, 2022 at 17:59
  • $\begingroup$ Hi @NicolasMartin , thanks for your reply. I would like to get both levels of the classification and see the path. So I want to see both the Category and Topic classified. If classifying animals for example and the model predicts "snake" I also want to see the level above that "reptile". To ensure the upper level is correct as well. Does that make sense? $\endgroup$
    – user142080
    Oct 28, 2022 at 7:05

1 Answer 1


Bert is a model that is able to extract meaning from text and make good classifications, even if the text hasn't been learned before.

That's why you can train a Bert model with many sentences that cover all classes, and then you should be able to classify any text.



This is the most universal solution adapted to any text.

Here are all text classification models:


They are mostly built to classify sentiment, but they could classify anything, like this one:



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.