Esempio n. 1
0
def test_train():
    model = SVM()
    data_filepath = os.path.dirname(__file__) + "/data/test-data.tsv"
    labels_filepath = os.path.dirname(__file__) + "/data/test-data-label.tsv"
    data = model.load_tsv_file(data_filepath)
    labels = model.load_tsv_file(labels_filepath)
    clf = model.train(data, labels)
Esempio n. 2
0
def test_load_delimited_file():
    model = SVM()
    filepath = os.path.dirname(__file__) + "/data/test-data.tsv"
    data = model.load_delimited_file(filepath, "\t")
    expected = [[0, 9, 0], [2, 8, 0], [2, 3, 2], [0, 9, 1], [2, 1, 7],
                [1, 1, 7], [0, 3, 6], [4, 1, 5], [2, 1, 7], [0, 2, 1]]
    assert_equal(data.tolist(), expected)
Esempio n. 3
0
def test_dump_model():
    model = SVM()
    data_filepath = os.path.dirname(__file__) + "/data/test-data.tsv"
    labels_filepath = os.path.dirname(__file__) + "/data/test-data-label.tsv"
    data = model.load_tsv_file(data_filepath)
    labels = model.load_tsv_file(labels_filepath)
    clf = model.train(data, labels)
    pickled_filepath = os.path.dirname(__file__) + "/data/trained-svm.pickle"
    pickled_string = model.dump_model(pickled_filepath)
    assert (isinstance(pickled_filepath, str))
    assert_equal(len(pickled_string), 1734)
Esempio n. 4
0
#!/usr/bin/env python3
#coding:utf-8

import os
import sys

PROJECT_HOME = os.path.dirname(os.path.abspath(__file__)) + "/../"
sys.path.append(PROJECT_HOME)

from webapp.recommend.svm import SVM

model = SVM()

## load test data
data_filepath = os.path.dirname(
    __file__) + "/../../resources/data/test-data.tsv"
labels_filepath = os.path.dirname(
    __file__) + "/../../resources/data/test-label.tsv"
data = model.load_tsv_file(data_filepath)
labels = model.load_tsv_file(labels_filepath)

## restore model from file
result = model.predict_with_default_dumped_model(data)

num_true = 0
num_false = 0
for i in range(0, len(labels)):
    if labels[i] == result[i]:
        num_true += 1
    else:
        num_false += 1
Esempio n. 5
0
#!/usr/bin/env python3
#coding:utf-8

import os
import sys

PROJECT_HOME = os.path.dirname(os.path.abspath(__file__)) + "/../"
sys.path.append(PROJECT_HOME)

from webapp.recommend.svm import SVM

## traininn
model = SVM()
data_filepath = os.path.dirname(
    __file__) + "/../../resources/data/train-data.tsv"
labels_filepath = os.path.dirname(
    __file__) + "/../../resources/data/train-label.tsv"
data = model.load_tsv_file(data_filepath)
labels = model.load_tsv_file(labels_filepath)
clf = model.train(data, labels)

## save default model
model.dump_default_model()
Esempio n. 6
0
def test_predict_with_default_dumped_model():
    model = SVM()
    target = [0, 2, 1]
    result = model.predict_with_default_dumped_model(target)
    assert_equal(result, [3])
Esempio n. 7
0
def test_predict_with_dumped_model():
    model = SVM()
    target = [0, 2, 1]
    filepath = os.path.dirname(__file__) + "/data/trained-svm.pickle"
    result = model.predict_with_dumped_model(target, filepath)
    assert_equal(result, [2])
Esempio n. 8
0
def test_loads_model_by_pickle():
    pickled_filepath = os.path.dirname(__file__) + "/data/trained-svm.pickle"
    model = SVM()
    clf = model.load_model_by_pickle(pickled_filepath)
    predict = clf.predict([0, 9, 0])
    assert_equal(predict, [3])