|
| 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())) |
0 commit comments