.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_decision_thresholds.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_decision_thresholds.py: =================================================== Decision Boundaries & Margin Scores =================================================== Visualizes the discrete decision boundaries generated by ternary weights :math:`w_j \in \{-1, 0, 1\}` on a 2D synthetic dataset. .. GENERATED FROM PYTHON SOURCE LINES 9-47 .. image-sg:: /auto_examples/images/sphx_glr_plot_decision_thresholds_001.png :alt: TLM Decision Surface (coef=[np.float64(1.0), np.float64(0.0)]) :srcset: /auto_examples/images/sphx_glr_plot_decision_thresholds_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Fitted Coefficients: [1. 0.] | .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import make_classification from sklearn.preprocessing import StandardScaler from tlmnet import TlmMilpClassifier X, y = make_classification( n_samples=100, n_features=2, n_redundant=0, n_informative=2, random_state=42, class_sep=1.2, ) X = StandardScaler().fit_transform(X) clf = TlmMilpClassifier(C=1.0) clf.fit(X, y) print(f"Fitted Coefficients: {clf.coef_}") # Plot Decision Boundary x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.linspace(x_min, x_max, 200), np.linspace(y_min, y_max, 200)) Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.figure(figsize=(7, 6)) plt.contourf(xx, yy, Z, alpha=0.3, cmap=plt.cm.coolwarm) plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.coolwarm, edgecolors="k", linewidths=0.5) plt.xlabel("Standardized Feature 1") plt.ylabel("Standardized Feature 2") plt.title(f"TLM Decision Surface (coef={list(clf.coef_)})") plt.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.118 seconds) .. _sphx_glr_download_auto_examples_plot_decision_thresholds.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_decision_thresholds.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_decision_thresholds.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_decision_thresholds.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_