Package: hiclass
HiClassHiClass is an open-source Python library for hierarchical classification compatible with scikit-learn.✨ Here is ademothat shows HiClass in action on hierarchical data:Classify a consumer complaints dataset from the consumer financial protection bureau:consumer-complaintsQuick linksFeaturesBenchmarksRoadmapWho is using HiClass?InstallQuick startStep-by-step walk-throughAPI documentationFAQSupportContributingGetting the latest updatesCitationFeaturesPython lists and NumPy arrays:Handles Python lists and NumPy arrays elegantly, out-of-the-box.Pandas Series and DataFrames:If you prefer to use pandas, that is not an issue as HiClass also works with Pandas.Sparse matrices:HiClass also supports features (X_train and X_test) built with sparse matrices, both for training and predicting, which can save you heaps of memory.Parallel training:Training can be performed in parallel on the hierarchical classifiers, which allows parallelization regardless of the implementations available on scikit-learn.Build pipelines:Since the hierarchical classifiers inherit from the BaseEstimator of scikit-learn, pipelines can be built to automate machine learning workflows.Hierarchical metrics:HiClass supports the computation of hierarchical precision, recall and f-score, which are more appropriate for hierarchical data than traditional metrics.Compatible with pickle:Easily store trained models on disk for future use.BERT sklearn:Compatible with the libraryBERT sklearn.Any feature missing on this list?Search ourissue trackerto see if someone has already requested it and add a comment to it explaining your use-case. Otherwise, please open a new issue describing the requested feature and possible use-case scenario. We prioritize our roadmap based on user feedback, so we would love to hear from you.BenchmarksConsumer complaints dataset with ~600K training examplesThis first benchmark was executed on Google Colab with only 1 core, using Logistic Regression as the base classifier.ClassifierTraining Time (hh:mm:ss)Memory Usage (GB)Disk Usage (MB)F-scoreLocal Classifier per Parent Node00:52:585.281210.7689Local Classifier per Node00:33:024.871230.7647Local Classifier per Level04:14:4510.711230.7684Flat Classifier03:20:269.571070.7636This second benchmark is similar to the last one, except that it was executed on multiple cluster nodes running GNU/Linux with 512 GB physical memory and 128
cores provided by two AMD EPYC™ 7742 processors, and each model had 12 cores available for training.ClassifierTraining Time (hh:mm:ss)Memory Usage (GB)Disk Usage (MB)F-scoreLocal Classifier per Parent Node00:32:059.301220.7798Local Classifier per Node00:04:0521.011230.7763Local Classifier per Level02:24:4411.451240.7795Flat Classifier00:57:163.151080.7748This third benchmark was also executed on the same cluster node as the previous benchmark and 12 cores were provided for each model, however, the base classifier was LightGBM instead.ClassifierTraining Time (hh:mm:ss)Memory Usage (GB)Disk Usage (MB)F-scoreLocal Classifier per Parent Node00:28:009.00770.7531Local Classifier per Node00:55:5531.924120.7901Local Classifier per Level01:35:269.04360.6854Flat Classifier01:11:244.54300.3710Lastly, this fourth benchmark was also executed on the same cluster node as the previous benchmarks and 12 cores were provided for each model, however, the base classifier was random forest instead.ClassifierTraining Time (hh:mm:ss)Memory Usage (GB)Disk Usage (GB)F-scoreLocal Classifier per Parent Node07:34:4748.30240.7407Local Classifier per Node06:50:1755.19270.7668Local Classifier per Level09:45:18191.39960.7383Flat Classifier01:26:55162.40810.6672For reproducibility, a Snakemake pipeline was created. Instructions on how to run it and source code are available athttps://github.com/scikit-learn-contrib/hiclass/tree/main/benchmarks/consumer_complaints.We would love to benchmark with larger datasets, if we can find them in the public domain. If you have any suggestions for hierarchical datasets that are public, please let us know by opening an issue. We would also be delighted if you are able to share benchmarks from your own large datasets. Please send us a pull request.RoadmapHere is our public roadmap:https://github.com/scikit-learn-contrib/hiclass/projects/1.We do Just-In-Time planning, and we tend to reprioritize based on your feedback. Hence, items you see on this roadmap are subject to change. We prioritize features based on the number of people asking for it, features/fixes that are small enough and can be addressed while we work on other related features, features/fixes that help improve stability & relevance and features that address interesting use cases that excite us! If you would like to have a request prioritized, we ask that you add a detailed use-case for it, either as a comment on an existing issue (besides a thumbs-up) or in a new issue. The detailed context helps.Who is using HiClass?HiClass is currently being used inHiTaC, a hierarchical taxonomic classifier for fungal ITS sequences.If you use HiClass in one of your projects and would like to have it listed here, please send us a pull request or contactfabio.malchermiranda@hpi.de.InstallOption 1: PipHiClass and its dependencies can be easily installed with pip:pipinstallhiclassIf you need additional functionality, you can install extra dependencies using the following syntax:pipinstallhiclass"[<extra_name>]"Replace <extra_name> with one of the following options:ray: Installs the ray package, which is required for parallel processing support.xai: Installs the shap and xarray packages, which are required for explaining Hiclass predictions.Option 2: CondaAlternatively, HiClass and its dependencies can also be installed with conda:condainstall-cconda-forgehiclassFurther installation instructions are available on ourgetting started guide. This will guide you through the process of setting up an isolated Python virtual environment with conda, venv or pipenv before installing hiclass with conda or pip, and how to verify a successful installation.Quick startHere's a quick example showcasing how you can train and predict using a local classifier per node, with aRandomForestClassifierfor each node:fromhiclassimportLocalClassifierPerNodefromsklearn.ensembleimportRandomForestClassifier# Define dataX_train=[[1],[2],[3],[4]]X_test=[[4],[3],[2],[1]]Y_train=[['Animal','Mammal','Sheep'],['Animal','Mammal','Cow'],['Animal','Reptile','Snake'],['Animal','Reptile','Lizard'],]# Use random forest classifiers for every noderf=RandomForestClassifier()classifier=LocalClassifierPerNode(local_classifier=rf)# Train local classifier per nodeclassifier.fit(X_train,Y_train)# Predictpredictions=classifier.predict(X_test)HiClass can also be adopted in scikit-learn pipelines, and fully supports sparse matrices as input. In order to demonstrate the use of both of these features, we will use the following example:fromhiclassimportLocalClassifierPerParentNodefromsklearn.feature_extraction.textimportCountVectorizer,TfidfTransformerfromsklearn.linear_modelimportLogisticRegressionfromsklearn.pipelineimportPipeline# Define dataX_train=['Struggling to repay loan','Unable to get annual report',]X_test=['Unable to get annual report','Struggling to repay loan',]Y_train=[['Loan','Student loan'],['Credit reporting','Reports']]Now, let's build a pipeline that will useCountVectorizerandTfidfTransformerto extract features as sparse matrices:# Use logistic regression classifiers for every parent nodelr=LogisticRegression()pipeline=Pipeline([('count',CountVectorizer()),('tfidf',TfidfTransformer()),('lcppn',LocalClassifierPerParentNode(local_classifier=lr)),])Finally, let's train and predict with the pipeline we just created:# Train local classifier per parent nodepipeline.fit(X_train,Y_train)# Predictpredictions=pipeline.predict(X_test)Step-by-step walk-throughA step-by-step walk-through is available on our documentation hosted onRead the Docs.This will guide you through the process of installing hiclass within a virtual environment, training, predicting, persisting models and much more.API documentationHere's our official API documentation, available onRead the Docs.If you notice any issues with the documentation or walk-through, please let us know by opening an issue here:https://github.com/scikit-learn-contrib/hiclass/issues.FAQHow do the hierarchical classifiers work?A detailed description on how the classifiers work is available at theAlgorithms Overviewsection on Read the Docs.SupportIf you run into any problems or issues, please create aGithub issueand we'll try our best to help.We strive to provide good support through our issue tracker on Github. However, if you'd like to receive private support with:Phone / video calls to discuss your specific use case and get recommendationsPrivate discussions over Slack or MattermostPlease reach out tofabio.malchermiranda@hpi.de.ContributingWe are a small team on a mission to democratize hierarchical classification, and we will take all the help we can get! If you would like to get involved, here is information oncontribution guidelines and how to test the code locally.You can contribute in multiple ways, e.g., reporting bugs, writing or translating documentation, reviewing or refactoring code, requesting or implementing new features, etc.Getting the latest updatesIf you'd like to get updates when we release new versions, please click on the "Watch" button on the top and select "Releases only". Github will then send you notifications along with a changelog with each new release.CitationIf you use HiClass in your research, please cite ourpaper:Miranda, F.M., Köehnecke, N. and Renard, B.Y. (2023) 'HiClass: a Python Library for Local Hierarchical Classification Compatible with Scikit-learn', Journal of Machine Learning Research, 24(29), pp. 1–17. Available at:https://jmlr.org/papers/v24/21-1518.html.@article{JMLR:v24:21-1518,
  author  ={F{\'a}bio M. Miranda and Niklas K{\"o}hnecke and Bernhard Y. Renard},
  title   ={HiClass: a Python Library for Local Hierarchical Classification Compatible with Scikit-learn},
  journal ={Journal of Machine Learning Research},
  year    ={2023},
  volume  ={24},
  number  ={29},
  pages   ={1--17},
  url     ={http://jmlr.org/papers/v24/21-1518.html}}Note: If you use HiClass in your GitHub projects, please addhiclassin therequirements.txt.In addition, we would like to list publications that use HiClass to solve hierarchical problems. If you would like your manuscript to be added to this list, please email the reference, the name of your lab, department and institution tofabio.malchermiranda@hpi.de