def test_highLevelsktime(network=ShapeletForestClassifier()):
    '''
    truly generalised test with sktime tasks/strategies
        load data, build task
        construct classifier, build strategy
        fit,
        score
    '''

    print("start test_highLevelsktime()")

    from sktime.highlevel.tasks import TSCTask
    from sktime.highlevel.strategies import TSCStrategy
    from sklearn.metrics import accuracy_score

    train = load_gunpoint(split='TRAIN')
    test = load_gunpoint(split='TEST')
    task = TSCTask(target='class_val', metadata=train)

    strategy = TSCStrategy(network)
    strategy.fit(task, train.iloc[:10])

    y_pred = strategy.predict(test.iloc[:10])
    y_test = test.iloc[:10][task.target].values.astype(np.float)
    print(accuracy_score(y_test, y_pred))

    print("End test_highLevelsktime()")
def test_pipeline(network=ShapeletForestClassifier()):
    '''
    slightly more generalised test with sktime pipelines
        load data,
        construct pipeline with classifier,
        fit,
        score
    '''

    print("Start test_pipeline()")

    from sktime.pipeline import Pipeline

    # just a simple (useless) pipeline

    steps = [('clf', network)]
    clf = Pipeline(steps)

    X_train, y_train = load_gunpoint(split='TRAIN', return_X_y=True)
    X_test, y_test = load_gunpoint(split='TEST', return_X_y=True)

    hist = clf.fit(X_train[:10], y_train[:10])

    print(clf.score(X_test[:10], y_test[:10]))
    print("End test_pipeline()")
def test_network(network=ShapeletForestClassifier()):
    # sklearn compatibility

    test_basic_univariate(network)
    test_basic_multivariate(network)
    test_pipeline(network)
    test_highLevelsktime(network)
def all_networks_all_tests():

    networks = [
        ShapeletForestClassifier(),
    ]

    for network in networks:
        print('\n\t\t' + network.__class__.__name__ + ' testing started')
        test_network(network)
        print('\t\t' + network.__class__.__name__ + ' testing finished')
def test_basic_univariate(network=ShapeletForestClassifier()):
    '''
    just a super basic test with gunpoint,
        load data,
        construct classifier,
        fit,
        score
    '''

    print("Start test_basic()")

    X_train, y_train = load_gunpoint(split='TRAIN', return_X_y=True)
    X_test, y_test = load_gunpoint(split='TEST', return_X_y=True)

    hist = network.fit(X_train[:10], y_train[:10])

    print(network.score(X_test[:10], y_test[:10]))
    print("End test_basic()")
Ejemplo n.º 6
0
def test_basic_multivariate(network=ShapeletForestClassifier()):
    '''
    just a super basic test with basicmotions,
        load data,
        construct classifier,
        fit,
        score
    '''

    print("Start test_multivariate()")

    X_train, y_train = load_basic_motions(split='train', return_X_y=True)
    X_test, y_test = load_basic_motions(split='test', return_X_y=True)

    hist = network.fit(X_train[:10], y_train[:10])

    print(network.score(X_test[:10], y_test[:10]))
    print("End test_multivariate()")
Ejemplo n.º 7
0
from sklearn.preprocessing import LabelEncoder
from shapeletForest.ensemble import ShapeletForestClassifier
from sktime.datasets import load_gunpoint

if __name__ == "__main__":

    X_train, y_train = load_gunpoint(split='train', return_X_y=True)
    X_test, y_test = load_gunpoint(split='test', return_X_y=True)

    # In case the labels are not numbers
    if not isinstance(y_train[0], (int, float)):
        le = LabelEncoder()
        le.fit(np.unique(y_train))
        y_train = le.transform(y_train)
        y_test = le.transform(y_test)
    else:
        y_train = y_train.astype(np.int64)
        y_test = y_test.astype(np.int64)

    f = ShapeletForestClassifier(n_shapelets=1,
                                 metric="scaled_dtw",
                                 metric_params={"r": 0.1})
    c = time.time()
    f.fit(X_train, y_train)
    print("Classes: ", f.classes_)
    print("Num dimensions: ", X_train.shape[1])
    print("Num patterns: ", X_train.shape[0])
    print("Num timepoints: ", len(X_train.iloc[0].iloc[0]))
    print("Accuracy: ", f.score(X_test, y_test))
    print("Time spent: ", round(time.time() - c))