Calmcode - human learn: fallback


1 2 3 4 5 6 7 8

In machine learning you typically want to do more than just apply a model. It's likely that you'll also want to have a fallback ready in situations where your machine learning model isn't confident.

The diagram in the video can easily be constructed using a FunctionClassifier. A pseudo-code implementation is listed below.

import numpy as np
from hulearn.classification import FunctionClassifier

# These two models are assumed to be trained beforehand.
outlier    = WhatEverOutlierDetector().fit(X, y)
classifier = WhatEverClassifier().fit(X, y)

def make_decision(dataf, proba_threshold=0.8):
    # First we create a resulting array with all the predictions
    res = classifier.predict(dataf)

    # If we detect doubt, "classify" it as a fallback instead.
    proba = classifier.predict_proba(dataf)
    res = np.where(proba.max(axis=1) < proba_threshold, "doubt_fallback", res)

    # If we detect an outlier, we'll fallback too.
    res = np.where(outlier.predict(dataf) == -1, "outlier_fallback", res)

    # This `res` array contains the output of the drawn diagram.
    return res

fallback_model = FunctionClassifier(make_decision, proba_threshold=0.8)