<jupyter_start><jupyter_text>Text classification<jupyter_code>text=['i am happy', 'i very friendly','you are happy', 'you are friendly','we are friendly', 'you are bad',
      'i am bad','i am unhappy','you are unhappy', 'we are unhappy', 'so bad']
category=['pos','pos','pos','pos','pos','neg','neg','neg','neg','neg', 'neg']
data=pd.DataFrame({"text":text, "category":category})
data<jupyter_output><empty_output><jupyter_text>initialize model object<jupyter_code>cf_cv=ClassifierCv(data['category'],data['text'])<jupyter_output><empty_output><jupyter_text>gridsearch<jupyter_code>#model uses skleanr Pipeline object to process data
cf_cv.prepare_pipeline( [('vect', CountVectorizer()),
                          ('tfidf', TfidfTransformer()),
                          ('crf', MultinomialNB()) ])

param_dist = {'crf__alpha':[0.01, 0.02, 0.05, 0.07, 0.09]}

cf_cv.perform_random_search(param_dist)
cf_cv.print_top_random_search(3)<jupyter_output>Model with rank: 1
Mean validation score: 0.806 (std: 0.119)
Parameters: {'crf__alpha': 0.01}

Model with rank: 1
Mean validation score: 0.806 (std: 0.119)
Parameters: {'crf__alpha': 0.02}

Model with rank: 3
Mean validation score: 0.661 (std: 0.269)
Parameters: {'crf__alpha': 0.05}

Model with rank: 3
Mean validation score: 0.661 (std: 0.269)
Parameters: {'crf__alpha': 0.07}

Model with rank: 3
Mean validation score: 0.661 (std: 0.269)
Parameters: {'crf__alpha': 0.09}<jupyter_text>train model if plot_path and metric_path are not None metrics files and plots will be saved<jupyter_code>cf_cv.train_save_metrics([('vect', CountVectorizer()),
                          ('tfidf', TfidfTransformer()),
                          ('clf', MultinomialNB(alpha=.01)),], 
                         'f1', 
                         'MultinomialNB',
                        num_cv=2)
#per category metrics
cf_cv.make_metric_boxplot(metric='f1')
cf_cv.plot_confusion_matrix()<jupyter_output>Confusion matrix, without normalization
[[5 0]
 [4 2]]<jupyter_text>calculate accuracy using evaluation data<jupyter_code>df_eval=pd.DataFrame({'text':['so good','very happy guy','very well unfriendly','it is bad'],
                     'category':['pos','pos','neg','neg']})
print(cf_cv.calc_evaluation_report(df_eval['text'], df_eval['category']))<jupyter_output>precision    recall  f1-score   support

        neg       0.50      0.50      0.50         2
        pos       0.50      0.50      0.50         2

avg / total       0.50      0.50      0.50         4<jupyter_text>compare each class accuracy to number of samples<jupyter_code>cf_cv.plot_acc_vs_nsamples()<jupyter_output><empty_output><jupyter_text>Fasttext Most models implemented are from sklearn package. Only one which is not is FastText. FastText takes text as an input. Otherwise usage is similar to previous.<jupyter_code>from TextClass.FasttextClassifier import FasttextClassifier
#model uses sklearn Pipeline object to process data
#always saves model binary
model_name='ft_example'
cf_cv.prepare_pipeline( [('crf', FasttextClassifier(output=model_name)) ])

param_dist = {'crf__epoch':[2,3,4]}

cf_cv.perform_random_search(param_dist)
cf_cv.print_top_random_search(3)
cf_cv.train_save_metrics([('clf', FasttextClassifier(epoch=4)),], 
                         'f1', 
                         'fasttext',
                        num_cv=2)
cf_cv.predict(['i am bad'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13529411764705881, 'fraction_numerical': 0.036176470588235296, 'mean_word_length': 3.86552217453505, 'pattern_counts': {'":': 2, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '326616', 'license': 'MIT', 'n_tokens_mistral': 1212, 'n_tokens_neox': 1123, 'n_words': 254, 'path': 'examples/2.0_text_classification_and_fasttext.ipynb', 'repo_name': 'RRisto/simple_classifiy', 'size': '3400'}