Last active
January 15, 2020 19:15
-
-
Save ianlcassidy/c889d0c8c8fed983dcf20ce8dd130639 to your computer and use it in GitHub Desktop.
An example of how to create a features container and model prediction pipeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from pydantic import ValidationError | |
from some.path.features import AnimalFeatures, FeatureIsNoneError | |
from some.other.path.models import ML_MODEL | |
from some.logger import log | |
def create_features() -> np.ndarray: | |
# instantiate the features container | |
features = AnimalFeatures() | |
# set the categorical feature values | |
# this sets cat_1 = 1 and cat_2 = 0 | |
features.set_categorical_features("cat", 1) | |
# set the continuous feature values | |
features.dog = 1.7 | |
features.bird = -0.2 | |
# or, alternatively | |
# features.set_bulk_features({"dog": 1.7, "bird": -0.2}) | |
# return the features as a numpy array | |
return features.numpy_array | |
def make_predictions() -> float: | |
# try to run the feature pipeline and get a prediction | |
try: | |
data = create_features() | |
prediction = ML_MODEL.predict(data) | |
# if one of the features container errors occur, set a default value and log a warning | |
except (ValidationError, FeatureIsNoneError) as e: | |
prediction = 0.0 | |
log.warn("there was a problem creating the features", exception=e) | |
return prediction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment