Beispiel #1
0
def _get_glm_coeffs(glm):
    """
    Get the GLM coefficients by choosing the lambda with diminishing returns on explained deviance
    """
    r = H2OGeneralizedLinearEstimator.getGLMRegularizationPath(glm)
    deviance = r.get('explained_deviance_train')
    inflection_pt = [i*3 for i, x in enumerate(np.diff(np.sign(np.diff(deviance, 2)))) if x != 0 and i > 0][0]
    intercept = {k: v for k,v in r.get('coefficients')[inflection_pt].items() if  k == "Intercept"}
    coeffs = {k: v for k,v in r.get('coefficients')[inflection_pt].items() if abs(v) > 0 and k != "Intercept"}
    return intercept, coeffs
Beispiel #2
0
def _get_glm_lambda(glm):
    """
    Get the best GLM lambda by choosing one diminishing returns on explained deviance
    """
    r = H2OGeneralizedLinearEstimator.getGLMRegularizationPath(glm)
    deviance = r.get('explained_deviance_train')
    rule_count = [len([k for k,v in x.items() if abs(v) > 0 and k != "Intercept"]) for x in r.get('coefficients')]
    lambda_index = [i*3 for i, x in enumerate(np.diff(np.sign(np.diff(deviance, 2)))) if x != 0 and i > 0][0]
        
    return r.get('lambdas')[lambda_index]
Beispiel #3
0
def _get_glm_lambda(glm):
    """
    Get the best GLM lambda by choosing one diminishing returns on explained deviance
    """
    r = H2OGeneralizedLinearEstimator.getGLMRegularizationPath(glm)
    deviance = r.get('explained_deviance_train')
    if len(deviance) < 5:
        lambda_index = len(deviance) - 1
    else:
        lambda_index = [
            i * 3 for i, x in enumerate(np.diff(np.sign(np.diff(deviance, 2))))
            if x != 0 and i > 0
        ][0]

    return r.get('lambdas')[lambda_index]
Beispiel #4
0
def _get_glm_lambda(glm, num_rules):
    """
    Get the best GLM lambda by choosing one diminishing returns on explained deviance
    :param num_rules: The number of rules to use in rulefit model.
    """
    r = H2OGeneralizedLinearEstimator.getGLMRegularizationPath(glm)
    deviance = r.get('explained_deviance_train')
    rule_count = [
        len([k for k, v in x.items() if abs(v) > 0 and k != "Intercept"])
        for x in r.get('coefficients')
    ]
    if num_rules is None:
        lambda_index = [
            i * 3 for i, x in enumerate(np.diff(np.sign(np.diff(deviance, 2))))
            if x != 0 and i > 0
        ][0]

    else:
        lambda_index = [
            x for x, val in enumerate(rule_count) if val > num_rules
        ][0]

    return r.get('lambdas')[lambda_index]