示例#1
0
    def testQuantileRegression(self):
        learner_config = learner_pb2.LearnerConfig()
        learner_config.num_classes = 2
        learner_config.constraints.max_tree_depth = 3
        learner_config.growing_mode = learner_pb2.LearnerConfig.WHOLE_TREE
        learner_config.constraints.min_node_weight = 1 / _QUANTILE_REGRESSION_SIZE
        learner_config.regularization.l2 = 1.0 / _QUANTILE_REGRESSION_SIZE
        learner_config.regularization.l1 = 1.0 / _QUANTILE_REGRESSION_SIZE
        learner_config.regularization.tree_complexity = (
            1.0 / _QUANTILE_REGRESSION_SIZE)

        train_input_fn, test_input_fn, y = _quantile_regression_input_fns()

        # 95% percentile.
        model_upper = estimator.GradientBoostedDecisionTreeQuantileRegressor(
            quantiles=[0.95],
            learner_config=learner_config,
            num_trees=100,
            examples_per_layer=_QUANTILE_REGRESSION_SIZE,
            center_bias=False)

        model_upper.fit(input_fn=train_input_fn, steps=1000)
        result_iter = model_upper.predict(input_fn=test_input_fn)
        upper = []
        for prediction_dict in result_iter:
            upper.append(prediction_dict["scores"])

        frac_below_upper = round(1. * np.count_nonzero(upper > y) / len(y), 3)
        # +/- 3%
        self.assertTrue(frac_below_upper >= 0.92)
        self.assertTrue(frac_below_upper <= 0.98)

        train_input_fn, test_input_fn, _ = _quantile_regression_input_fns()
        model_lower = estimator.GradientBoostedDecisionTreeQuantileRegressor(
            quantiles=[0.05],
            learner_config=learner_config,
            num_trees=100,
            examples_per_layer=_QUANTILE_REGRESSION_SIZE,
            center_bias=False)

        model_lower.fit(input_fn=train_input_fn, steps=1000)
        result_iter = model_lower.predict(input_fn=test_input_fn)
        lower = []
        for prediction_dict in result_iter:
            lower.append(prediction_dict["scores"])

        frac_above_lower = round(1. * np.count_nonzero(lower < y) / len(y), 3)
        # +/- 3%
        self.assertTrue(frac_above_lower >= 0.92)
        self.assertTrue(frac_above_lower <= 0.98)
示例#2
0
    def testQuantileRegressionMultiDimLabel(self):
        learner_config = learner_pb2.LearnerConfig()
        learner_config.num_classes = 2
        learner_config.constraints.max_tree_depth = 6
        learner_config.growing_mode = learner_pb2.LearnerConfig.LAYER_BY_LAYER
        learner_config.constraints.min_node_weight = 1 / _QUANTILE_REGRESSION_SIZE
        learner_config.regularization.l2 = 1.0 / _QUANTILE_REGRESSION_SIZE
        learner_config.regularization.l1 = 1.0 / _QUANTILE_REGRESSION_SIZE
        learner_config.regularization.tree_complexity = (
            1.0 / _QUANTILE_REGRESSION_SIZE)

        train_input_fn, test_input_fn, y = _quantile_regression_input_fns(
            two_dimension=True)

        # 95% percentile.
        model_upper = estimator.GradientBoostedDecisionTreeQuantileRegressor(
            quantiles=[0.95],
            learner_config=learner_config,
            label_dimension=2,
            num_trees=18,
            examples_per_layer=_QUANTILE_REGRESSION_SIZE,
            center_bias=False)

        model_upper.fit(input_fn=train_input_fn, steps=1000)
        result_iter = model_upper.predict(input_fn=test_input_fn)
        upper = []
        for prediction_dict in result_iter:
            upper.append(prediction_dict["scores"])

        count_below_upper = np.count_nonzero(upper > y, axis=0)
        count_both_below_upper = np.count_nonzero(np.prod(upper > y, axis=1))
        frac_below_upper_0 = round(1. * count_below_upper[0] / len(y), 3)
        frac_below_upper_1 = round(1. * count_below_upper[1] / len(y), 3)
        frac_both_below_upper = round(1. * count_both_below_upper / len(y), 3)
        # +/- 3%
        self.assertTrue(frac_below_upper_0 >= 0.92)
        self.assertTrue(frac_below_upper_0 <= 0.98)
        self.assertTrue(frac_below_upper_1 >= 0.92)
        self.assertTrue(frac_below_upper_1 <= 0.98)
        self.assertTrue(frac_both_below_upper >= 0.91)
        self.assertTrue(frac_both_below_upper <= 0.99)