def test_contextualbanditlabel_multiple_label(): df = pd.DataFrame( {"a1": [1], "c1": [-0.5], "p1": [0.1], "a2": [2], "c2": [-1.5], "p2": [0.6], "x": [1]} ) conv = DFtoVW( df=df, label=[ContextualbanditLabel("a1", "c1", "p1"), ContextualbanditLabel("a2", "c2", "p2")], features=Feature("x"), ) first_line = conv.convert_df()[0] assert first_line == "1:-0.5:0.1 2:-1.5:0.6 | x:1"
def test_listlabel_mixed_label_error(): with pytest.raises(TypeError) as type_error: df = pd.DataFrame({"a": [1], "c": [-0.5], "p": [0.1], "b": [2], "x": [1]}) conv = DFtoVW( df=df, label=[ContextualbanditLabel("a", "c", "p"), MultiLabel("b")], features=Feature("x"), ) expected = "The list passed in 'label' has mixed label types." assert expected == str(type_error.value)
def test_contextualbanditlabel_non_positive_action(): df = pd.DataFrame({"a": [0], "c": [-0.5], "p": [0.5], "x": [1]}) with pytest.raises(ValueError) as value_error: DFtoVW( df=df, label=ContextualbanditLabel("a", "c", "p"), features=Feature("x"), ) expected = "In argument 'action' of 'ContextualbanditLabel', column 'a' must be >= 1." assert expected == str(value_error.value)
def test_contextualbanditlabel_non_float_proba_error(): df = pd.DataFrame({"a": [1], "c": [-0.5], "p": [1], "x": [1]}) with pytest.raises(TypeError) as value_error: DFtoVW( df=df, label=ContextualbanditLabel("a", "c", "p"), features=Feature("x"), ) expected = "In argument 'probability' of 'ContextualbanditLabel', column 'p' should be either of the following type(s): 'float'." assert expected == str(value_error.value)
def test_contextualbanditlabel_negative_proba_error(): df = pd.DataFrame({"a": [1], "c": [-0.5], "p": [-0.1], "x": [1]}) with pytest.raises(ValueError) as value_error: DFtoVW( df=df, label=ContextualbanditLabel("a", "c", "p"), features=Feature("x"), ) expected = ( "In argument 'probability' of 'ContextualbanditLabel', column 'p' must be >= 0 and <= 1." ) assert expected == str(value_error.value)
def test_contextualbanditlabel_one_label(): df = pd.DataFrame({"a": [1], "c": [-0.5], "p": [0.1], "x": [1]}) conv = DFtoVW(df=df, label=ContextualbanditLabel("a", "c", "p"), features=Feature("x")) first_line = conv.convert_df()[0] assert first_line == "1:-0.5:0.1 | x:1"