πŸ“„ Tablerow ClassifyΒΆ

cards.tablerow_classify

TaskCard(
    loader=LoadFromKaggle(
        url="https://www.kaggle.com/datasets/fedesoriano/heart-failure-prediction",
    ),
    preprocess_steps=[
        SplitRandomMix(
            mix={
                "train": "train[70%]",
                "validation": "train[10%]",
                "test": "train[20%]",
            },
        ),
        Rename(
            field_to_field={
                "HeartDisease": "label",
            },
        ),
        MapInstanceValues(
            mappers={
                "label": {
                    "0": "Normal",
                    "1": "Heart Disease",
                },
            },
        ),
        Set(
            fields={
                "text_type": "Person medical record",
                "type_of_class": "Heart Disease Possibility",
            },
        ),
        ExtractFieldValues(
            field="label",
            to_field="classes",
            stream_name="train",
        ),
        SerializeTableRowAsText(
            fields=[
                "Age",
                "Sex",
                "ChestPainType",
                "RestingBP",
                "Cholesterol",
                "FastingBS",
                "RestingECG",
                "MaxHR",
                "ExerciseAngina",
                "Oldpeak",
                "ST_Slope",
            ],
            to_field="text",
            max_cell_length=25,
        ),
    ],
    task="tasks.classification.multi_class",
    templates="templates.classification.multi_class.all",
)
[source]

from unitxt.loaders import LoadFromKaggle
from unitxt.operators import ExtractFieldValues, MapInstanceValues, Rename, Set
from unitxt.splitters import SplitRandomMix
from unitxt.struct_data_operators import SerializeTableRowAsText

Explanation about TaskCardΒΆ

TaskCard delineates the phases in transforming the source dataset into model input, and specifies the metrics for evaluation of model output.

Args:
loader:

specifies the source address and the loading operator that can access that source and transform it into a unitxt multistream.

preprocess_steps:

list of unitxt operators to process the data source into model input.

task:

specifies the fields (of the already (pre)processed instance) making the inputs, the fields making the outputs, and the metrics to be used for evaluating the model output.

templates:

format strings to be applied on the input fields (specified by the task) and the output fields. The template also carries the instructions and the list of postprocessing steps, to be applied to the model output.

Explanation about MapInstanceValuesΒΆ

A class used to map instance values into other values.

This class is a type of InstanceOperator, it maps values of instances in a stream using predefined mappers.

Args:
mappers (Dict[str, Dict[str, Any]]):

The mappers to use for mapping instance values. Keys are the names of the fields to undergo mapping, and values are dictionaries that define the mapping from old values to new values. Note that mapped values are defined by their string representation, so mapped values are converted to strings before being looked up in the mappers.

strict (bool):

If True, the mapping is applied strictly. That means if a value does not exist in the mapper, it will raise a KeyError. If False, values that are not present in the mapper are kept as they are.

process_every_value (bool):

If True, all fields to be mapped should be lists, and the mapping is to be applied to their individual elements. If False, mapping is only applied to a field containing a single value.

Examples:

MapInstanceValues(mappers={"a": {"1": "hi", "2": "bye"}}) replaces "1" with "hi" and "2" with "bye" in field "a" in all instances of all streams: instance {"a": 1, "b": 2} becomes {"a": "hi", "b": 2}. Note that the value of "b" remained intact, since field-name "b" does not participate in the mappers, and that 1 was casted to "1" before looked up in the mapper of "a".

MapInstanceValues(mappers={"a": {"1": "hi", "2": "bye"}}, process_every_value=True): Assuming field "a" is a list of values, potentially including "1"-s and "2"-s, this replaces each such "1" with "hi" and "2" – with "bye" in all instances of all streams: instance {"a": ["1", "2"], "b": 2} becomes {"a": ["hi", "bye"], "b": 2}.

MapInstanceValues(mappers={"a": {"1": "hi", "2": "bye"}}, strict=True): To ensure that all values of field "a" are mapped in every instance, use strict=True. Input instance {"a":"3", "b": 2} will raise an exception per the above call, because "3" is not a key in the mapper of "a".

MapInstanceValues(mappers={"a": {str([1,2,3,4]): "All", str([]): "None"}}, strict=True) replaces a list [1,2,3,4] with the string "All" and an empty list by string "None".

Explanation about SetΒΆ

Sets specified fields in each instance, in a given stream or all streams (default), with specified values. If fields exist, updates them, if do not exist – adds them.

Args:

fields (Dict[str, object]): The fields to add to each instance. Use β€˜/’ to access inner fields

use_deepcopy (bool) : Deep copy the input value to avoid later modifications

Examples:

