def predict_real(self, x_test, y_test, param, settings):
        pred_mean = np.zeros(x_test.shape[0])
        pred_second_moment = np.zeros(x_test.shape[0])
        pred_sample = np.zeros(x_test.shape[0])
        log_pred_prob = -np.inf * np.ones(x_test.shape[0])
        prob_not_separated_yet = np.ones(x_test.shape[0])
        prob_separated = np.zeros(x_test.shape[0])
        node_list = [self.root]
        d_idx_test = {self.root: np.arange(x_test.shape[0])}
        while True:
            try:
                node_id = node_list.pop(0)
            except IndexError:
                break
            idx_test = d_idx_test[node_id]
            if len(idx_test) == 0:
                continue
            x = x_test[idx_test, :]
            expo_parameter = np.maximum(0, node_id.min_d - x).sum(1) + np.maximum(0, x - node_id.max_d).sum(1)
            prob_not_separated_now = np.exp(-expo_parameter * self.max_split_costs[node_id])
            prob_separated_now = 1 - prob_not_separated_now
            if math.isinf(self.max_split_costs[node_id]):
                # rare scenario where test point overlaps exactly with a training data point
                idx_zero = expo_parameter == 0
                # to prevent nan in computation above when test point overlaps with training data point
                prob_not_separated_now[idx_zero] = 1.
                prob_separated_now[idx_zero] = 0.
            # predictions for idx_test_zero
            idx_non_zero = expo_parameter > 0
            idx_test_non_zero = idx_test[idx_non_zero]
            n_test_non_zero = len(idx_test_non_zero)
            expo_parameter_non_zero = expo_parameter[idx_non_zero]
            if np.any(idx_non_zero):
                # expected variance (averaging over time of cut which is a truncated exponential)
                # NOTE: expected variance is approximate since E[f(x)] not equal to f(E[x])
                expected_cut_time = 1.0 / expo_parameter_non_zero
                if not np.isinf(self.max_split_costs[node_id]):
                    tmp_exp_term_arg = -self.max_split_costs[node_id] * expo_parameter_non_zero
                    tmp_exp_term = np.exp(tmp_exp_term_arg)
                    expected_cut_time -= self.max_split_costs[node_id] * tmp_exp_term / (-np.expm1(tmp_exp_term_arg))
                try:
                    assert np.all(expected_cut_time >= 0.)
                except AssertionError:
                    print tmp_exp_term_arg
                    print tmp_exp_term
                    print expected_cut_time
                    print np.any(np.isnan(expected_cut_time))
                    print 1.0 / expo_parameter_non_zero
                    raise AssertionError
                if not settings.smooth_hierarchically:
                    pred_mean_tmp = self.sum_y[node_id] / float(self.n_points[node_id])
                    pred_second_moment_tmp = self.sum_y2[node_id] / float(self.n_points[node_id]) + param.noise_variance
                else:
                    pred_mean_tmp, pred_second_moment_tmp = self.pred_moments[node_id]
                    # FIXME: approximate since E[f(x)] not equal to f(E[x])
                    expected_split_time = expected_cut_time + self.get_parent_split_time(node_id, settings)
                    variance_from_mean = self.variance_coef * (sigmoid(self.sigmoid_coef * expected_split_time) \
                                        - sigmoid(self.sigmoid_coef * self.get_parent_split_time(node_id, settings)))
                    pred_second_moment_tmp += variance_from_mean
                pred_variance_tmp = pred_second_moment_tmp - pred_mean_tmp ** 2
                pred_sample_tmp = pred_mean_tmp + np.random.randn(n_test_non_zero) * np.sqrt(pred_variance_tmp)
                log_pred_prob_tmp = compute_gaussian_logpdf(pred_mean_tmp, pred_variance_tmp, y_test[idx_test_non_zero])
                prob_separated_now_weighted = \
                        prob_separated_now[idx_non_zero] * prob_not_separated_yet[idx_test_non_zero]
                pred_mean[idx_test_non_zero] += prob_separated_now_weighted * pred_mean_tmp
                pred_sample[idx_test_non_zero] += prob_separated_now_weighted * pred_sample_tmp
                pred_second_moment[idx_test_non_zero] += prob_separated_now_weighted * pred_second_moment_tmp
                log_pred_prob[idx_test_non_zero] = logsumexp_array(log_pred_prob[idx_test_non_zero], \
                                                    np.log(prob_separated_now_weighted) + log_pred_prob_tmp)
                prob_not_separated_yet[idx_test] *= prob_not_separated_now
            # predictions for idx_test_zero
            if math.isinf(self.max_split_costs[node_id]) and np.any(idx_zero):
                idx_test_zero = idx_test[idx_zero]
                n_test_zero = len(idx_test_zero)
                if not settings.smooth_hierarchically:
                    pred_mean_node_id = self.sum_y[node_id] / float(self.n_points[node_id])
                    pred_second_moment_node_id = self.sum_y2[node_id] / float(self.n_points[node_id]) \
                                                    + param.noise_variance
                else:
                    pred_mean_node_id, pred_second_moment_node_id = self.pred_moments[node_id]
                pred_variance_node_id = pred_second_moment_node_id - pred_mean_node_id ** 2
                pred_sample_node_id = pred_mean_node_id + np.random.randn(n_test_zero) * np.sqrt(pred_variance_node_id)
                log_pred_prob_node_id = \
                        compute_gaussian_logpdf(pred_mean_node_id, pred_variance_node_id, y_test[idx_test_zero])
                pred_mean[idx_test_zero] += prob_not_separated_yet[idx_test_zero] * pred_mean_node_id
                pred_sample[idx_test_zero] += prob_not_separated_yet[idx_test_zero] * pred_sample_node_id
                pred_second_moment[idx_test_zero] += prob_not_separated_yet[idx_test_zero] * pred_second_moment_node_id
                log_pred_prob[idx_test_zero] = logsumexp_array(log_pred_prob[idx_test_zero], \
                                                np.log(prob_not_separated_yet[idx_test_zero]) + log_pred_prob_node_id)
            try:
                feat_id, split = self.node_info[node_id]
                cond = x[:, feat_id] <= split
                left, right = get_children_id(node_id)
                d_idx_test[left], d_idx_test[right] = idx_test[cond], idx_test[~cond]
                node_list.append(left)
                node_list.append(right)
            except KeyError:
                pass
        pred_var = pred_second_moment - (pred_mean ** 2)
        if True or settings.debug:  # FIXME: remove later
            assert not np.any(np.isnan(pred_mean))
            assert not np.any(np.isnan(pred_var))
            try:
                assert np.all(pred_var >= 0.)
            except AssertionError:
                min_pred_var = np.min(pred_var)
                print 'min_pred_var = %s' % min_pred_var
                assert np.abs(min_pred_var) < 1e-3  # allowing some numerical errors
            assert not np.any(np.isnan(log_pred_prob))
        return (pred_mean, pred_var, pred_second_moment, log_pred_prob, pred_sample)
Ensure request.data returns empty QueryDict for HEAD request.
dataset/ETHPy150Open balajiln/mondrianforest/src/mondrianforest.py/MondrianTree.predict_real--crossover with--dataset/ETHPy150Open tomchristie/django-rest-framework/tests/test_request.py/TestContentParsing.test_standard_behaviour_determines_no_content_HEAD