RegressionInstance#
- class capymoa.instance.RegressionInstance[source]#
Bases:
InstanceAn
Instancewith a continuous target value.Most of the time, regression datastreams will automatically return instances for you with the target value. For example, the
capymoa.datasets.Frieddataset:>>> from capymoa.datasets import Fried ... >>> from capymoa.instance import RegressionInstance >>> stream = Fried() >>> instance: RegressionInstance = stream.next_instance() >>> instance.y_value 17.949 >>> instance.x array([0.487, 0.072, 0.004, 0.833, 0.765, 0.6 , 0.132, 0.886, 0.073, 0.342])
- __init__(
- schema: Schema,
- instance: InstanceExample | Tuple[ndarray[tuple[Any, ...], dtype[float64]], float64],
Creates a new instance.
Its recommended that you prefer using
from_array()orfrom_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,
- x: ndarray[tuple[Any, ...], dtype[float64]],
- y_value: float64,
Creates a new regression instance from a schema, feature vector, and target value.
This is useful in the rare cases you need to create custom regression instances from scratch. In most cases, your datastream will automatically create these for you.
>>> from capymoa.stream import Schema ... >>> from capymoa.instance import LabeledInstance >>> import numpy as np >>> schema = Schema.from_custom( ... ["f1", "f2", "target"], ... target="target", ... name="CustomDataset", ... ) >>> x = np.array([0.1, 0.2]) >>> instance = RegressionInstance.from_array(schema, x, 0.5) >>> instance RegressionInstance( Schema(CustomDataset), x=[0.1 0.2], y_value=0.5 ) >>> instance.y_value 0.5 >>> instance.java_instance.toString() '0.1,0.2,0.5,'
- Parameters:
schema – A schema describing the datastream the instance belongs to.
x – A vector of features
numpy.ndarraycontaining float values.y_value – A float value representing the target value or dependent variable.
- Returns:
A new
RegressionInstanceobject.
- 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
Instanceobject.
- 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 x: ndarray[tuple[Any, ...], dtype[float64]]#
Returns a feature vector containing float values for the instance.
NaN values indicate missing features.
- property y_value: float64#
Returns the target value of the instance.