Instance#

class capymoa.instance.Instance[source]#

Bases: object

An instance is a single data point in a stream. It contains a feature vector and a schema that describes the datastream it belongs to.

In supervised learning, your more likely to encounter LabeledInstance or RegressionInstance which are subclasses of Instance with a class label or target value respectively.

__init__(
schema: Schema,
instance: InstanceExample | ndarray[tuple[Any, ...], dtype[float64]],
) None[source]#

Creates a new instance.

Its recommended that you prefer using from_array() or from_java_instance() to create instances, as they provide a more user-friendly interface.

Parameters:
  • schema – A schema that describes the datastream the instance belongs to.

  • instance – A vector of features (float values) or a Java instance.

Raises:

ValueError – If the given instance type is of an unsupported type.

classmethod from_array(
schema: Schema,
instance: ndarray[tuple[Any, ...], dtype[float64]],
) Instance[source]#

A class constructor to create an instance from a schema and a vector of features.

This is useful in the rare cases you need to create custom unlabeled instances from scratch. In most cases, your datastream will automatically create instances for you.

>>> from capymoa.stream import Schema
...
>>> from capymoa.instance import Instance
>>> import numpy as np
>>> schema = Schema.from_custom(["f1", "f2", "target"], "target")
>>> x = np.array([0.1, 0.2])
>>> instance = Instance.from_array(schema, x)
>>> instance
Instance(
    Schema(unnamed),
    x=[0.1 0.2],
)
Parameters:
  • schema – A schema that describes the datastream the instance belongs to.

  • instance – A vector (numpy.ndarray) of features (float values

Returns:

A new Instance object

classmethod from_csv_row(schema: Schema, row: Sequence[str]) Instance[source]#

Create an instance from a CSV row.

>>> from capymoa.stream import Schema
>>> from capymoa.instance import Instance
>>> schema = Schema.from_custom(
...     ["feature1", "feature2", "target"],
...     target="target",
...     categories={"feature2": ["A", "B"], "target": ["yes", "no"]},
...     name="classification-example"
... )
>>> row = ["1.0", "A", "yes"]
>>> instance = Instance.from_csv_row(schema, row)
>>> instance
Instance(
    Schema(classification-example),
    x=[1. 0.],
)
>>> instance.x
array([1., 0.])
Parameters:
  • schema – A schema providing the structure of each row. Like the header of the CSV.

  • row – A sequence of strings representing a CSV row.

Raises:

ValueError – If an attribute type is unsupported.

Returns:

A new Instance object.

classmethod from_java_instance(
schema: Schema,
java_instance: InstanceExample,
) Instance[source]#
property java_instance: InstanceExample#

Returns a representation of the instance in Java for use in MOA. This method is for advanced users who want to directly interact with MOA’s Java API.

property schema: Schema#

Returns the schema of the instance and the stream it belongs to.

property x: ndarray[tuple[Any, ...], dtype[float64]]#

Returns a feature vector containing float values for the instance.

  • NaN values indicate missing features.