Instance class.

  • This class supports different features for an instance.
  • The features, together with the id of the instance, will be used in machine learning algorithms.
  • All Features are stored in a dictionary, where keys are feature names (self.features).
Instance properties:
  • Properties are additional side information of an instance (e.g. query_id, entity_id, …).
  • properties are stored in a dictionary (

This is the base instance class. Specific type of instances can inherit form class and add more properties to the base class.

Author:Faegheh Hasibi
class, features=None, target='0', properties=None)[source]

Bases: object

Class attributes:
ins_id: (string) features: a dictionary of feature names and values target: (string) target id or class properties: a dictionary of property names and values
add_feature(feature, value)[source]

Adds a new feature to the features.

Parameters:feature – (string), feature name

:param value

add_property(property, value)[source]

Adds a new property to the properties.

Parameters:property – (string), property name

:param value

classmethod from_json(ins_id, fields)[source]

Reads an instance in JSON format and generates Instance.

  • ins_id – instance id
  • fields – A dictionary of fields

:return (ml.Instance)


Returns the value of a given feature.

:param feature :return value


Returns the value of a given property.

:param property :return value


Converts instance to the JSON format.

Parameters:file_name – (string)

:return JSON dump of the instance.

to_libsvm(features, qid_prop=None)[source]

Converts instance to the Libsvm format. - RankLib format:

<target> qid:<qid> <feature>:<value> … # <info>
  • Example: 3 qid:1 1:1 2:1 3:0 4:0.2 5:0 # 1A

NOTE: the property used for qid(qid_prop) should hold integers

  • features – the list of features that should be in the output
  • qid_prop – property to be used as qid

:return str, instance in the rankLib format.


Converts instances to string.

Parameters:feature_set – features to be included in the output format
Return (string) tab separated string:
 ins_id target ftr_1 ftr_2 … ftr_n properties[source]