def lts_discovery(X_train, y_train, X_test, y_test, nr_shap, l, r, reg, max_it, shap_out_path, pred_out_path, timing_out_path): # Fit LTS model, print metrics on test-set, write away predictions and shapelets shapelet_dict = grabocka_params_to_shapelet_size_dict( X_train.shape[0], X_train.shape[1], int(nr_shap * X_train.shape[1]), l, r) clf = ShapeletModel(n_shapelets_per_size=shapelet_dict, max_iter=max_it, verbose_level=1, batch_size=1, optimizer='sgd', weight_regularizer=reg) start = time.time() clf.fit(np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1)), y_train) learning_time = time.time() - start print([len(x) for x in clf.shapelets_]) print(clf.get_weights()) print('Learning shapelets took {}s'.format(learning_time)) with open(shap_out_path, 'w+') as ofp: for shap in clf.shapelets_: ofp.write(str(np.reshape(shap, (-1))) + '\n') with open(timing_out_path, 'w+') as ofp: ofp.write(str(learning_time)) X_distances_train = clf.transform(X_train) X_distances_test = clf.transform(X_test) fit_lr(X_distances_train, y_train, X_distances_test, y_test, pred_out_path)
def test_shapelets(): pytest.importorskip('keras') from tslearn.shapelets import ShapeletModel n, sz, d = 15, 10, 2 rng = np.random.RandomState(0) time_series = rng.randn(n, sz, d) y = rng.randint(2, size=n) clf = ShapeletModel(n_shapelets_per_size={2: 5}, max_iter=1, verbose=0, optimizer="sgd", random_state=0) clf.fit(time_series, y) np.testing.assert_allclose(clf.shapelets_[0], np.array([[0.56373, 0.494684], [1.235707, 1.119235]]), atol=1e-2) np.testing.assert_allclose(clf.predict(time_series), np.array([0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0])) cross_validate(clf, time_series, y, cv=2) model = ShapeletModel(n_shapelets_per_size={3: 2, 4: 1}, max_iter = 1) model.fit(time_series, y) for shp, shp_bis in zip(model.shapelets_, model.shapelets_as_time_series_): np.testing.assert_allclose(shp, to_time_series(shp_bis, remove_nans=True)) # Test set_weights / get_weights clf = ShapeletModel(n_shapelets_per_size={2: 5}, max_iter=1, verbose=0, random_state=0) clf.fit(time_series, y) preds_before = clf.predict_proba(time_series) weights = clf.get_weights() # Change number of iterations, then refit, then set weights clf.max_iter *= 2 clf.fit(time_series, y) clf.set_weights(weights) np.testing.assert_allclose(preds_before, clf.predict_proba(time_series))
def test_shapelets(): pytest.importorskip('tensorflow') from tslearn.shapelets import ShapeletModel import tensorflow as tf n, sz, d = 15, 10, 2 rng = np.random.RandomState(0) time_series = rng.randn(n, sz, d) y = rng.randint(2, size=n) clf = ShapeletModel(n_shapelets_per_size={2: 5}, max_iter=1, verbose=0, optimizer="sgd", random_state=0) cross_validate(clf, time_series, y, cv=2) clf = ShapeletModel(n_shapelets_per_size={2: 5}, max_iter=1, verbose=0, optimizer=tf.optimizers.Adam(.1), random_state=0) cross_validate(clf, time_series, y, cv=2) model = ShapeletModel(n_shapelets_per_size={3: 2, 4: 1}, max_iter=1) model.fit(time_series, y) for shp, shp_bis in zip(model.shapelets_, model.shapelets_as_time_series_): np.testing.assert_allclose(shp, to_time_series(shp_bis, remove_nans=True)) # Test set_weights / get_weights clf = ShapeletModel(n_shapelets_per_size={2: 5}, max_iter=1, verbose=0, random_state=0) clf.fit(time_series, y) preds_before = clf.predict_proba(time_series) weights = clf.get_weights() # Change number of iterations, then refit, then set weights clf.max_iter *= 2 clf.fit(time_series, y) clf.set_weights(weights) np.testing.assert_allclose(preds_before, clf.predict_proba(time_series))