h1st.core.model module

class h1st.core.model.Model[source]

Bases: h1st.core.node_containable.NodeContainable

Base class for H1ST Model.

To create your own model, inherit Model class and implement prepare_data, train and predict accordingly. Please refer to Tutorial for more details how to create a model.

The framework allows you to persist and load model to the model repository. To persist the model, you can call persist(), and then load to retrieve the model. See persist() and load() document for more detail.

Model Persistence and Loading Example
import h1st as h1
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression

class MyModel(h1.Model):
    def __init__(self):
        super().__init__()
        self.model = LogisticRegression(random_state=0)

    def train(self, prepared_data):
        X, y = prepared_data['X'], prepared_data['y']
        self.model.fit(X, y)

my_model = MyModel()
X, y = load_iris(return_X_y=True)
prepared_data = {'X': X, 'y': y}
my_model.train(prepared_data)
# Persist the model to repo
my_model.persist('1st_version')

# Load the model from the repo
my_model_2 = MyModel()
my_model_2.load('1st_version')
load_data() → dict[source]

Implement logic of load data from data source

Returns

loaded data

prep(loaded_data: dict) → dict[source]

Implement logic to prepare data from loaded data

Parameters

data – loaded data from load_data method

Returns

prepared data

explore(loaded_data: dict) → None[source]

Implement logic to explore data from loaded data

train(prepared_data: dict)[source]

Implement logic of training model

Parameters

prepared_data – prepared data from prep method

persist(version=None)[source]

Persist this model’s properties to the ModelRepository. Currently, only stats, metrics, model properties are supported.

model property could be single model, list or dict of models Currently, only sklearn and tensorflow-keras are supported.

Parameters

version – model version, leave blank for autogeneration

Returns

model version

load(version: str = None)h1st.core.model.Model[source]

Load parameters from the specified version from the ModelRepository. Leave version blank to load latest version.

evaluate(data: dict) → dict[source]

Implement logic to evaluate the model using the loaded data

Parameters

data – loaded data

predict(input_data: dict) → dict[source]

Implement logic to generate prediction from data

Params data

data for prediction

Returns

prediction result as a dictionary

test_output(input_data: Any = None, schema=None)[source]

From NodeContainable. Validates the models’ prediction with a schema.:: :param input_data: input data for prediction, it can be anything. :param schema: target schema :return: validation result