phygnn.model_interfaces.base_model.ModelBase

class ModelBase(model, feature_names=None, label_names=None, norm_params=None, normalize=(True, False), one_hot_categories=None)[source]

Bases: ABC

Base Model Interface

Parameters:
  • model (OBJ) – Initialized model object

  • feature_names (list) – Ordered list of feature names.

  • label_names (list) – Ordered list of label (output) names.

  • norm_params (dict, optional) – Dictionary mapping feature and label names (keys) to normalization parameters (mean, stdev), by default None

  • normalize (bool | tuple, optional) – Boolean flag(s) as to whether features and labels should be normalized. Possible values: - True means normalize both - False means don’t normalize either - Tuple of flags (normalize_feature, normalize_label) by default True

  • one_hot_categories (dict, optional) – Features to one-hot encode using given categories, if None do not run one-hot encoding, by default None

Methods

dict_json_convert(inp)

Recursively convert numeric values in dict to work with json dump

get_mean(name)

Get feature | label mean

get_norm_params(names)

Get means and stdevs for given feature/label names

get_stdev(name)

Get feature | label stdev

make_one_hot_feature_names(feature_names, ...)

Update feature_names after one-hot encoding

normalize(data[, names])

Normalize given data

parse_features(features[, names])

Parse features - preprocessing of feature data before training or prediction.

parse_labels(labels[, names])

Parse labels and normalize if desired

predict(features[, table, parse_kwargs, ...])

Use model to predict label from given features

seed([s])

Set the random seed for reproducible results.

unnormalize(data[, names])

Un-normalize given data

Attributes

feature_dims

Number of features

feature_means

Feature means, used for (un)normalization

feature_names

List of the feature variable names.

feature_stdevs

Feature stdevs, used for (un)normalization

input_feature_names

Input feature names

label_dims

Number of labels

label_means

label means, used for (un)normalization

label_names

label variable names

label_stdevs

label stdevs, used for (un)normalization

means

Mapping feature/label names to the mean values for (un)normalization

model

Trained model

model_summary

Tensorflow model summary

normalization_parameters

Features and label (un)normalization parameters

normalize_features

Flag to normalize features

normalize_labels

Flag to normalize labels

one_hot_categories

categories to use for one-hot encoding

one_hot_feature_names

One-hot encoded feature names

one_hot_input_feature_names

Input feature names to be one-hot encoded

stdevs

Mapping feature/label names to the stdev values for (un)normalization

version_record

A record of important versions that this model was built with.

property version_record

A record of important versions that this model was built with.

Returns:

dict

property model_summary

Tensorflow model summary

Returns:

str

property normalize_features

Flag to normalize features

Returns:

bool

property feature_names

List of the feature variable names.

Returns:

list

property feature_dims

Number of features

Returns:

int

property normalize_labels

Flag to normalize labels

Returns:

bool

property label_names

label variable names

Returns:

list

property label_dims

Number of labels

Returns:

int

property normalization_parameters

Features and label (un)normalization parameters

Returns:

dict

property means

Mapping feature/label names to the mean values for (un)normalization

Returns:

dict

property stdevs

Mapping feature/label names to the stdev values for (un)normalization

Returns:

dict

property model

Trained model

Returns:

tensorflow.keras.models

property feature_means

Feature means, used for (un)normalization

Returns:

list

property feature_stdevs

Feature stdevs, used for (un)normalization

Returns:

list

property label_means

label means, used for (un)normalization

Returns:

list

property label_stdevs

label stdevs, used for (un)normalization

Returns:

list

property input_feature_names

Input feature names

Returns:

list

property one_hot_input_feature_names

Input feature names to be one-hot encoded

Returns:

list

property one_hot_feature_names

One-hot encoded feature names

Returns:

list

property one_hot_categories

categories to use for one-hot encoding

Returns:

dict

static dict_json_convert(inp)[source]

Recursively convert numeric values in dict to work with json dump

Parameters:

inp (dict) – Dictionary to convert.

Returns:

out (dict) – Copy of dict input with all nested numeric values converted to base python int or float and all arrays converted to lists.

static seed(s=0)[source]

Set the random seed for reproducible results. :Parameters: s (int) – Random number generator seed

static make_one_hot_feature_names(feature_names, one_hot_categories)[source]

Update feature_names after one-hot encoding

Parameters:
  • feature_names (list) – Input feature names

  • one_hot_categories (dict) – Features to one-hot encode using given categories

Returns:

one_hot_feature_names (list) – Updated list of feature names with one_hot categories

get_norm_params(names)[source]

Get means and stdevs for given feature/label names

Parameters:

names (list) – list of feature/label names to get normalization params for

Returns:

  • means (list) – List of means to use for (un)normalization

  • stdevs (list) – List of stdevs to use for (un)normalization

get_mean(name)[source]

Get feature | label mean

Parameters:

name (str) – feature | label name

Returns:

mean (float) – Mean value used for normalization

get_stdev(name)[source]

Get feature | label stdev

Parameters:

name (str) – feature | label name

Returns:

stdev (float) – Stdev value used for normalization

normalize(data, names=None)[source]

Normalize given data

Parameters:
  • data (dict | pandas.DataFrame | ndarray) – Data to normalize

  • names (list, optional) – List of data item names, needed to normalized ndarrays, by default None

Returns:

data (dict | pandas.DataFrame | ndarray) – Normalized data in same format as input

unnormalize(data, names=None)[source]

Un-normalize given data

Parameters:
  • data (dict | pandas.DataFrame | ndarray) – Data to un-normalize

  • names (list, optional) – List of data item names, needed to un-normalized ndarrays, by default None

Returns:

data (dict | pandas.DataFrame | ndarray) – Native data in same format as input

parse_features(features, names=None, **kwargs)[source]

Parse features - preprocessing of feature data before training or prediction. This will do one-hot encoding based on self.one_hot_categories, and feature normalization based on self.normalize_features

Parameters:
  • features (pandas.DataFrame | dict | ndarray) – Features to train on or predict from

  • names (list, optional) – List of feature names, by default None

  • kwargs (dict, optional) – kwargs for PreProcess.one_hot

Returns:

features (ndarray) – Parsed features array normalized and with str columns converted to one hot vectors if desired

parse_labels(labels, names=None)[source]

Parse labels and normalize if desired

Parameters:
  • labels (pandas.DataFrame | dict | ndarray) – Features to train on or predict from

  • names (list, optional) – List of label names, by default None

Returns:

labels (ndarray) – Parsed labels array, normalized if desired

predict(features, table=True, parse_kwargs=None, predict_kwargs=None)[source]

Use model to predict label from given features

Parameters:
  • features (dict | pandas.DataFrame) – features to predict from

  • table (bool, optional) – Return pandas DataFrame

  • parse_kwargs (dict) – kwargs for cls.parse_features

  • predict_kwargs (dict) – kwargs for tensorflow.*.predict

Returns:

prediction (ndarray | pandas.DataFrame) – label prediction