0

Hi I am trying to reproduce Scikit's example for plotting decision boundaries Voting Classifiers.

The classification part is rather straight forward, and the neat way of plotting several plots in a single figure is intruiging. However, I have trouble with altering the coloring scheme.

This is the straight forward classification part:

from itertools import product
import numpy as np
import matplotlib.pyplot as plt

from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.ensemble import VotingClassifier

# Loading some example data
iris = datasets.load_iris()
X = iris.data[:, [0, 2]]
y = iris.target

# Training classifiers
clf1 = DecisionTreeClassifier(max_depth=4)
clf2 = KNeighborsClassifier(n_neighbors=7)
clf3 = SVC(kernel='rbf', probability=True)
eclf = VotingClassifier(estimators=[('dt', clf1), ('knn', clf2),
                                    ('svc', clf3)],
                        voting='soft', weights=[2, 1, 2])

clf1.fit(X, y)
clf2.fit(X, y)
clf3.fit(X, y)
eclf.fit(X, y)

The example uses the following code to create the figure:

# Plotting decision regions
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.arange(x_min, x_max, 0.1),
                     np.arange(y_min, y_max, 0.1))

f, axarr = plt.subplots(2, 2, sharex='col', sharey='row', figsize=(10, 8))

for idx, clf, tt in zip(product([0, 1], [0, 1]),
                        [clf1, clf2, clf3, eclf],
                        ['Decision Tree (depth=4)', 'KNN (k=7)',
                         'Kernel SVM', 'Soft Voting']):

    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)

    axarr[idx[0], idx[1]].contourf(xx, yy, Z, alpha=0.4)
    axarr[idx[0], idx[1]].scatter(X[:, 0], X[:, 1], c=y,
                                  s=20, edgecolor='k')
    axarr[idx[0], idx[1]].set_title(tt)

plt.show()

It seems that matplotlib somehow uses a default coloring scheme. Is there a way to pass other colors? I tried to fiddle arround with c=y (e.g c = ['y', 'b']) but that does not do the trick.

I would like to alter both the background coloring and the scatter coloring. Any ideas?

Rachel
  • 1,937
  • 7
  • 31
  • 58
  • 2
    Using `edgecolor='w'` gives you white edges, as [here](https://i.stack.imgur.com/n1N2i.png). Since the code is not runnable (missing variables and a lot of useless slashes (`\`) all over the place), I don't know how else to help. – ImportanceOfBeingErnest Sep 13 '17 at 15:32
  • Thank you for not down voting the question. I juts c/p my code and somehow it was scattered with slashes and alike. I cleaned it and used the original example. It is now fully reproducable. – Rachel Sep 13 '17 at 15:45

1 Answers1

2

The colors are chosen according to the values in y and Z for the respective plots. y has as many entries as there are points and it has 3 unique values. Z has 3 levels as well. They are colormapped according to matplotlib colormaps.

You may choose a different colormap, e.g. cmap="brg":

axarr[idx].contourf(xx, yy, Z, alpha=0.4, cmap="brg")
axarr[idx].scatter(X[:, 0], X[:, 1], c=y, cmap="brg",
                              s=20, edgecolor='w')

enter image description here

Complete code:

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier

iris = datasets.load_iris()
X = iris.data[:, [0, 2]]
y = iris.target

clf1 = DecisionTreeClassifier(max_depth=4)
clf2 = KNeighborsClassifier(n_neighbors=7)

clf1.fit(X, y)
clf2.fit(X, y)

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.arange(x_min, x_max, 0.1),
                 np.arange(y_min, y_max, 0.1))

f, axarr = plt.subplots(1,2, sharex='col', sharey='row', figsize=(5,3))

for idx, clf, tt in zip([0, 1],[clf1, clf2],
                    ['Decision Tree (depth=4)', 'KNN (k=7)']):

Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

axarr[idx].contourf(xx, yy, Z, alpha=0.4, cmap="brg")
axarr[idx].scatter(X[:, 0], X[:, 1], c=y, cmap="brg",
                              s=20, edgecolor='w')
axarr[idx].set_title(tt)

plt.show()

You may also create your custom colormap. E.g. to use gold, crimson and indigo as colors,

import matplotlib.colors
cmap = matplotlib.colors.ListedColormap(["gold", "crimson", "indigo"])

axarr[idx].contourf(xx, yy, Z, alpha=0.4, cmap=cmap)
axarr[idx].scatter(X[:, 0], X[:, 1], c=y, cmap=cmap,
                              s=20, edgecolor='w')

enter image description here

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier

iris = datasets.load_iris()
X = iris.data[:, [0, 2]]
y = iris.target

clf1 = DecisionTreeClassifier(max_depth=4)
clf2 = KNeighborsClassifier(n_neighbors=7)

clf1.fit(X, y)
clf2.fit(X, y)

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.arange(x_min, x_max, 0.1),
                 np.arange(y_min, y_max, 0.1))

f, axarr = plt.subplots(1,2, sharex='col', sharey='row', figsize=(5,3))

cmap = matplotlib.colors.ListedColormap(["gold", "crimson", "indigo"])

for idx, clf, tt in zip([0, 1],[clf1, clf2],
                    ['Decision Tree (depth=4)', 'KNN (k=7)']):

Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

axarr[idx].contourf(xx, yy, Z, alpha=0.4, cmap=cmap)
axarr[idx].scatter(X[:, 0], X[:, 1], c=y, cmap=cmap,
                              s=20, edgecolor='w')
axarr[idx].set_title(tt)

plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712