-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclassifier_digits.py
51 lines (38 loc) · 1.35 KB
/
classifier_digits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
"""
Using Classifier and Digits dataset
===================================================
In this example we show classification using Digits dataset.
"""
import sklearn.datasets
import sklearn.metrics
import jubakit
from jubakit.classifier import Classifier, Dataset, Config
# Load the digits dataset.
digits = sklearn.datasets.load_digits()
# Create a Dataset.
dataset = Dataset.from_array(digits.data, digits.target)
n_samples = len(dataset)
n_train_samples = int(n_samples / 2)
# Create a Classifier Service
cfg = Config(method='AROW', parameter={'regularization_weight': 0.1})
classifier = Classifier.run(cfg)
print("Started Service: {0}".format(classifier))
# Train the classifier using the first half of the dataset.
train_ds = dataset[:n_train_samples]
print("Training...: {0}".format(train_ds))
for _ in classifier.train(train_ds): pass
# Test the classifier using the last half of the dataset.
test_ds = dataset[n_train_samples:]
print("Testing...: {0}".format(test_ds))
y_true = []
y_pred = []
for (idx, label, result) in classifier.classify(test_ds):
y_true.append(label)
y_pred.append(result[0][0])
# Stop the classifier.
classifier.stop()
# Print the result.
print(sklearn.metrics.classification_report(y_true, y_pred))