Skip to content

Commit 411713b

Browse files
committed
花弁の長さを元に分類する
1つ前のデータプロットで花弁の長さを使えばvirginicaとversicolorを9割ほど分類できることがわかったので 境界線を引いて分類してみる。
1 parent c089d23 commit 411713b

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

‎ch02/figure2_copy.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
 (0)