File size: 4,752 Bytes
c07df4a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from itertools import combinations
plt.rcParams['figure.dpi'] = 100
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.ensemble import VotingClassifier
import gradio as gr
#==================================================
C1, C2, C3 = '#ff0000', '#ffff00', '#0000ff'
CMAP = ListedColormap([C1, C2, C3])
GRANULARITY = 0.05
SEED = 1
FEATURE_NAMES = ["Sepal Length", "Sepal Width", "Petal Length", "Petal Width"]
TARGET_NAMES = ["Setosa", "Versicolour", "Virginica"]
MODEL_NAMES = ['DecisionTreeClassifier', 'KNeighborsClassifier', 'SupportVectorClassifier', 'VotingClassifier']
iris = load_iris()
#==================================================
def get_decision_surface(X, y, model):
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xrange = np.arange(x_min, x_max, GRANULARITY)
yrange = np.arange(y_min, y_max, GRANULARITY)
xx, yy = np.meshgrid(xrange, yrange)
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
return xx, yy, Z
def create_plot(feature_string, max_depth, n_neighbors, gamma, weight1, weight2, weight3):
np.random.seed(SEED)
feature_list = feature_string.split(',')
feature_list = [s.strip() for s in feature_list]
idx_x = FEATURE_NAMES.index(feature_list[0])
idx_y = FEATURE_NAMES.index(feature_list[1])
X = iris.data[:, [idx_x, idx_y]]
y = iris.target
rnd_idx = np.random.permutation(X.shape[0])
X = X[rnd_idx]
y = y[rnd_idx]
clf1 = DecisionTreeClassifier(max_depth=max_depth)
clf2 = KNeighborsClassifier(n_neighbors=n_neighbors, n_jobs=-1)
clf3 = SVC(gamma=gamma, kernel="rbf", probability=True)
eclf = VotingClassifier(
estimators=[("dt", clf1), ("knn", clf2), ("svc", clf3)],
voting="soft",
weights=[weight1, weight2, weight3],
)
clf1.fit(X, y)
clf2.fit(X, y)
clf3.fit(X, y)
eclf.fit(X, y)
fig = plt.figure(figsize=(12, 12))
for i, clf in enumerate([clf1, clf2, clf3, eclf]):
xx, yy, Z = get_decision_surface(X, y, clf)
ax = fig.add_subplot(2, 2, i+1)
ax.contourf(xx, yy, Z, cmap=CMAP, alpha=0.65)
for j, label in enumerate(TARGET_NAMES):
X_label = X[y==j,:]
y_label = y[y==j]
ax.scatter(X_label[:, 0], X_label[:, 1], c=[[C1], [C2], [C3]][j]*len(y_label), edgecolor='k', s=40, label=label)
ax.set_xlabel(feature_list[0]); ax.set_ylabel(feature_list[1])
ax.legend()
ax.set_title(f'{MODEL_NAMES[i]}')
return fig
info = '''
# Voting Classifier Decision Surface
This app plots the decision surface of four classifiers on two selected features of the Iris dataset:
- DecisionTreeClassifier.
- KNeighborsClassifier.
- SupportVectorClassifier.
- A VotingClassifier from all of the above.
Use 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.
'''
with gr.Blocks() as demo:
gr.Markdown(info)
selections = combinations(FEATURE_NAMES, 2)
selections = [f'{s[0]}, {s[1]}' for s in selections]
dd = gr.Dropdown(selections, value=selections[0], interactive=True, label="Input features")
with gr.Row():
with gr.Column():
slider_max_depth = gr.Slider(1, 50, value=4, step=1, label='max_depth (for DecisionTreeClassifier)')
slider_n_neighbors = gr.Slider(1, 20, value=7, step=1, label='n_neighbors (for KNeighborsClassifier)')
slider_gamma = gr.Slider(0, 10, value=0.1, step=0.1, label='gamma (for SVC)')
with gr.Column():
slider_w1 = gr.Slider(0, 10, value=2, step=0.1, label='DecisionTreeClassifier weight')
slider_w2 = gr.Slider(0, 10, value=1, step=0.1, label='KNeighborsClassifier weight')
slider_w3 = gr.Slider(0, 10, value=2, step=0.1, label='SVC weight')
btn = gr.Button(value='Submit')
plot = gr.Plot(label='Decision Surfaces')
btn.click(create_plot, inputs=[dd, slider_max_depth, slider_n_neighbors, slider_gamma, slider_w1, slider_w2, slider_w3], outputs=[plot])
demo.load(create_plot, inputs=[dd, slider_max_depth, slider_n_neighbors, slider_gamma, slider_w1, slider_w2, slider_w3], outputs=[plot])
demo.launch()
#================================================== |