vowpalwabbit.pyvw

Python binding for pylibvw class

class vowpalwabbit.pyvw.SearchTask(vw, sch, num_actions)

Bases: object

Search task class

example(initStringOrDict=None, labelType=0)

Create an example initStringOrDict can specify example as VW formatted string, or a dictionary labelType can specify the desire label type

learn(data_iterator)

Train search task by providing an iterator of examples

predict(my_example, useOracle=False)

Return prediction

class vowpalwabbit.pyvw.abstract_label

Bases: object

An abstract class for a VW label.

from_example(ex)

grab a label from a given VW example

class vowpalwabbit.pyvw.cbandits_label(costs=[], prediction=0)

Bases: vowpalwabbit.pyvw.abstract_label

Class for contextual bandits VW label

from_example(ex)
class vowpalwabbit.pyvw.cost_sensitive_label(costs=[], prediction=0)

Bases: vowpalwabbit.pyvw.abstract_label

Class for cost sensative VW label

from_example(ex)
class vowpalwabbit.pyvw.example(vw, initStringOrDictOrRawExample=None, labelType=0)

Bases: pylibvw.example

The example class is a (non-trivial) wrapper around pylibvw.example. Most of the wrapping is to make the interface easier to use (by making the types safer via namespace_id) and also with added python-specific functionality.

ensure_namespace_exists(ns)

Check to see if a namespace already exists. If it does, do nothing. If it doesn’t, add it.

feature(ns, i)

Get the i-th hashed feature id in a given namespace (i can range from 0 to self.num_features_in(ns)-1)

feature_weight(ns, i)

Get the value(weight) associated with a given feature id in a given namespace (i can range from 0 to self.num_features_in(ns)-1)

get_feature_id(ns, feature, ns_hash=None)

Return the hashed feature id for a given feature in a given namespace. feature can either be an integer (already a feature id) or a string, in which case it is hashed. Note that if –hash all is on, then get_feature_id(ns,”5”) != get_feature_id(ns, 5). If you’ve already hashed the namespace, you can optionally provide that value to avoid re-hashing it.

get_label(label_class=<class 'vowpalwabbit.pyvw.simple_label'>)

Given a known label class (default is simple_label), get the corresponding label structure for this example.

get_ns(id)

Construct a namespace_id from either an integer or string (or, if a namespace_id is fed it, just return it directly).

iter_features()

Iterate over all feature/value pairs in this example (all namespace included).

learn()

Learn on this example (and before learning, automatically call setup_example if the example hasn’t yet been setup).

num_features_in(ns)

Return the total number of features in a given namespace.

pop_feature(ns)

Remove the top feature from a given namespace; returns True if a feature was removed, returns False if there were no features to pop.

pop_namespace()

Remove the top namespace from an example; returns True if a namespace was removed, or False if there were no namespaces left.

push_feature(ns, feature, v=1.0, ns_hash=None)

Add an unhashed feature to a given namespace.

push_features(ns, featureList)

Push a list of features to a given namespace. Each feature in the list can either be an integer (already hashed) or a string (to be hashed) and may be paired with a value or not (if not, the value is assumed to be 1.0).

Examples:

ex.push_features(‘x’, [‘a’, ‘b’]) ex.push_features(‘y’, [(‘c’, 1.), ‘d’])

space_hash = vw.hash_space( ‘x’ ) feat_hash = vw.hash_feature( ‘a’, space_hash ) ex.push_features(‘x’, [feat_hash]) # note: ‘x’ should match the space_hash!

push_hashed_feature(ns, f, v=1.0)

Add a hashed feature to a given namespace.

push_namespace(ns)

Push a new namespace onto this example. You should only do this if you’re sure that this example doesn’t already have the given namespace.

set_label_string(string)

Give this example a new label, formatted as a string (ala the VW data file format).

setup_example()

If this example hasn’t already been setup (ie, quadratic features constructed, etc.), do so.

sum_feat_sq(ns)

Return the total sum feature-value squared for a given namespace.

unsetup_example()

If this example has been setup, reverse that process so you can continue editing the examples.

class vowpalwabbit.pyvw.example_namespace(ex, ns, ns_hash=None)

Bases: object

The example_namespace class is a helper class that allows you to extract namespaces from examples and operate at a namespace level rather than an example level. Mainly this is done to enable indexing like ex[‘x’][0] to get the 0th feature in namespace ‘x’ in example ex.

iter_features()

iterate over all feature/value pairs in this namespace.

num_features_in()

Return the total number of features in this namespace.

pop_feature()

Remove the top feature from the current namespace; returns True if a feature was removed, returns False if there were no features to pop.

push_feature(feature, v=1.0)

Add an unhashed feature to the current namespace (fails if setup has already run on this example).

push_features(ns, featureList)

Push a list of features to a given namespace. Each feature in the list can either be an integer (already hashed) or a string (to be hashed) and may be paired with a value or not (if not, the value is assumed to be 1.0). See example.push_features for examples.

vowpalwabbit.pyvw.get_prediction(ec, prediction_type)

Get specified type of prediction from example

class vowpalwabbit.pyvw.multiclass_label(label=1, weight=1.0, prediction=1)

Bases: vowpalwabbit.pyvw.abstract_label

Class for multiclass VW label with prediction

from_example(ex)
class vowpalwabbit.pyvw.multiclass_probabilities_label(label, prediction=None)

Bases: vowpalwabbit.pyvw.abstract_label

Class for multiclass VW label with probabilities

from_example(ex)
class vowpalwabbit.pyvw.namespace_id(ex, id)

Bases: object

The namespace_id class is simply a wrapper to convert between hash spaces referred to by character (eg ‘x’) versus their index in a particular example. Mostly used internally, you shouldn’t really need to touch this.

class vowpalwabbit.pyvw.simple_label(label=0.0, weight=1.0, initial=0.0, prediction=0.0)

Bases: vowpalwabbit.pyvw.abstract_label

Class for simple VW label

from_example(ex)
class vowpalwabbit.pyvw.vw(arg_str=None, **kw)

Bases: pylibvw.vw

The pyvw.vw object is a (trivial) wrapper around the pylibvw.vw object; you’re probably best off using this directly and ignoring the pylibvw.vw structure entirely.

example(stringOrDict=None, labelType=0)

TODO: document

finish()

stop VW by calling finish (and, eg, write weights to disk)

finish_example(ex)

Should only be used in conjunction with the parse method

get_weight(index, offset=0)

Given an (integer) index (and an optional offset), return the weight for that position in the (learned) weight vector.

init_search_task(search_task, task_data=None)
learn(ec)

Perform an online update; ec can either be an example object or a string (in which case it is parsed and then learned on) or list which is iterated over.

num_weights()

Get length of weight vector.

parse(str_ex, labelType=0)

Returns a collection of examples for a multiline example learner or a single example for a single example learner.

predict(ec, prediction_type=None)

Just make a prediction on this example; ec can either be an example object or a string (in which case it is parsed and then predicted on).

if prediction_type is provided the matching return type is used otherwise the the learner’s prediction type will determine the output.

save(filename)

save model to disk