from wildboar.datasets import load_dataset
from wildboar.ensemble import ShapeletForestClassifier
from wildboar.explain.counterfactual import counterfactuals
from wildboar.linear_model import RocketClassifier

x, y = load_dataset("GunPoint")
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=123)

rocket = RocketClassifier(n_kernels=1000, n_jobs=-1, random_state=123)
rocket.fit(x_train, y_train)
print("Rocket score", rocket.score(x_test, y_test))

sf = ShapeletForestClassifier(n_shapelets=10, n_jobs=-1, random_state=123)
sf.fit(x_train, y_train)
print("Shapelet score", sf.score(x_test, y_test))

nearest = KNeighborsClassifier(n_jobs=-1)
nearest.fit(x_train, y_train)
print("Neighbors score", nearest.score(x_test, y_test))

x_test_cls = x_test[y_test == 1.0][:2]

for method in [rocket, sf, nearest]:
    print(method)
    x_counter, x_success = counterfactuals(
        method,
        x_test_cls,
        2.0,
        method="prototype",
        random_state=123,
Exemplo n.º 2
0
from wildboar.explain.counterfactual import counterfactuals

x_train, x_test, y_train, y_test = load_dataset("TwoLeadECG",
                                                repository="wildboar/ucr",
                                                merge_train_test=False)

# x_train = x_train.repeat(2, axis=0).reshape(x_train.shape[0], 2, -1)
# print(x_train[0])
# x_test = x_test.repeat(2, axis=0).reshape(x_test.shape[0], 2, -1)

clf = ShapeletForestClassifier(metric="euclidean",
                               random_state=1,
                               n_jobs=-1,
                               n_estimators=100)
clf.fit(x_train, y_train)
print(clf.score(x_test, y_test))
y_pred = clf.predict(x_test)
class_ = clf.classes_[1]
print("Class: %s" % class_)
print("Pred: %r" % y_pred)
x_test = x_test[y_pred != class_][:10]
y_test = y_test[y_pred != class_][:10]

x_counterfactual, success, score = counterfactuals(clf,
                                                   x_test,
                                                   class_,
                                                   random_state=123,
                                                   scoring="euclidean")

print(np.mean(score))
print(clf.predict(x_counterfactual))
Exemplo n.º 3
0
    tree = ShapeletTreeClassifier(random_state=10, metric="scaled_dtw")
    tree.fit(x, y, sample_weight=np.ones(x.shape[0]) / x.shape[0])
    print_tree(tree.root_node_)

    print("Score")
    print(tree.score(x, y))
    print("score_done")

    train = np.loadtxt("data/synthetic_control_TRAIN", delimiter=",")
    test = np.loadtxt("data/synthetic_control_TEST", delimiter=",")

    y = train[:, 0].astype(np.intp)
    x = train[:, 1:].astype(np.float64)
    i = np.arange(x.shape[0])

    np.random.shuffle(i)

    x_test = test[:, 1:].astype(np.float64)
    y_test = test[:, 0].astype(np.intp)

    f = ShapeletForestClassifier(n_shapelets=1,
                                 metric="scaled_dtw",
                                 metric_params={"r": 0.1})
    f.fit(x, y)
    c = time.time()
    f.fit(x, y)
    print(f.classes_)
    print("acc:", f.score(x_test, y_test))
    print(round(time.time() - c) * 1000)