sklearn-docs/voting-classifier-decision-surface
2
1import numpy as np2import matplotlib.pyplot as plt3from matplotlib.colors import ListedColormap4from itertools import combinations5 6plt.rcParams['figure.dpi'] = 1007 8from sklearn.datasets import load_iris9 10from sklearn.tree import DecisionTreeClassifier11from sklearn.neighbors import KNeighborsClassifier12from sklearn.svm import SVC13from sklearn.ensemble import VotingClassifier14 15import gradio as gr16 17#==================================================18C1, C2, C3 = '#ff0000', '#ffff00', '#0000ff'19CMAP = ListedColormap([C1, C2, C3])20GRANULARITY = 0.0521 22FEATURE_NAMES = ["Sepal Length", "Sepal Width", "Petal Length", "Petal Width"]23TARGET_NAMES = ["Setosa", "Versicolour", "Virginica"]24MODEL_NAMES = ['DecisionTreeClassifier', 'KNeighborsClassifier', 'SupportVectorClassifier', 'VotingClassifier']25 26iris = load_iris()27#==================================================28def get_decision_surface(X, y, model):29 x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 130 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 131 xrange = np.arange(x_min, x_max, GRANULARITY)32 yrange = np.arange(y_min, y_max, GRANULARITY)33 xx, yy = np.meshgrid(xrange, yrange)34 35 Z = model.predict(np.c_[xx.ravel(), yy.ravel()])36 Z = Z.reshape(xx.shape)37 38 return xx, yy, Z39 40def create_plot(feature_string, max_depth, n_neighbors, gamma, weight1, weight2, weight3):41 42 feature_list = feature_string.split(',')43 feature_list = [s.strip() for s in feature_list]44 idx_x = FEATURE_NAMES.index(feature_list[0])45 idx_y = FEATURE_NAMES.index(feature_list[1])46 47 X = iris.data[:, [idx_x, idx_y]]48 y = iris.target49 50 rnd_idx = np.random.permutation(X.shape[0])51 X = X[rnd_idx]52 y = y[rnd_idx]53 54 clf1 = DecisionTreeClassifier(max_depth=max_depth)55 clf2 = KNeighborsClassifier(n_neighbors=n_neighbors, n_jobs=-1)56 clf3 = SVC(gamma=gamma, kernel="rbf", probability=True)57 eclf = VotingClassifier(58 estimators=[("dt", clf1), ("knn", clf2), ("svc", clf3)],59 voting="soft",60 weights=[weight1, weight2, weight3],61 )62 63 clf1.fit(X, y)64 clf2.fit(X, y)65 clf3.fit(X, y)66 eclf.fit(X, y)67 68 fig, _ = plt.subplots(2, 2, figsize=(7, 7), sharex=True, sharey=True)69 70 for i, clf in enumerate([clf1, clf2, clf3, eclf]):71 xx, yy, Z = get_decision_surface(X, y, clf)72 73 ax = fig.add_subplot(2, 2, i+1)74 ax.set_axis_off()75 ax.contourf(xx, yy, Z, cmap=CMAP, alpha=0.65)76 77 for j, label in enumerate(TARGET_NAMES):78 X_label = X[y==j,:]79 y_label = y[y==j]80 ax.scatter(X_label[:, 0], X_label[:, 1], c=[[C1], [C2], [C3]][j]*len(y_label), edgecolor='k', s=40, label=label)81 82 ax.legend()83 ax.set_title(f'{MODEL_NAMES[i]}')84 85 fig.supxlabel(feature_list[0]); fig.supylabel(feature_list[1])86 fig.set_tight_layout(True)87 fig.set_constrained_layout(True)88 return fig89 90info = '''91# Voting Classifier Decision Surface92 93This app plots the decision surface of four classifiers on two selected features of the Iris dataset: DecisionTreeClassifier, KNeighborsClassifier, SupportVectorClassifier, and a VotingClassifier from all of them.94 95Use the controls below to tune the parameters of the classifiers and the weights of each of them in the soft voting classifier and click submit. The more weight you assign to a classifier, the more importance will be assigned to its predictions compared to the other classifiers in the vote.96 97Created by [@huabdul](https://huggingface.co/huabdul) based on [scikit-learn docs](https://scikit-learn.org/stable/auto_examples/ensemble/plot_voting_decision_regions.html).98'''99 100with gr.Blocks(analytics_enabled=False) as demo:101 selections = combinations(FEATURE_NAMES, 2)102 selections = [f'{s[0]}, {s[1]}' for s in selections]103 104 with gr.Row():105 with gr.Column():106 gr.Markdown(info)107 dd = gr.Dropdown(selections, value=selections[0], interactive=True, label="Input features")108 with gr.Row(): 109 with gr.Column(min_width=100):110 slider_max_depth = gr.Slider(1, 50, value=4, step=1, label='max_depth (DecisionTree)')111 slider_n_neighbors = gr.Slider(1, 20, value=7, step=1, label='n_neighbors (KNN)')112 slider_gamma = gr.Slider(0, 10, value=0.1, step=0.1, label='gamma (SVC)')113 with gr.Column(min_width=100):114 slider_w1 = gr.Slider(0, 10, value=2, step=0.1, label='DecisionTreeClassifier weight')115 slider_w2 = gr.Slider(0, 10, value=1, step=0.1, label='KNeighborsClassifier weight')116 slider_w3 = gr.Slider(0, 10, value=2, step=0.1, label='SVC weight')117 118 btn = gr.Button(value='Submit')119 120 with gr.Column():121 plot = gr.Plot(show_label=False)122 123 btn.click(create_plot, inputs=[dd, slider_max_depth, slider_n_neighbors, slider_gamma, slider_w1, slider_w2, slider_w3], outputs=[plot])124 demo.load(create_plot, inputs=[dd, slider_max_depth, slider_n_neighbors, slider_gamma, slider_w1, slider_w2, slider_w3], outputs=[plot])125 126demo.launch()127#==================================================