sklearn-docs/support-vectors-LinearSVC
0
1import gradio as gr2import numpy as np3import matplotlib.pyplot as plt4import warnings5 6from functools import partial7from sklearn.datasets import make_blobs, make_spd_matrix8from sklearn.svm import LinearSVC9from sklearn.inspection import DecisionBoundaryDisplay10from sklearn.exceptions import ConvergenceWarning11 12def train_model(n_samples, C, penalty, loss, max_iter):13 14 if penalty == "l1" and loss == "hinge":15 raise gr.Error("The combination of penalty='l1' and loss='hinge' is not supported")16 17 default_base = {"n_samples": 20}18 19 # Algorithms to compare20 params = default_base.copy()21 params.update({"n_samples":n_samples,22 "C": C,23 "penalty": penalty,24 "loss": loss,25 "max_iter": max_iter})26 27 X, y = make_blobs(n_samples=params["n_samples"], centers=2, random_state=0)28 29 fig, ax = plt.subplots()30 31 # catch warnings related to convergence32 with warnings.catch_warnings():33 warnings.filterwarnings("ignore", category=ConvergenceWarning)34 35 # add penalty, l1 and l2. Default is l236 # add loss, square_hinge is Default. the other loss is hinge37 # multi_class{‘ovr’, ‘crammer_singer’}, default=’ovr’38 39 clf = LinearSVC(penalty=penalty, C=params["C"], 40 loss=params["loss"], 41 max_iter=params["max_iter"], 42 random_state=42).fit(X, y)43 # obtain the support vectors through the decision function44 decision_function = clf.decision_function(X)45 # we can also calculate the decision function manually46 # decision_function = np.dot(X, clf.coef_[0]) + clf.intercept_[0]47 # The support vectors are the samples that lie within the margin48 # boundaries, whose size is conventionally constrained to 149 support_vector_indices = np.where(np.abs(decision_function) <= 1 + 1e-15)[0]50 support_vectors = X[support_vector_indices]51 52 ax.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=plt.cm.Paired)53 DecisionBoundaryDisplay.from_estimator(54 clf,55 X,56 ax=ax,57 grid_resolution=50,58 plot_method="contour",59 colors="k",60 levels=[-1, 0, 1],61 alpha=0.5,62 linestyles=["--", "-", "--"],63 )64 ax.scatter(65 support_vectors[:, 0],66 support_vectors[:, 1],67 s=100,68 linewidth=1,69 facecolors="none",70 edgecolors="k",71 )72 ax.set_title("C=" + str(C))73 74 return fig75 76def iter_grid(n_rows, n_cols):77 # create a grid using gradio Block78 for _ in range(n_rows):79 with gr.Row():80 for _ in range(n_cols):81 with gr.Column():82 yield83 84title = "📈 Linear Support Vector Classification"85with gr.Blocks(title=title) as demo:86 gr.Markdown(f"## {title}")87 gr.Markdown("The LinearSVC is an implementation of a \88 Support Vector Machine (SVM) for classification. \89 It aims to find the optimal linear \90 decision boundary that separates classes in the input data.")91 gr.Markdown("The most important parameters of `LinearSVC` are:")92 param_C = "\93 1. `C`: The inverse of the regularization strength. \94 A smaller `C` value increases the amount of regularization, \95 promoting simpler models, while a larger `C` value reduces \96 regularization, allowing more complex models. \97 It controls the trade-off between fitting the \98 training data and generalization to unseen data."99 param_loss=" \100 2. `loss`: The loss function used for training. \101 The default is `squared_hinge`, which is a variant \102 of hinge loss. The combination of penalty='l1' and \103 loss='hinge' is not supported."104 param_penalty="\105 3. `penalty`: The type of regularization penalty \106 applied to the model. The default is `l2`, which uses \107 the L2 norm."108 param_dual="\109 4. `dual`: Determines whether the dual or primal optimization \110 problem is solved. By default, `dual=True` when the number \111 of samples is less than the number of features, and `dual=False` \112 otherwise. For large-scale problems, setting `dual=False` \113 can be more efficient."114 param_tol="\115 5. `tol`: The tolerance for stopping criteria. \116 The solver stops when the optimization reaches \117 a specified tolerance level."118 param_max_iter="\119 6. `max_iter`: The maximum number of iterations for solver \120 convergence. If not specified, the default value is 1000."121 gr.Markdown(param_C)122 gr.Markdown(param_loss)123 gr.Markdown(param_penalty)124 gr.Markdown(param_dual)125 gr.Markdown(param_tol)126 gr.Markdown(param_max_iter)127 gr.Markdown("Read more in the \128 [original example](https://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html#sklearn.svm.LinearSVC).")129 130 131 n_samples = gr.Slider(minimum=20, maximum=100, step=5, 132 label = "Number of Samples")133 134 135 with gr.Row():136 input_model = "LinearSVC"137 fn = partial(train_model)138 139 with gr.Row():140 penalty = gr.Dropdown(["l1", "l2"], value="l2", interactive=True, label="Penalty to prevent overfitting")141 loss = gr.Dropdown(["hinge", "squared hinge"], value="hinge", interactive=True, label="Loss function")142 143 with gr.Row():144 max_iter = gr.Slider(minimum=100, maximum=2000, step=100, value=1000, 145 label = "Max. number of iterations")146 param_C = gr.Number(value=1,147 label = "Regularization parameter C", 148 # info="When C is smal the regularization effect is stronger. " 149 # + "This can help to avoid overfitting but may lead to higher bias. "150 # + "On the other hand, when C is large, the regularization effect "151 # + "is weaker, and the model can have larger parameter values, "152 # + "allowing for more complex decision boundaries that fit the "153 # + "training data more closely. This may increase the risk of "154 # + "overfitting and result in a higher variance model."155 )156 157 with gr.Row():158 penalty2 = gr.Dropdown(["l1", "l2"], value="l2", interactive=True, label="Penalty to prevent overfitting")159 loss2 = gr.Dropdown(["hinge", "squared hinge"], value="hinge", interactive=True, label="Loss function")160 161 with gr.Row():162 max_iter2 = gr.Slider(minimum=100, maximum=2000, step=100, value=1000, 163 label = "Max. number of iterations")164 param_C2 = gr.Number(value=100,165 label = "Regularization parameter C"166 )167 168 with gr.Row():169 plot = gr.Plot(label=input_model)170 n_samples.change(fn=fn, inputs=[n_samples, param_C, penalty, loss, max_iter], outputs=plot)171 param_C.change(fn=fn, inputs=[n_samples, param_C, penalty, loss, max_iter], outputs=plot)172 penalty.change(fn=fn, inputs=[n_samples, param_C, penalty, loss, max_iter], outputs=plot)173 loss.change(fn=fn, inputs=[n_samples, param_C, penalty, loss, max_iter], outputs=plot)174 max_iter.change(fn=fn, inputs=[n_samples, param_C, penalty, loss, max_iter], outputs=plot)175 176 plot2 = gr.Plot(label=input_model)177 n_samples.change(fn=fn, inputs=[n_samples, param_C2, penalty2, loss2, max_iter2], outputs=plot2)178 param_C2.change(fn=fn, inputs=[n_samples, param_C2, penalty2, loss2, max_iter2], outputs=plot2)179 penalty2.change(fn=fn, inputs=[n_samples, param_C2, penalty2, loss2, max_iter2], outputs=plot2)180 loss2.change(fn=fn, inputs=[n_samples, param_C2, penalty2, loss2, max_iter2], outputs=plot2)181 max_iter2.change(fn=fn, inputs=[n_samples, param_C2, penalty2, loss2, max_iter2], outputs=plot2)182 183demo.launch()