-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclassifier_shogun.py
55 lines (44 loc) · 1.48 KB
/
classifier_shogun.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
52
53
54
55
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
"""
Using Classifier and String Features
========================================
This is a famous `shogun` classifier example that predicts family name
of Shogun from his first name.
"""
from jubakit.classifier import Classifier, Schema, Dataset, Config
from jubakit.loader.csv import CSVLoader
# Load the shogun dataset.
train_loader = CSVLoader('shogun.train.csv')
test_loader = CSVLoader('shogun.test.csv')
# Define a Schema that defines types for each columns of the CSV file.
schema = Schema({
'family_name': Schema.LABEL,
'first_name': Schema.STRING,
})
# Create a Dataset.
train_dataset = Dataset(train_loader, schema).shuffle()
test_dataset = Dataset(test_loader, schema)
# Create a Classifier Service.
cfg = Config(
method = 'PA',
converter = {
'string_rules': [{'key': 'first_name', 'type': 'unigram', 'sample_weight': 'bin', 'global_weight': 'bin'}]
}
)
classifier = Classifier.run(cfg)
# Train the classifier.
for _ in classifier.train(train_dataset): pass
# Classify using the classifier.
for (idx, label, result) in classifier.classify(test_dataset):
true_family_name = label
pred_family_name = result[0][0]
first_name = test_dataset.get(idx)['first_name']
print("{0} {1} ({2})".format(
pred_family_name,
first_name,
'correct!' if pred_family_name == true_family_name else 'incorrect'
))
# Stop the classifier.
classifier.stop()