|
14 | 14 | feature_names = data['feature_names'] |
15 | 15 | species = data['target_names'][data['target']] |
16 | 16 |
|
17 | | -setosa = (species == 'setosa') |
18 | | -features = features[~setosa] |
19 | | -species = species[~setosa] |
20 | | -virginica = species == 'virginica' |
| 17 | +is_setosa = (species == 'setosa') |
| 18 | +features = features[~is_setosa] |
| 19 | +species = species[~is_setosa] |
| 20 | +is_virginica = (species == 'virginica') |
21 | 21 |
|
| 22 | +# Hand fixed threshold: |
22 | 23 | t = 1.75 |
23 | | -p0, p1 = 3, 2 |
| 24 | + |
| 25 | +# Features to use: 3 & 2 |
| 26 | +f0, f1 = 3, 2 |
24 | 27 |
|
25 | 28 | if COLOUR_FIGURE: |
26 | 29 | area1c = (1., .8, .8) |
|
29 | 32 | area1c = (1., 1, 1) |
30 | 33 | area2c = (.7, .7, .7) |
31 | 34 |
|
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 | | -plt.fill_between([t, x1], [y0, y0], [y1, y1], color=area2c) |
36 | | -plt.fill_between([x0, t], [y0, y0], [y1, y1], color=area1c) |
37 | | -plt.plot([t, t], [y0, y1], 'k--', lw=2) |
38 | | -plt.plot([t - .1, t - .1], [y0, y1], 'k:', lw=2) |
39 | | -plt.scatter(features[virginica, p0], |
40 | | - features[virginica, p1], c='b', marker='o') |
41 | | -plt.scatter(features[~virginica, p0], |
42 | | - features[~virginica, p1], c='r', marker='x') |
43 | | -plt.ylim(y0, y1) |
44 | | -plt.xlim(x0, x1) |
45 | | -plt.xlabel(feature_names[p0]) |
46 | | -plt.ylabel(feature_names[p1]) |
47 | | -plt.savefig('figure2.png') |
| 35 | +# Plot from 90% of smallest value to 110% of largest value |
| 36 | +# (all feature values are positive, otherwise this would not work very well) |
| 37 | + |
| 38 | +x0 = features[:, f0].min() * .9 |
| 39 | +x1 = features[:, f0].max() * 1.1 |
| 40 | + |
| 41 | +y0 = features[:, f1].min() * .9 |
| 42 | +y1 = features[:, f1].max() * 1.1 |
| 43 | + |
| 44 | +fig,ax = plt.subplots() |
| 45 | +ax.fill_between([t, x1], [y0, y0], [y1, y1], color=area2c) |
| 46 | +ax.fill_between([x0, t], [y0, y0], [y1, y1], color=area1c) |
| 47 | +ax.plot([t, t], [y0, y1], 'k--', lw=2) |
| 48 | +ax.plot([t - .1, t - .1], [y0, y1], 'k:', lw=2) |
| 49 | +ax.scatter(features[is_virginica, f0], |
| 50 | + features[is_virginica, f1], c='b', marker='o') |
| 51 | +ax.scatter(features[~is_virginica, f0], |
| 52 | + features[~is_virginica, f1], c='r', marker='x') |
| 53 | +ax.set_ylim(y0, y1) |
| 54 | +ax.set_xlim(x0, x1) |
| 55 | +ax.set_xlabel(feature_names[f0]) |
| 56 | +ax.set_ylabel(feature_names[f1]) |
| 57 | +fig.tight_layout() |
| 58 | +fig.savefig('figure2.png') |
0 commit comments