Implementation:Recommenders team Recommenders VowpalWabbit
| Knowledge Sources | |
|---|---|
| Domains | Online Learning, Recommendation Systems, Command-Line Wrappers |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
The VW class provides a Python wrapper around the Vowpal Wabbit command-line tool for recommendation tasks, converting between pandas DataFrames and VW's native input format.
Description
The VW class manages the full Vowpal Wabbit workflow through temporary files and subprocess invocations. The key components are:
- Initialization -- Creates temporary files for training data, test data, model, and predictions using TemporaryDirectory. Parses hyperparameters into valid VW command-line arguments via to_vw_cmd, parse_train_params, and parse_test_params, which strip invalid or conflicting options for each mode (e.g., removing learning rate parameters from test commands).
- Data Conversion -- The to_vw_file method converts a pandas DataFrame into VW's text input format: [label] [tag]|user [userID] |item [itemID]. For training, ratings are cast to integers and optionally converted to binary values (-1/1) when logistic mode is detected. For testing, the label field is left empty.
- Training -- The fit method writes the training DataFrame to disk in VW format and invokes the VW binary via subprocess.run.
- Prediction -- The predict method writes the test DataFrame, invokes VW in test mode (--testonly flag), reads predictions from the output file, and joins them back into the original DataFrame.
- Cleanup -- The __del__ destructor cleans up all temporary files via TemporaryDirectory.cleanup.
The to_vw_cmd static method converts a dictionary of parameters to VW command-line format, using single hyphens for single-character flags and double hyphens for longer parameter names.
Usage
Use the VW wrapper for demonstration and experimentation with Vowpal Wabbit's fast online learning algorithms in recommendation notebooks. As noted in the module docstring, this wrapper is not recommended for production use; instead, use the native VW Python bindings or the command line directly. The wrapper supports VW's namespace-based feature separation, enabling interaction features through command-line options like quadratic or cubic interactions.
Code Reference
Source Location
- Repository: Recommenders
- File: recommenders/models/vowpal_wabbit/vw.py
- Lines: 1-266
Signature
class VW:
def __init__(self, col_user=DEFAULT_USER_COL, col_item=DEFAULT_ITEM_COL,
col_rating=DEFAULT_RATING_COL, col_timestamp=DEFAULT_TIMESTAMP_COL,
col_prediction=DEFAULT_PREDICTION_COL, **kwargs)
@staticmethod
def to_vw_cmd(params)
def parse_train_params(self, params)
def parse_test_params(self, params)
def to_vw_file(self, df, train=True)
def fit(self, df)
def predict(self, df)
Import
from recommenders.models.vowpal_wabbit.vw import VW
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| col_user | str | No | User column name (default: DEFAULT_USER_COL) |
| col_item | str | No | Item column name (default: DEFAULT_ITEM_COL) |
| col_rating | str | No | Rating column name (default: DEFAULT_RATING_COL) |
| col_timestamp | str | No | Timestamp column name (default: DEFAULT_TIMESTAMP_COL) |
| col_prediction | str | No | Prediction column name (default: DEFAULT_PREDICTION_COL) |
| **kwargs | dict | No | VW hyperparameters passed as keyword arguments (e.g., loss_function="logistic", b=18) |
| df | pandas.DataFrame | Yes | Input DataFrame containing user, item, and rating columns (for fit and predict) |
| params | dict | Yes | Dictionary of VW parameters where key is the parameter name and value is the value (for to_vw_cmd) |
Outputs
| Name | Type | Description |
|---|---|---|
| to_vw_cmd return | list[str] | VW command-line parameters as a list of strings |
| predict return | pandas.DataFrame | Input DataFrame with an additional prediction column joined from VW output |
Usage Examples
Basic Usage
from recommenders.models.vowpal_wabbit.vw import VW
# Initialize VW with matrix factorization parameters
model = VW(
col_user="userID",
col_item="itemID",
col_rating="rating",
rank=10,
l2=0.001,
learning_rate=0.01,
b=18,
quiet=True
)
# Train the model
model.fit(train_df)
# Generate predictions
predictions = model.predict(test_df)