# Set a value of a list consisting of β€œpositive” and β€œnegative” do field β€œclasses” to each and every instance of all streams Set(fields={"classes": ["positive","negatives"]})

# In each and every instance of all streams, field β€œspan” is to become a dictionary containing a field β€œstart”, in which the value 0 is to be set Set(fields={"span/start": 0}

# In all instances of stream β€œtrain” only, Set field β€œclasses” to have the value of a list consisting of β€œpositive” and β€œnegative” Set(fields={"classes": ["positive","negatives"], apply_to_stream=["train"]})

# Set field β€œclasses” to have the value of a given list, preventing modification of original list from changing the instance. Set(fields={"classes": alist}), use_deepcopy=True) if now alist is modified, still the instances remain intact.

Explanation about LoadFromKaggleΒΆ

Loads datasets from Kaggle.

Requires Kaggle API credentials and does not support streaming.

Args:

url: URL to the Kaggle dataset.

Example:

Loading from kaggle

load_kaggle = LoadFromKaggle(url='kaggle.com/dataset/example')

Explanation about RenameΒΆ

Renames fields.

Move value from one field to another, potentially, if field name contains a /, from one branch into another. Remove the from field, potentially part of it in case of / in from_field.

Examples:

Rename(field_to_field={β€œb”: β€œc”}) will change inputs [{β€œa”: 1, β€œb”: 2}, {β€œa”: 2, β€œb”: 3}] to [{β€œa”: 1, β€œc”: 2}, {β€œa”: 2, β€œc”: 3}]

Rename(field_to_field={β€œb”: β€œc/d”}) will change inputs [{β€œa”: 1, β€œb”: 2}, {β€œa”: 2, β€œb”: 3}] to [{β€œa”: 1, β€œc”: {β€œd”: 2}}, {β€œa”: 2, β€œc”: {β€œd”: 3}}]

Rename(field_to_field={β€œb”: β€œb/d”}) will change inputs [{β€œa”: 1, β€œb”: 2}, {β€œa”: 2, β€œb”: 3}] to [{β€œa”: 1, β€œb”: {β€œd”: 2}}, {β€œa”: 2, β€œb”: {β€œd”: 3}}]

Rename(field_to_field={β€œb/c/e”: β€œb/d”}) will change inputs [{β€œa”: 1, β€œb”: {β€œc”: {β€œe”: 2, β€œf”: 20}}}] to [{β€œa”: 1, β€œb”: {β€œc”: {β€œf”: 20}, β€œd”: 2}}]

Explanation about SplitRandomMixΒΆ

Splits a multistream into new streams (splits), whose names, source input stream, and amount of instances, are specified by arg β€˜mix’.

The keys of arg β€˜mix’, are the names of the new streams, the values are of the form: β€˜name-of-source-stream[percentage-of-source-stream]’ Each input instance, of any input stream, is selected exactly once for inclusion in any of the output streams.

Examples: When processing a multistream made of two streams whose names are β€˜train’ and β€˜test’, by SplitRandomMix(mix = { β€œtrain”: β€œtrain[99%]”, β€œvalidation”: β€œtrain[1%]”, β€œtest”: β€œtest” }) the output is a multistream, whose three streams are named β€˜train’, β€˜validation’, and β€˜test’. Output stream β€˜train’ is made of randomly selected 99% of the instances of input stream β€˜train’, output stream β€˜validation’ is made of the remaining 1% instances of input β€˜train’, and output stream β€˜test’ is made of the whole of input stream β€˜test’.

When processing the above input multistream by SplitRandomMix(mix = { β€œtrain”: β€œtrain[50%]+test[0.1]”, β€œvalidation”: β€œtrain[50%]+test[0.2]”, β€œtest”: β€œtest[0.7]” }) the output is a multistream, whose three streams are named β€˜train’, β€˜validation’, and β€˜test’. Output stream β€˜train’ is made of randomly selected 50% of the instances of input stream β€˜train’ + randomly selected 0.1 (i.e., 10%) of the instances of input stream β€˜test’. Output stream β€˜validation’ is made of the remaining 50% instances of input β€˜train’+ randomly selected 0.2 (i.e., 20%) of the original instances of input β€˜test’, that were not selected for output β€˜train’, and output stream β€˜test’ is made of the remaining instances of input β€˜test’.

Explanation about SerializeTableRowAsTextΒΆ

Serializes a table row as text.

Args:

fields (str) - list of fields to be included in serialization. to_field (str) - serialized text field name. max_cell_length (int) - limits cell length to be considered, optional.

References: templates.classification.multi_class.all, tasks.classification.multi_class

Read more about catalog usage here.