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, model_url: str | None = None, default_config: dict | None = None, **kwargs)[source]#

Bases: Artifact

ML Model artifact

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

Parameters:
  • key -- Artifact key or artifact class ()

  • body -- Will use the body as the artifact content

  • format -- Optional, format to use (e.g. csv, parquet, ..)

  • model_file -- Path to the local model file we upload (see also model_dir) or to a model file data url (e.g. http://host/path/model.pkl)

  • metrics -- The key/value dict of model metrics

  • target_path -- Absolute target path (instead of using artifact_path + local_path)

  • parameters -- Key/value dict of model parameters

  • inputs -- Ordered list of model input features (name, type, ..)

  • outputs -- Ordered list of model output/result elements (name, type, ..)

  • framework -- Name of the ML framework

  • algorithm -- Training algorithm name

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

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

  • extra_data -- Extra artifacts and files to log with the model.

  • model_dir -- Path to the local dir holding the model file and extra files

  • model_url -- Remote model url.

  • default_config -- Default configuration for client building Saved as a sub-dictionary under the parameter.

  • kwargs -- Arguments to pass to the artifact class.

before_log()[source]#
property default_config#
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 model_url#
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[Feature] | None = None, outputs: list[Feature] | None = None, framework=None, algorithm=None, feature_vector=None, feature_weights=None, feature_stats=None, model_target_file=None, model_url=None)[source]#

Bases: ArtifactSpec

property default_config#
property inputs: ObjectList#

input feature list

property outputs: ObjectList#

output feature list

mlrun.artifacts.model.get_model(model_dir: str | ~mlrun.artifacts.model.ModelArtifact | ~mlrun.datastore.base.DataItem | None = None, suffix='') -> (<class 'str'>, <class 'mlrun.artifacts.model.ModelArtifact'>, <class 'dict'>)[source]#

Return model file, model spec object, and dictionary 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[Feature] | None = None, outputs: list[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.