Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions python/tests/test_DFtoVW.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ def test_multiple_named_namespaces_multiple_features_multiple_lines():
]


def test_multiple_lines_with_weight():
df = pd.DataFrame({
"y": [1, 2, -1],
"w": [2.5, 1.2, 3.75],
"x": ["a", "b", "c"]
})
conv = DFtoVW(df=df, label=SimpleLabel(label="y", weight="w"), features=Feature("x"))
lines_list = conv.convert_df()
assert lines_list == ['1 2.5 | x=a', '2 1.2 | x=b', '-1 3.75 | x=c']


# Exception tests for SimpleLabel
def test_absent_col_error():
with pytest.raises(ValueError) as value_error:
Expand Down Expand Up @@ -162,6 +173,14 @@ def test_wrong_feature_type_error():
assert expected == str(type_error.value)


def test_wrong_weight_type_error():
df = pd.DataFrame({"y": [1], "x": [2], "w": ["a"]})
with pytest.raises(TypeError) as type_error:
DFtoVW(df=df, label=SimpleLabel(label="y", weight="w"), features=Feature("x"))
expected = "In argument 'weight' of 'SimpleLabel', column 'w' should be either of the following type(s): 'int', 'float'."
assert expected == str(type_error.value)


# Tests for MulticlassLabel
def test_multiclasslabel():
df = pd.DataFrame({"a": [1], "b": [0.5], "c": [-3]})
Expand Down
18 changes: 11 additions & 7 deletions python/vowpalwabbit/DFtoVW.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,24 @@ class SimpleLabel(object):
"""The simple label type for the constructor of DFtoVW."""

label = AttributeDescriptor("label", expected_type=(int, float))
weight = AttributeDescriptor("weight", expected_type=(int, float))

def __init__(self, label):
def __init__(self, label, weight=None):
"""Initialize a SimpleLabel instance.

Parameters
----------
label : str
The column name with the label.
weight : str
The column name with the weight.

Returns
-------
self : SimpleLabel
"""
self.label = label
self.weight = weight

def process(self, df):
"""Returns the SimpleLabel string representation.
Expand All @@ -253,7 +257,10 @@ def process(self, df):
pandas.Series
The SimpleLabel string representation.
"""
return self.label.get_col(df)
out = self.label.get_col(df)
if self.weight is not None:
out += " " + self.weight.get_col(df)
return out


class MulticlassLabel(object):
Expand Down Expand Up @@ -292,12 +299,9 @@ def process(self, df):
pandas.Series
The MulticlassLabel string representation.
"""
label_col = self.label.get_col(df)
out = self.label.get_col(df)
if self.weight is not None:
weight_col = self.weight.get_col(df)
out = label_col + " " + weight_col
else:
out = label_col
out += " " + self.weight.get_col(df)
return out
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not directly linked to this PR (but shorter)



Expand Down