class mlrun.artifacts.model.ModelArtifact(key=None, body=None, format=None, model_file=None, metrics=None, target_path=None, parameters=None, inputs=None, outputs=None, framework=None, algorithm=None, feature_vector=None, feature_weights=None, extra_data=None, model_dir=None, **kwargs)[source]#

Bases: Artifact

ML Model artifact

Store link to ML model file(s) along with the model metrics, parameters, schema, and stats

before_log()[source]#
property feature_stats#
property feature_vector#
property feature_weights#
infer_from_df(df, label_columns=None, with_stats=True, num_bins=None)[source]#

infer inputs, outputs, and stats from provided df (training set)

Parameters:
  • df -- dataframe to infer from

  • label_columns -- name of the label (target) column

  • with_stats -- infer statistics (min, max, .. histogram)

  • num_bins -- number of bins for histogram

property inputs: ObjectList#

input feature list

property is_dir#

this is a directory

kind = 'model'#
property metrics#
property model_file#
property model_target_file#
property outputs: ObjectList#

input feature list

property parameters#
property spec: ModelArtifactSpec#
upload(artifact_path: str | None = None)[source]#

internal, upload to target store :param artifact_path: required only for when generating target_path from artifact hash

class mlrun.artifacts.model.ModelArtifactSpec(src_path=None, target_path=None, viewer=None, is_inline=False, format=None, size=None, db_key=None, extra_data=None, body=None, model_file=None, metrics=None, paraemeters=None, inputs: list[mlrun.features.Feature] | None = None, outputs: list[mlrun.features.Feature] | None = None, framework=None, algorithm=None, feature_vector=None, feature_weights=None, feature_stats=None, model_target_file=None)[source]#

Bases: ArtifactSpec

property inputs: ObjectList#

input feature list

property outputs: ObjectList#

output feature list

mlrun.artifacts.model.get_model(model_dir, suffix='')[source]#

return model file, model spec object, and list of extra data items

this function will get the model file, metadata, and extra data the returned model file is always local, when using remote urls (such as v3io://, s3://, store://, ..) it will be copied locally.

returned extra data dict (of key, DataItem objects) allow reading additional model files/objects e.g. use DataItem.get() or .download(target) .as_df() to read

example:

model_file, model_artifact, extra_data = get_model(models_path, suffix=".pkl")
model = load(open(model_file, "rb"))
categories = extra_data["categories"].as_df()
Parameters:
  • model_dir -- model dir or artifact path (store://..) or DataItem

  • suffix -- model filename suffix (when using a dir)

Returns:

model filename, model artifact object, extra data dict

mlrun.artifacts.model.update_model(model_artifact, parameters: dict | None = None, metrics: dict | None = None, extra_data: dict | None = None, inputs: list[mlrun.features.Feature] | None = None, outputs: list[mlrun.features.Feature] | None = None, feature_vector: str | None = None, feature_weights: list | None = None, key_prefix: str = '', labels: dict | None = None, write_spec_copy=True, store_object: bool = True) ModelArtifact[source]#

Update model object attributes

this method will edit or add attributes to a model object

example:

update_model(
    model_path,
    metrics={"speed": 100},
    extra_data={"my_data": b"some text", "file": "s3://mybucket/.."},
)
Parameters:
  • model_artifact -- model artifact object or path (store://..) or DataItem

  • parameters -- parameters dict

  • metrics -- model metrics e.g. accuracy

  • extra_data -- extra data items key, value dict (value can be: path string | bytes | artifact)

  • inputs -- list of input features (feature vector schema)

  • outputs -- list of output features (output vector schema)

  • feature_vector -- feature store feature vector uri (store://feature-vectors/<project>/<name>[:tag])

  • feature_weights -- list of feature weights, one per input column

  • key_prefix -- key prefix to add to metrics and extra data items

  • labels -- metadata labels

  • write_spec_copy -- write a YAML copy of the spec to the target dir

  • store_object -- Whether to store the model artifact updated.