Skip to content

Commit 740ebbd

Browse files
committed
保持データで評価
データセットを半分に分割してトレーニングデータとテストデータとして使用する。
1 parent b6c7ffe commit 740ebbd

4 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎ch02/heldout_syakyo.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from matplotlib import pyplot as plt
2+
import numpy as np
3+
from sklearn.datasets import load_iris
4+
from threshold_syakyo import learn_model, apply_model, accuracy
5+
6+
7+
data = load_iris()
8+
features = data['data']
9+
labels = data['target_names'][data['target']]
10+
11+
setosa = (labels == 'setosa')
12+
features = features[~setosa]
13+
labels = labels[~setosa]
14+
virginica = (labels == 'virginica')
15+
print(virginica.shape)
16+
print(virginica)
17+
18+
print(features.shape)
19+
#np.tile()でT,Fの配列を順序よく作る
20+
#testing = np.tile([True, False], int(features.shape[0] / 2))
21+
#flags = np.tile([True, False], int(features.shape[0] / 2))
22+
# 2つの値を50回繰り返して要素100個の配列を作る
23+
flags = np.tile([True, False], int(features.shape[0] / 2))
24+
np.random.shuffle(flags)
25+
testing = flags
26+
training = ~testing
27+
28+
print(testing)
29+
print(training)
30+
31+
print(features.shape)
32+
print(features)
33+
34+
model = learn_model(features[training], virginica[training])
35+
train_error = accuracy(features[training], virginica[training], model)
36+
test_error = accuracy(features[testing], virginica[testing], model)
37+
38+
print('''\
39+
Training error was {0:.1%}.
40+
Testing error was {1:.1%} (N = {2}).
41+
'''.format(train_error, test_error, testing.sum()))
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import numpy as np
32

43
def learn_model(features, labels):

0 commit comments

Comments
 (0)