Created
February 19, 2019 00:20
-
-
Save a-mitani/70ab5201675d6b6ad2711021e33759ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 決定境界の可視化 | |
import matplotlib.pyplot as plt | |
# Parameters for plot | |
n_classes = 2 | |
plot_colors = "br" | |
plot_step = 0.05 | |
#グラフ描画時の説明変数 x、yの最大値&最小値を算出。 | |
#グラフ描画のメッシュを定義 | |
x_min, x_max = data_array[:, 0].min() - 1, data_array[:, 0].max() + 1 | |
y_min, y_max = data_array[:, 1].min() - 1, data_array[:, 1].max() + 1 | |
xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step), | |
np.arange(y_min, y_max, plot_step)) | |
#各メッシュ上での決定木による分類を計算 | |
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) | |
Z = Z.reshape(xx.shape) | |
#決定木による分類を等高線フィールドプロットでプロット | |
cs = plt.contour(xx, yy, Z, cmap=plt.cm.Paired) | |
plt.xlabel('x') | |
plt.ylabel('y') | |
plt.axis("tight") | |
#教師データも重ねてプロット | |
for i, color in zip(range(n_classes), plot_colors): | |
idx = np.where(class_array == i) | |
plt.scatter(data_array[idx, 0], data_array[idx, 1], c=color, label=['a','b'], | |
cmap=plt.cm.Paired) | |
plt.axis("tight") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment