Exemplo n.º 1
0
def test_slates_label():
    model = Workspace(slates=True, quiet=True)
    slates_shared_label = vowpalwabbit.SlatesLabel.from_example(
        model.example("slates shared 0.8 | shared_0 shared_1"))
    slates_action_label = vowpalwabbit.SlatesLabel.from_example(
        model.example("slates action 1 | action_3"))
    slates_slot_label = vowpalwabbit.SlatesLabel.from_example(
        model.example("slates slot 1:0.8,0:0.1,2:0.1 | slot_0"))
    assert slates_shared_label.type == vowpalwabbit.SlatesLabelType.SHARED
    assert slates_shared_label.labeled == True
    assert isclose(slates_shared_label.cost, 0.8)
    assert str(slates_shared_label) == "slates shared 0.8"
    assert slates_action_label.type == vowpalwabbit.SlatesLabelType.ACTION
    assert slates_action_label.labeled == False
    assert slates_action_label.weight == 1.0
    assert slates_action_label.slot_id == 1
    assert str(slates_action_label) == "slates action 1"
    assert slates_slot_label.type == vowpalwabbit.SlatesLabelType.SLOT
    assert slates_slot_label.labeled == True
    assert slates_slot_label.probabilities[0].action == 1
    assert isclose(slates_slot_label.probabilities[0].score, 0.8)
    assert slates_slot_label.probabilities[1].action == 0
    assert isclose(slates_slot_label.probabilities[1].score, 0.1)
    assert slates_slot_label.probabilities[2].action == 2
    assert isclose(slates_slot_label.probabilities[2].score, 0.1)
    assert str(slates_slot_label) == "slates slot 1:0.8,0:0.1,2:0.1"
    del model
Exemplo n.º 2
0
def test_ccb_label():
    model = Workspace(ccb_explore_adf=True, quiet=True)
    ccb_shared_label = vowpalwabbit.CCBLabel.from_example(
        model.example("ccb shared | shared_0 shared_1"))
    ccb_action_label = vowpalwabbit.CCBLabel.from_example(
        model.example("ccb action | action_1 action_3"))
    ccb_slot_label = vowpalwabbit.CCBLabel.from_example(
        model.example("ccb slot 0:0.8:1.0 0 | slot_0"))
    ccb_slot_pred_label = vowpalwabbit.CCBLabel.from_example(
        model.example("ccb slot |"))
    assert ccb_shared_label.type == vowpalwabbit.CCBLabelType.SHARED
    assert len(ccb_shared_label.explicit_included_actions) == 0
    assert ccb_shared_label.outcome is None
    assert str(ccb_shared_label) == "ccb shared"
    assert ccb_action_label.type == vowpalwabbit.CCBLabelType.ACTION
    assert len(ccb_action_label.explicit_included_actions) == 0
    assert ccb_action_label.weight == 1.0
    assert ccb_action_label.outcome is None
    assert str(ccb_action_label) == "ccb action"
    assert ccb_slot_label.type == vowpalwabbit.CCBLabelType.SLOT
    assert ccb_slot_label.explicit_included_actions[0] == 0
    assert ccb_slot_label.outcome.action_probs[0].action == 0
    assert isclose(ccb_slot_label.outcome.action_probs[0].score, 1.0)
    assert isclose(ccb_slot_label.outcome.cost, 0.8)
    assert str(ccb_slot_label) == "ccb slot 0:0.8:1.0 0"
    assert ccb_slot_pred_label.type == vowpalwabbit.CCBLabelType.SLOT
    assert len(ccb_slot_pred_label.explicit_included_actions) == 0
    assert ccb_slot_pred_label.outcome is None
    assert str(ccb_slot_pred_label) == "ccb slot"
    del model
Exemplo n.º 3
0
def test_action_scores_prediction_type():
    model = Workspace(loss_function="logistic", csoaa_ldf="m", quiet=True)
    multi_ex = [model.example("1:1 | a b c"), model.example("2:-1  | a b c")]
    model.learn(multi_ex)
    assert model.get_prediction_type() == model.pMULTICLASS
    multi_ex = [model.example("1 | a b c"), model.example("2 | a b c")]
    prediction = model.predict(multi_ex)
    assert isinstance(prediction, int)
    del model
Exemplo n.º 4
0
def test_example_learn():
    vw_ex = Workspace(quiet=True)
    ex = vw_ex.example("1 |a two features |b more features here")
    ex.learn()
    assert ex.setup_done is True
    ex.unsetup_example()  # unsetup an example as it is already setup
    assert ex.setup_done is False
Exemplo n.º 5
0
def test_prob_prediction_type():
    model = Workspace(
        loss_function="logistic",
        csoaa_ldf="mc",
        probabilities=True,
        quiet=True,
    )
    multi_ex = [
        model.example("1:0.2 | a b c"),
        model.example("2:0.8  | a b c"),
    ]
    model.learn(multi_ex)
    assert model.get_prediction_type() == vowpalwabbit.PredictionType.PROB
    multi_ex = [model.example("1 | a b c"), model.example("2 | a b c")]
    prediction = model.predict(multi_ex)
    assert isinstance(prediction, float)
    del model
Exemplo n.º 6
0
def test_example_namespace_id():
    vw_ex = Workspace(quiet=True)
    ex = vw_ex.example("1 |a two features |b more features here")
    ns = vowpalwabbit.NamespaceId(ex, 1)
    assert isinstance(ex.get_ns(1), vowpalwabbit.NamespaceId)
    assert isinstance(ex[2], vowpalwabbit.ExampleNamespace)
    assert ex.setup_done is True
    assert ex.num_features_in(ns) == 3
Exemplo n.º 7
0
def test_SimpleLabel_example():
    vw_ex = Workspace(quiet=True)
    ex = vw_ex.example("1 |a two features |b more features here")
    sl2 = vowpalwabbit.SimpleLabel.from_example(ex)
    assert sl2.label == 1.0
    assert sl2.weight == 1.0
    assert sl2.prediction == 0.0
    assert sl2.initial == 0.0
    assert str(sl2) == "1.0"
Exemplo n.º 8
0
def test_multilabel_label():
    model = Workspace(multilabel_oaa=5, quiet=True)
    multil = vowpalwabbit.MultilabelLabel.from_example(
        model.example("1,2,3 |"))
    assert len(multil.labels) == 3
    assert multil.labels[0] == 1
    assert multil.labels[1] == 2
    assert multil.labels[2] == 3
    assert str(multil) == "1,2,3"
Exemplo n.º 9
0
def test_CostSensitiveLabel():
    model = Workspace(csoaa=4, quiet=True)
    csl = vowpalwabbit.CostSensitiveLabel.from_example(model.example("2:5 |"))
    assert csl.costs[0].label == 2
    assert csl.costs[0].wap_value == 0.0
    assert csl.costs[0].partial_prediction == 0.0
    assert csl.costs[0].cost == 5.0
    assert str(csl) == "2:5.0"
    del model
Exemplo n.º 10
0
def test_CBLabel():
    model = Workspace(cb=4, quiet=True)
    cbl = vowpalwabbit.CBLabel.from_example(model.example("1:10:0.5 |"))
    assert cbl.weight == 1.0
    assert cbl.costs[0].action == 1
    assert cbl.costs[0].probability == 0.5
    assert cbl.costs[0].partial_prediction == 0
    assert cbl.costs[0].cost == 10.0
    assert str(cbl) == "1:10.0:0.5"
    del model
Exemplo n.º 11
0
def test_example_features():
    vw_ex = Workspace(quiet=True)
    ex = vw_ex.example("1 |a two features |b more features here")
    ns = vowpalwabbit.NamespaceId(ex, 1)
    assert ex.get_feature_id(ns, "a") == 127530
    ex.push_hashed_feature(ns, 1122)
    ex.push_features("x", [("c", 1.0), "d"])
    ex.push_feature(ns, 11000)
    assert ex.num_features_in("x") == 2
    assert ex.sum_feat_sq(ns) == 5.0
    ns2 = vowpalwabbit.NamespaceId(ex, 2)
    ex.push_namespace(ns2)
    assert ex.pop_namespace()
Exemplo n.º 12
0
def test_CBContinuousLabel():
    model = Workspace(cats=4,
                      min_value=185,
                      max_value=23959,
                      bandwidth=3000,
                      quiet=True)
    cb_contl = vowpalwabbit.CBContinuousLabel.from_example(
        model.example("ca 1:10:0.5 |"))
    assert cb_contl.costs[0].action == 1
    assert cb_contl.costs[0].pdf_value == 0.5
    assert cb_contl.costs[0].cost == 10.0
    assert str(cb_contl) == "ca 1:10.0:0.5"
    del model
Exemplo n.º 13
0
def test_namespace_id():
    vw_ex = Workspace(quiet=True)
    ex = vw_ex.example("1 |a two features |b more features here")
    nm1 = vowpalwabbit.NamespaceId(ex, 0)
    nm2 = vowpalwabbit.NamespaceId(ex, 1)
    nm3 = vowpalwabbit.NamespaceId(ex, 2)
    assert nm1.id == 0
    assert nm1.ord_ns == 97
    assert nm1.ns == "a"
    assert nm2.id == 1
    assert nm2.ord_ns == 98
    assert nm2.ns == "b"
    assert nm3.id == 2
    assert nm3.ord_ns == 128
    assert nm3.ns == "\x80"  # Represents string of ord_ns
Exemplo n.º 14
0
def test_example_namespace():
    vw_ex = Workspace(quiet=True)
    ex = vw_ex.example("1 |a two features |b more features here")
    ns_id = vowpalwabbit.NamespaceId(ex, 1)
    ex_nm = vowpalwabbit.ExampleNamespace(ex,
                                          ns_id,
                                          ns_hash=vw_ex.hash_space(ns_id.ns))
    assert isinstance(ex_nm.ex, vowpalwabbit.Example)
    assert isinstance(ex_nm.ns, vowpalwabbit.NamespaceId)
    assert ex_nm.ns_hash == 2514386435
    assert ex_nm.num_features_in() == 3
    assert ex_nm[2] == (11617, 1.0)  # represents (feature, value)
    iter_obj = ex_nm.iter_features()
    for i in range(ex_nm.num_features_in()):
        assert ex_nm[i] == next(iter_obj)
    assert ex_nm.pop_feature()
    ex_nm.push_features(ns_id, ["c", "d"])
    assert ex_nm.num_features_in() == 4
Exemplo n.º 15
0
def test_example_label():
    vw_ex = Workspace(quiet=True)
    ex = vw_ex.example("1 |a two features |b more features here")
    ex.set_label_string("1.0")
    assert isinstance(ex.get_label(), vowpalwabbit.SimpleLabel)