Text Classification Baseline
Pipeline for fast building text classification baselines with TF-IDF + LogReg.
Usage
Instead of writing custom code for specific text classification task, you just need:
- install pipeline:
pip install text-classification-baseline
- run pipeline:
- either in terminal:
text-clf-train --path_to_config config.yaml
- or in python:
import text_clf
model, target_names_mapping = text_clf.train(path_to_config="config.yaml")
NOTE: more about config file here.
No data preparation is needed, only a csv file with two raw columns (with arbitrary names):
texttarget
The target can be presented in any format, including text - not necessarily integers from 0 to n_classes-1.
Config
The user interface consists of two files:
- config.yaml - general configuration with sklearn TF-IDF and LogReg parameters
- hyperparams.py - sklearn GridSearchCV parameters
Change config.yaml and hyperparams.py to create the desired configuration and train text classification model with the following command:
- terminal:
text-clf-train --path_to_config config.yaml
- python:
import text_clf
model, target_names_mapping = text_clf.train(path_to_config="config.yaml")
Default config.yaml:
seed: 42
path_to_save_folder: models
experiment_name: model
# data
data:
train_data_path: data/train.csv
test_data_path: data/test.csv
sep: ','
text_column: text
target_column: target_name_short
# preprocessing
# (included in resulting model pipeline, so preserved for inference)
preprocessing:
lemmatization: null # pymorphy2
# tf-idf
tf-idf:
lowercase: true
ngram_range: (1, 1)
max_df: 1.0
min_df: 1
# logreg
logreg:
penalty: l2
C: 1.0
class_weight: balanced
solver: saga
n_jobs: -1
# grid-search
grid-search:
do_grid_search: false
grid_search_params_path: hyperparams.py
NOTE: grid search is disabled by default, to use it set do_grid_search: true.
NOTE: tf-idf and logreg are sklearn TfidfVectorizer and LogisticRegression parameters correspondingly, so you can parameterize instances of these classes however you want. The same logic applies to grid-search which is sklearn GridSearchCV parametrized with hyperparams.py.
Output
After training the model, the pipeline will return the following files:
model.joblib- sklearn pipeline with TF-IDF and LogReg stepstarget_names.json- mapping from encoded target labels from 0 to n_classes-1 to it namesconfig.yaml- config that was used to train the modelhyperparams.py- grid-search parameters (if grid-search was used)logging.txt- logging file
Additional functions
text_clf.token_frequency.get_token_frequency(path_to_config)-
get token frequency of train dataset according to the config file parameters
Only for binary classifiers:
text_clf.pr_roc_curve.get_precision_recall_curve(path_to_model_folder)-
get precision and recall metrics for precision-recall curvetext_clf.pr_roc_curve.get_roc_curve(path_to_model_folder)-
get false positive rate (fpr) and true positive rate (tpr) metrics for roc curvetext_clf.pr_roc_curve.plot_precision_recall_curve(precision, recall)-
plot precision-recall curvetext_clf.pr_roc_curve.plot_roc_curve(fpr, tpr)-
plot roc curvetext_clf.pr_roc_curve.plot_precision_recall_f1_curves_for_thresholds(precision, recall, thresholds)-
plot precision, recall, f1-score curves for probability thresholds
Requirements
Python >= 3.6
Citation
If you use text-classification-baseline in a scientific publication, we would appreciate references to the following BibTex entry:
@misc{dayyass2021textclf,
author = {El-Ayyass, Dani},
title = {Pipeline for training text classification baselines},
howpublished = {\url{https://github.com/dayyass/text-classification-baseline}},
year = {2021}
}
Metadata
Release files for text-classification-baseline 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| text-classification-baseline-0.1.6.tar.gz | 9.9 kB | Without provenance | Details for text-classification-baseline-0.1.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| text_classification_baseline-0.1.6-py3-none-any.whl | Python 3 | none | any | Details for text_classification_baseline-0.1.6-py3-none-any.whl |
Total release size: 23.5 kB
Release files / text-classification-baseline-0.1.6.tar.gz
| Download URL | text-classification-baseline-0.1.6.tar.gz |
|---|---|
| Size | 9.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a43a2ecf22f63210685af21d7ed76e35bda9e0b9740786ed61094b642240aedf
|
|
BLAKE2b-256 checksum How to use checksums |
077380a8f224c464ec8615a62473c6eea42d9e5d5b74feebb13032b9c97bb10c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.5
|
Release files / text_classification_baseline-0.1.6-py3-none-any.whl
| Download URL | text_classification_baseline-0.1.6-py3-none-any.whl |
|---|---|
| Size | 13.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
133c5cb791d460e9d7415b12834e11f7812ea4654ec431afdd535962d0031e45
|
|
BLAKE2b-256 checksum How to use checksums |
fdefc6705136a2ec37f7a4cfde4af0d72492bd0923127c6bac125021b5125baa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.5
|
Security for text-classification-baseline 0.1.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.