.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_classify_newsgroups.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_classify_newsgroups.py: =================================================== Sparse Text Classification with Vocabulary Limits =================================================== Shows how to pair ``TfidfVectorizer`` with ``TlmMilpClassifier`` by constraining vocabulary dimension to prevent exponential MILP solver time, visualizing the resulting ternary text weights. .. GENERATED FROM PYTHON SOURCE LINES 10-62 .. image-sg:: /auto_examples/images/sphx_glr_plot_classify_newsgroups_001.png :alt: Exact MILP Selected Terms (Text Classification) :srcset: /auto_examples/images/sphx_glr_plot_classify_newsgroups_001.png :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import fetch_20newsgroups from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.pipeline import Pipeline from tlmnet import TlmMilpClassifier # Fetch binary text dataset subset categories = ["alt.atheism", "sci.space"] newsgroups = fetch_20newsgroups( subset="train", categories=categories, remove=("headers", "footers", "quotes") ) # Constrain vocabulary to top 25 terms to keep MILP dimension tractable pipeline = Pipeline( [ ( "tfidf", TfidfVectorizer(max_features=25, stop_words="english", sublinear_tf=True), ), ("tlm", TlmMilpClassifier(max_features=8, C=1.0)), ] ) pipeline.fit(newsgroups.data, newsgroups.target) # Retrieve selected words and their discrete ternary weights feature_names = pipeline.named_steps["tfidf"].get_feature_names_out() weights = pipeline.named_steps["tlm"].coef_ # Filter down to only the non-zero features selected by the MILP solver mask = weights != 0 active_words = feature_names[mask] active_weights = weights[mask] # Sort by weight for cleaner plotting sort_idx = np.argsort(active_weights) active_words = active_words[sort_idx] active_weights = active_weights[sort_idx] # Plot the ternary weights plt.figure(figsize=(8, 4)) colors = ["#d62728" if w < 0 else "#2ca02c" for w in active_weights] plt.barh(active_words, active_weights, color=colors, edgecolor="black") plt.xlabel("Ternary Weight (-1 or +1)") plt.title("Exact MILP Selected Terms (Text Classification)") plt.xticks([-1, 0, 1]) plt.grid(axis="x", linestyle="--", alpha=0.7) plt.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 10.650 seconds) .. _sphx_glr_download_auto_examples_plot_classify_newsgroups.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_classify_newsgroups.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_classify_newsgroups.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_classify_newsgroups.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_