-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclassifier_libsvm.py
56 lines (43 loc) · 1.56 KB
/
classifier_libsvm.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
56
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
"""
Using Classifier and LIBSVM file
===================================================
In this example we show how to handle LIBSVM file format.
"""
from sklearn.datasets import load_svmlight_files
import sklearn.metrics
import jubakit
from jubakit.classifier import Classifier, Dataset, Config
# Load LIBSVM files.
# Note that these example files are not included in this repository.
# You can fetch them from: https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass.html#news20
print("Loading LIBSVM files...")
(train_X, train_y, test_X, test_y) = load_svmlight_files(['news20', 'news20.t'])
# Create a Train Dataset.
print("Creating train dataset...")
train_ds = Dataset.from_matrix(train_X, train_y)
# Create a Test Dataset
print("Creating test dataset...")
test_ds = Dataset.from_matrix(test_X, test_y)
# Create a Classifier Service
classifier = Classifier.run(Config())
# Train the classifier.
print("Training...")
for (idx, _) in classifier.train(train_ds):
if idx % 1000 == 0:
print("Training... ({0} %)".format(100 * idx / len(train_ds)))
# Test the classifier.
print("Testing...")
y_true = []
y_pred = []
for (idx, label, result) in classifier.classify(test_ds):
y_true.append(label)
y_pred.append(result[0][0])
if idx % 1000 == 0:
print("Testing... ({0} %)".format(100 * idx / len(test_ds)))
# Stop the classifier.
classifier.stop()
# Print the result.
print(sklearn.metrics.classification_report(y_true, y_pred))