nannyml.metadata.extraction module

Module containing functionality to extract metadata for a given model type from a data sample.

class nannyml.metadata.extraction.ModelMetadataFactory[source]

Bases: object

A factory class that aids in the construction of ModelMetadata subclasses.

classmethod create(model_type: nannyml.metadata.base.ModelType, **kwargs)[source]

Creates a new ModelMetadata subclass instance for a given model type.

Parameters
  • model_type (ModelType) – The type of model NannyML should try to extract metadata for. This type will determine the properties NannyML will look for in the data sample.

  • kwargs – Any optional keyword arguments to be passed along to the ModelMetadata constructor.

Returns

metadata – A new ModelMetadata subclass instance.

Return type

ModelMetadata

mapping = {ModelType.CLASSIFICATION_BINARY: <class 'nannyml.metadata.binary_classification.BinaryClassificationMetadata'>, ModelType.CLASSIFICATION_MULTICLASS: <class 'nannyml.metadata.multiclass_classification.MulticlassClassificationMetadata'>}
nannyml.metadata.extraction.extract_metadata(data: pandas.core.frame.DataFrame, model_type: str, model_name: Optional[str] = None, exclude_columns: Optional[List[str]] = None)[source]

Tries to extract model metadata from a given data set.

Manually constructing model metadata can be cumbersome, especially if you have hundreds of features. NannyML includes this helper function that tries to do the boring stuff for you using some simple rules.

By default, all columns in the given dataset are considered to be either model features or metadata. Use the exclude_columns parameter to prevent columns from being interpreted as metadata or features.

Parameters
  • data (DataFrame) – The dataset containing model inputs and outputs, enriched with the required metadata.

  • model_type (str) – The kind of model to extract metadata for. Should be one of “classification_binary” or “classification_multiclass”.

  • model_name (str) – A human-readable name for the model.

  • exclude_columns (List[str], default=None) – A list of column names that are to be skipped during metadata extraction, preventing them from being interpreted as either model metadata or model features.

Returns

metadata – A fully initialized ModelMetadata instance.

Return type

ModelMetadata

Examples

>>> from nannyml.datasets import load_synthetic_binary_classification_dataset
>>> from nannyml.metadata import extract_metadata
>>> ref_df, _, _ = load_synthetic_binary_classification_dataset()
>>> metadata = extract_metadata(ref_df, model_name='work_from_home')
>>> metadata.is_complete()
(False, ['target_column_name'])
>>> metadata.to_dict()
{'identifier_column_name': 'identifier',
 'timestamp_column_name': 'timestamp',
 'partition_column_name': 'partition',
 'target_column_name': None,
 'prediction_column_name': None,
 'predicted_probability_column_name': 'y_pred_proba',
 'features': [{'label': 'distance_from_office',
   'column_name': 'distance_from_office',
   'type': 'continuous',
   'description': 'extracted feature: distance_from_office'},
  {'label': 'salary_range',
   'column_name': 'salary_range',
   'type': 'categorical',
   'description': 'extracted feature: salary_range'},
  {'label': 'gas_price_per_litre',
   'column_name': 'gas_price_per_litre',
   'type': 'continuous',
   'description': 'extracted feature: gas_price_per_litre'},
  {'label': 'public_transportation_cost',
   'column_name': 'public_transportation_cost',
   'type': 'continuous',
   'description': 'extracted feature: public_transportation_cost'},
  {'label': 'wfh_prev_workday',
   'column_name': 'wfh_prev_workday',
   'type': 'categorical',
   'description': 'extracted feature: wfh_prev_workday'},
  {'label': 'workday',
   'column_name': 'workday',
   'type': 'categorical',
   'description': 'extracted feature: workday'},
  {'label': 'tenure',
   'column_name': 'tenure',
   'type': 'continuous',
   'description': 'extracted feature: tenure'},
  {'label': 'work_home_actual',
   'column_name': 'work_home_actual',
   'type': 'categorical',
   'description': 'extracted feature: work_home_actual'}]}

Notes

NannyML can only make educated guesses as to what kind of data lives where. When NannyML feels to unsure about a guess, it will not use it. Be sure to always review the results of this method for their correctness and completeness. Adjust and complete as you see fit.