print classifier.get_config()
    print classifier.get_status()


    for line in open('train.dat'):
        label, file = line[:-1].split(',')
        dat = open(file).read()
        datum = Datum({"message": dat})
        classifier.train([LabeledDatum(label, datum)])

    print classifier.get_status()

    print classifier.save("tutorial")

    print classifier.load("tutorial")

    print classifier.get_config()

    count_ok = 0
    count_ng = 0
    for line in open('test.dat'):
        label, file = line[:-1].split(',')
        dat = open(file).read()        
        datum = Datum({"message": dat})
        ans = classifier.classify([datum])
        if ans != None:
            estm = get_most_likely(ans[0])
            if (label == estm[0]):
                result = "OK"
                count_ok += 1
Exemplo n.º 2
0
    print(classifier.get_status())

    train_list = cross_validation_python()
    data_train, data_test, label_train, label_test = train_test_split(train_list[0], train_list[1])

    for label, dat in izip(label_train, data_train):
        print(dat[0])
        data_dict = json.loads(dat[0])
        datum = Datum(data_dict)
        classifier.train([LabeledDatum(label, datum)])

    print(classifier.get_status())

    print(classifier.save("tutorial"))

    print(classifier.load("tutorial"))

    print(classifier.get_config())

    count_ok = 0
    count_ng = 0
    for label, dat in izip(label_test, data_test):
        data_dict = json.loads(dat[0])
        datum = Datum(data_dict)
        ans = classifier.classify([datum])
        if ans != None:
            estm = get_most_likely(ans[0])
            if (label == estm[0]):
                result = "OK"
                count_ok += 1
            else:
Exemplo n.º 3
0
class ClassifierTest(unittest.TestCase):
    def setUp(self):
        self.config = {
            "method": "AROW",
            "converter": {
                "string_filter_types": {},
                "string_filter_rules": [],
                "num_filter_types": {},
                "num_filter_rules": [],
                "string_types": {},
                "string_rules": [{"key": "*", "type": "str", "sample_weight": "bin", "global_weight": "bin"}],
                "num_types": {},
                "num_rules": [{"key": "*", "type": "num"}],
            },
            "parameter": {"regularization_weight": 1.001},
        }

        TestUtil.write_file("config_classifier.json", json.dumps(self.config))
        self.srv = TestUtil.fork_process("classifier", port, "config_classifier.json")
        try:
            self.cli = Classifier(host, port, "name")
        except:
            TestUtil.kill_process(self.srv)
            raise

    def tearDown(self):
        if self.cli:
            self.cli.get_client().close()
        TestUtil.kill_process(self.srv)

    def test_get_client(self):
        self.assertTrue(isinstance(self.cli.get_client(), msgpackrpc.client.Client))

    def test_get_config(self):
        config = self.cli.get_config()
        self.assertEqual(json.dumps(json.loads(config), sort_keys=True), json.dumps(self.config, sort_keys=True))

    def test_train(self):
        d = Datum({"skey1": "val1", "skey2": "val2", "nkey1": 1.0, "nkey2": 2.0})
        data = [["label", d]]
        self.assertEqual(self.cli.train(data), 1)

    def test_classify(self):
        d = Datum({"skey1": "val1", "skey2": "val2", "nkey1": 1.0, "nkey2": 2.0})
        data = [d]
        result = self.cli.classify(data)

    def test_set_label(self):
        self.assertEqual(self.cli.set_label("label"), True)

    def test_get_labels(self):
        self.cli.set_label("label")
        self.assertEqual(self.cli.get_labels(), {"label": 0})

    def test_delete_label(self):
        self.cli.set_label("label")
        self.assertEqual(self.cli.delete_label("label"), True)

    def test_save(self):
        self.assertEqual(len(self.cli.save("classifier.save_test.model")), 1)

    def test_load(self):
        model_name = "classifier.load_test.model"
        self.cli.save(model_name)
        self.assertEqual(self.cli.load(model_name), True)

    def test_get_status(self):
        self.cli.get_status()

    def test_str(self):
        self.assertEqual("estimate_result{label: label, score: 1.0}", str(EstimateResult("label", 1.0)))
