def test_loss_boundary(loss): """Test interval ranges of y_true and y_pred in losses.""" # make sure low and high are always within the interval, used for linspace if loss.is_multiclass: y_true = np.linspace(0, 9, num=10) else: low, high = _inclusive_low_high(loss.interval_y_true) y_true = np.linspace(low, high, num=10) # add boundaries if they are included if loss.interval_y_true.low_inclusive: y_true = np.r_[y_true, loss.interval_y_true.low] if loss.interval_y_true.high_inclusive: y_true = np.r_[y_true, loss.interval_y_true.high] assert loss.in_y_true_range(y_true) n = y_true.shape[0] low, high = _inclusive_low_high(loss.interval_y_pred) if loss.is_multiclass: y_pred = np.empty((n, 3)) y_pred[:, 0] = np.linspace(low, high, num=n) y_pred[:, 1] = 0.5 * (1 - y_pred[:, 0]) y_pred[:, 2] = 0.5 * (1 - y_pred[:, 0]) else: y_pred = np.linspace(low, high, num=n) assert loss.in_y_pred_range(y_pred) # calculating losses should not fail raw_prediction = loss.link.link(y_pred) loss.loss(y_true=y_true, raw_prediction=raw_prediction)
def random_y_true_raw_prediction(loss, n_samples, y_bound=(-100, 100), raw_bound=(-5, 5), seed=42): """Random generate y_true and raw_prediction in valid range.""" rng = np.random.RandomState(seed) if loss.is_multiclass: raw_prediction = np.empty((n_samples, loss.n_classes)) raw_prediction.flat[:] = rng.uniform( low=raw_bound[0], high=raw_bound[1], size=n_samples * loss.n_classes, ) y_true = np.arange(n_samples).astype(float) % loss.n_classes else: raw_prediction = rng.uniform(low=raw_bound[0], high=raw_bound[0], size=n_samples) # generate a y_true in valid range low, high = _inclusive_low_high(loss.interval_y_true) low = max(low, y_bound[0]) high = min(high, y_bound[1]) y_true = rng.uniform(low, high, size=n_samples) # set some values at special boundaries if loss.interval_y_true.low == 0 and loss.interval_y_true.low_inclusive: y_true[::(n_samples // 3)] = 0 if loss.interval_y_true.high == 1 and loss.interval_y_true.high_inclusive: y_true[1::(n_samples // 3)] = 1 return y_true, raw_prediction
def test_is_in_range(interval): # make sure low and high are always within the interval, used for linspace low, high = _inclusive_low_high(interval) x = np.linspace(low, high, num=10) assert interval.includes(x) # x contains lower bound assert interval.includes(np.r_[x, interval.low]) == interval.low_inclusive # x contains upper bound assert interval.includes(np.r_[x, interval.high]) == interval.high_inclusive # x contains upper and lower bound assert interval.includes(np.r_[x, interval.low, interval.high]) == ( interval.low_inclusive and interval.high_inclusive )