|
| 1 | +COLOR_FIGURE = True |
| 2 | + |
| 3 | +from matplotlib import pyplot as plt |
| 4 | +from sklearn.datasets import load_iris |
| 5 | + |
| 6 | +data = load_iris() |
| 7 | +features = data['data'] |
| 8 | +feature_names = data['feature_names'] |
| 9 | + |
| 10 | +species = data['target_names'][data['target']] |
| 11 | +setosa = (species == 'setosa') |
| 12 | + |
| 13 | +# virginicaとversicolorの2つを比較するためのsetosaを除外 |
| 14 | +features = features[~setosa] |
| 15 | +species = species[~setosa] |
| 16 | +virginica = species == 'virginica' |
| 17 | + |
| 18 | +# petal-widthの境界値 |
| 19 | +t = 1.75 |
| 20 | + |
| 21 | +# 3:petal-width, 2:petal-length |
| 22 | +p0, p1 = 3, 2 |
| 23 | + |
| 24 | +if COLOR_FIGURE: |
| 25 | + area1c = (1., .8, .8) |
| 26 | + area2c = (.8, .8, 1.) |
| 27 | +else: |
| 28 | + area1c = (1., 1, 1) |
| 29 | + area2c = (.7, .7, .7) |
| 30 | + |
| 31 | +# petal width,length の最大小値を取得 |
| 32 | +x0, x1 = ([features[:, p0].min() * .9, features[:, p0].max() * 1.1]) |
| 33 | +y0, y1 = ([features[:, p1].min() * .9, features[:, p1].max() * 1.1]) |
| 34 | + |
| 35 | +# 境界値で領域に色を付ける |
| 36 | +plt.fill_between([t, x1], [y0, y0], [y1, y1], color=area2c) |
| 37 | +plt.fill_between([x0, t], [y0, y0], [y1, y1], color=area1c) |
| 38 | + |
| 39 | +plt.plot([t, t], [y0, y1], 'k--', lw=2) |
| 40 | +plt.plot([t - .1, t - .1], [y0, y1], 'k:', lw=2) |
| 41 | +plt.scatter(features[virginica, p0], features[virginica, p1], c='b', marker='o') |
| 42 | +plt.scatter(features[~virginica, p0], features[~virginica, p1], c='r', marker='x') |
| 43 | + |
| 44 | +plt.ylim(y0, y1) |
| 45 | +plt.xlim(x0, x1) |
| 46 | +plt.xlabel(feature_names[p0]) |
| 47 | +plt.ylabel(feature_names[p1]) |
| 48 | +plt.savefig('../1400_02_02.png') |
| 49 | + |
0 commit comments