import argparse
import socket
from jubatus.classifier.client import Classifier

parser = argparse.ArgumentParser()
parser.add_argument("-n", "--name", help="set the name of the file to load")
parser.add_argument("--host", help="set the host address")
parser.add_argument("--port", help="set the port number")

args = parser.parse_args()
print(args)
host_ip = args.host if args.host else socket.gethostbyname(
    socket.gethostname())
port = args.port if args.port else 9199

client = Classifier(host_ip, port, "")

if args.name:
    print(args.name)
    client.load(args.name)
    print("model " + args.name + " has been loaded")
else:
    print("[Error] specify the model's name to be loaded!")
Exemplo n.º 5
0
                            options.name, 10.0)

    print classifier.get_config()
    print classifier.get_status()

    for line in open('train.dat'):
        label, file = line[:-1].split(',')
        dat = open(file).read()
        datum = Datum({"message": dat})
        classifier.train([LabeledDatum(label, datum)])

    print classifier.get_status()

    print classifier.save("tutorial")

    print classifier.load("tutorial")

    print classifier.get_config()

    count_ok = 0
    count_ng = 0
    for line in open('test.dat'):
        label, file = line[:-1].split(',')
        dat = open(file).read()
        datum = Datum({"message": dat})
        ans = classifier.classify([datum])
        if ans != None:
            estm = get_most_likely(ans[0])
            if (label == estm[0]):
                result = "OK"
                count_ok += 1
Exemplo n.º 6
0
class ClassifierTest(unittest.TestCase):
    def setUp(self):
        self.config = {
            "method": "AROW",
            "converter": {
                "string_filter_types": {},
                "string_filter_rules": [],
                "num_filter_types": {},
                "num_filter_rules": [],
                "string_types": {},
                "string_rules": [{
                    "key": "*",
                    "type": "str",
                    "sample_weight": "bin",
                    "global_weight": "bin"
                }],
                "num_types": {},
                "num_rules": [{
                    "key": "*",
                    "type": "num"
                }]
            },
            "parameter": {
                "regularization_weight": 1.001
            }
        }

        TestUtil.write_file('config_classifier.json', json.dumps(self.config))
        self.srv = TestUtil.fork_process('classifier', port,
                                         'config_classifier.json')
        try:
            self.cli = Classifier(host, port, "name")
        except:
            TestUtil.kill_process(self.srv)
            raise

    def tearDown(self):
        if self.cli:
            self.cli.get_client().close()
        TestUtil.kill_process(self.srv)

    def test_get_client(self):
        self.assertTrue(
            isinstance(self.cli.get_client(), msgpackrpc.client.Client))

    def test_get_config(self):
        config = self.cli.get_config()
        self.assertEqual(json.dumps(json.loads(config), sort_keys=True),
                         json.dumps(self.config, sort_keys=True))

    def test_train(self):
        d = Datum({
            "skey1": "val1",
            "skey2": "val2",
            "nkey1": 1.0,
            "nkey2": 2.0
        })
        data = [["label", d]]
        self.assertEqual(self.cli.train(data), 1)

    def test_classify(self):
        d = Datum({
            "skey1": "val1",
            "skey2": "val2",
            "nkey1": 1.0,
            "nkey2": 2.0
        })
        data = [d]
        result = self.cli.classify(data)

    def test_set_label(self):
        self.assertEqual(self.cli.set_label("label"), True)

    def test_get_labels(self):
        self.cli.set_label("label")
        self.assertEqual(self.cli.get_labels(), {"label": 0})

    def test_delete_label(self):
        self.cli.set_label("label")
        self.assertEqual(self.cli.delete_label("label"), True)

    def test_save(self):
        self.assertEqual(len(self.cli.save("classifier.save_test.model")), 1)

    def test_load(self):
        model_name = "classifier.load_test.model"
        self.cli.save(model_name)
        self.assertEqual(self.cli.load(model_name), True)

    def test_get_status(self):
        self.cli.get_status()

    def test_str(self):
        self.assertEqual("estimate_result{label: label, score: 1.0}",
                         str(EstimateResult("label", 1.0)))