Package: text-classification-baseline
Text Classification BaselinePipeline for fast building text classification baselines withTF-IDF + LogReg.UsageInstead of writing custom code for specific text classification task, you just need:install pipeline:pipinstalltext-classification-baselinerun pipeline:either interminal:text-clf-train--path_to_configconfig.yamlor inpython:importtext_clfmodel,target_names_mapping=text_clf.train(path_to_config="config.yaml")NOTE: more about config filehere.No data preparation is needed, only acsvfile with two raw columns (with arbitrary names):texttargetThetargetcan be presented in any format, including text - not necessarily integers from0ton_classes-1.ConfigThe user interface consists of two files:config.yaml- general configuration with sklearnTF-IDFandLogRegparametershyperparams.py- sklearnGridSearchCVparametersChangeconfig.yamlandhyperparams.pyto create the desired configuration and train text classification model with the following command:terminal:text-clf-train--path_to_configconfig.yamlpython:importtext_clfmodel,target_names_mapping=text_clf.train(path_to_config="config.yaml")Defaultconfig.yaml:seed:42path_to_save_folder:modelsexperiment_name:model# datadata:train_data_path:data/train.csvtest_data_path:data/test.csvsep:','text_column:texttarget_column:target_name_short# preprocessing# (included in resulting model pipeline, so preserved for inference)preprocessing:lemmatization:null# pymorphy2# tf-idftf-idf:lowercase:truengram_range:(1, 1)max_df:1.0min_df:1# logreglogreg:penalty:l2C:1.0class_weight:balancedsolver:sagan_jobs:-1# grid-searchgrid-search:do_grid_search:falsegrid_search_params_path:hyperparams.pyNOTE: grid search is disabled by default, to use it setdo_grid_search: true.NOTE:tf-idfandlogregare sklearnTfidfVectorizerandLogisticRegressionparameters correspondingly, so you can parameterize instances of these classes however you want. The same logic applies togrid-searchwhich is sklearnGridSearchCVparametrized withhyperparams.py.OutputAfter 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 from0ton_classes-1to it namesconfig.yaml- config that was used to train the modelhyperparams.py- grid-search parameters (if grid-search was used)logging.txt- logging fileAdditional functionstext_clf.token_frequency.get_token_frequency(path_to_config)-get token frequency oftrain datasetaccording to the config file parametersOnly for binary classifiers:text_clf.pr_roc_curve.get_precision_recall_curve(path_to_model_folder)-getprecisionandrecallmetrics for precision-recall curvetext_clf.pr_roc_curve.get_roc_curve(path_to_model_folder)-getfalse positive rate (fpr)andtrue positive rate (tpr)metrics for roc curvetext_clf.pr_roc_curve.plot_precision_recall_curve(precision, recall)-plotprecision-recall curvetext_clf.pr_roc_curve.plot_roc_curve(fpr, tpr)-plotroc curvetext_clf.pr_roc_curve.plot_precision_recall_f1_curves_for_thresholds(precision, recall, thresholds)-plotprecision,recall,f1-scorecurves for probability thresholdsRequirementsPython >= 3.6CitationIf you usetext-classification-baselinein 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}}