예제 #1
0
def init_od_mahalanobis(state_dict: Dict) -> Mahalanobis:
    """
    Initialize Mahalanobis.

    Parameters
    ----------
    state_dict
        Dictionary containing the parameter values.

    Returns
    -------
    Initialized Mahalanobis instance.
    """
    od = Mahalanobis(threshold=state_dict['threshold'],
                     n_components=state_dict['n_components'],
                     std_clip=state_dict['std_clip'],
                     start_clip=state_dict['start_clip'],
                     max_n=state_dict['max_n'],
                     cat_vars=state_dict['cat_vars'],
                     ohe=state_dict['ohe'])
    od.d_abs = state_dict['d_abs']
    od.clip = state_dict['clip']
    od.mean = state_dict['mean']
    od.C = state_dict['C']
    od.n = state_dict['n']
    return od
예제 #2
0
def test_mahalanobis(mahalanobis_params):
    threshold, n_components, std_clip, start_clip, max_n, \
        threshold_perc, return_instance_score = mahalanobis_params
    X, y = load_iris(return_X_y=True)
    mh = Mahalanobis(threshold,
                     n_components=n_components,
                     std_clip=std_clip,
                     start_clip=start_clip,
                     max_n=max_n)
    assert mh.threshold == threshold
    assert mh.n == 0
    assert mh.meta == {
        'name': 'Mahalanobis',
        'detector_type': 'online',
        'data_type': 'tabular',
        'version': __version__
    }
    mh.infer_threshold(X, threshold_perc=threshold_perc)
    assert mh.n == X.shape[0]
    iscore = mh.score(X)  # noqa
    assert mh.n == 2 * X.shape[0]
    assert mh.mean.shape[0] == X.shape[1]
    assert mh.C.shape == (X.shape[1], X.shape[1])
    assert (np.diag(mh.C) >= 0).all()
    od_preds = mh.predict(X, return_instance_score=return_instance_score)
    assert mh.n == 3 * X.shape[0]
    assert od_preds['meta'] == mh.meta
    if return_instance_score:
        assert od_preds['data']['is_outlier'].sum() == (
            od_preds['data']['instance_score'] >
            mh.threshold).astype(int).sum()
    else:
        assert od_preds['data']['instance_score'] is None
예제 #3
0
])

threshold_net = tf.keras.Sequential([
    InputLayer(input_shape=(seq_len, latent_dim)),
    Dense(5, activation=tf.nn.relu)
])

# define model
inputs = tf.keras.Input(shape=(input_dim, ))
outputs = tf.keras.layers.Dense(2, activation=tf.nn.softmax)(inputs)
model = tf.keras.Model(inputs=inputs, outputs=outputs)

detector = [
    AdversarialAE(threshold=threshold, model=model, **kwargs),
    IForest(threshold=threshold),
    Mahalanobis(threshold=threshold),
    OutlierAEGMM(threshold=threshold,
                 gmm_density_net=gmm_density_net,
                 n_gmm=n_gmm,
                 **kwargs),
    OutlierVAE(threshold=threshold,
               latent_dim=latent_dim,
               samples=samples,
               **kwargs),
    OutlierAE(threshold=threshold, **kwargs),
    OutlierVAEGMM(threshold=threshold,
                  gmm_density_net=gmm_density_net,
                  n_gmm=n_gmm,
                  latent_dim=latent_dim,
                  samples=samples,
                  **kwargs),