在 Scikit 的示例中更改颜色以绘制 VotingClassifier 的决策边界?
Changing color in Scikit's example for plotting decision boundaries of a VotingClassifier?
您好,我正在尝试重现 Scikit's example for plotting decision boundaries Voting Classifiers。
分类部分相当直接,在一张图中绘制多个情节的巧妙方式令人着迷。但是,我无法更改配色方案。
这是直接的分类部分:
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)
该示例使用以下代码创建图形:
# 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()
matplotlib 似乎以某种方式使用了默认的着色方案。有没有办法传递其他颜色?我尝试用 c=y
(例如 c = ['y', 'b']
)来 fiddle,但这并不能解决问题。
我想改变背景颜色和散点颜色。有什么想法吗?
颜色是根据 y
和 Z
中的值选择的。 y
的条目数与点数一样多,并且有 3 个唯一值。 Z
也有 3 个级别。它们根据 matplotlib 进行颜色映射
colormaps.
您可以选择不同的颜色图,例如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')
完整代码:
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()
您还可以创建自定义颜色图。例如。使用金色、深红色和靛蓝色作为颜色,
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')
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()
您好,我正在尝试重现 Scikit's example for plotting decision boundaries Voting Classifiers。
分类部分相当直接,在一张图中绘制多个情节的巧妙方式令人着迷。但是,我无法更改配色方案。
这是直接的分类部分:
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)
该示例使用以下代码创建图形:
# 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()
matplotlib 似乎以某种方式使用了默认的着色方案。有没有办法传递其他颜色?我尝试用 c=y
(例如 c = ['y', 'b']
)来 fiddle,但这并不能解决问题。
我想改变背景颜色和散点颜色。有什么想法吗?
颜色是根据 y
和 Z
中的值选择的。 y
的条目数与点数一样多,并且有 3 个唯一值。 Z
也有 3 个级别。它们根据 matplotlib 进行颜色映射
colormaps.
您可以选择不同的颜色图,例如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')
完整代码:
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()
您还可以创建自定义颜色图。例如。使用金色、深红色和靛蓝色作为颜色,
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')
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()