Beispiel #1
0
def test_r2_sklearn_comparison(multioutput):
    """Test against sklearn's implementation on random inputs."""
    for _ in range(10):
        actuals = np.random.rand(64, 3)
        preds = np.random.rand(64, 3)
        sample_weight = np.random.rand(64, 1)
        tensor_actuals = tf.constant(actuals, dtype=tf.float32)
        tensor_preds = tf.constant(preds, dtype=tf.float32)
        tensor_sample_weight = tf.constant(sample_weight, dtype=tf.float32)
        tensor_actuals = tf.cast(tensor_actuals, dtype=tf.float32)
        tensor_preds = tf.cast(tensor_preds, dtype=tf.float32)
        tensor_sample_weight = tf.cast(tensor_sample_weight, dtype=tf.float32)
        # Initialize
        r2_obj = initialize_vars(y_shape=(3, ), multioutput=multioutput)
        # Update
        update_obj_states(
            r2_obj,
            tensor_actuals,
            tensor_preds,
            sample_weight=tensor_sample_weight,
        )
        # Check results by comparing to results of scikit-learn r2 implementation
        sklearn_result = sklearn_r2_score(actuals,
                                          preds,
                                          sample_weight=sample_weight,
                                          multioutput=multioutput)
        check_results(r2_obj, sklearn_result)
Beispiel #2
0
def test_r2_sklearn_comparison():
    """Test that RSquare behaves similarly to the scikit-learn
    implementation of the same metric, given random input.
    """
    for multioutput in VALID_MULTIOUTPUT:
        for _ in range(10):
            actuals = np.random.rand(64, 3)
            preds = np.random.rand(64, 3)
            sample_weight = np.random.rand(64, 1)
            tensor_actuals = tf.constant(actuals, dtype=tf.float32)
            tensor_preds = tf.constant(preds, dtype=tf.float32)
            tensor_sample_weight = tf.constant(sample_weight, dtype=tf.float32)
            tensor_actuals = tf.cast(tensor_actuals, dtype=tf.float32)
            tensor_preds = tf.cast(tensor_preds, dtype=tf.float32)
            tensor_sample_weight = tf.cast(tensor_sample_weight,
                                           dtype=tf.float32)
            # Initialize
            r2_obj = initialize_vars(y_shape=(3, ), multioutput=multioutput)
            # Update
            update_obj_states(
                r2_obj,
                tensor_actuals,
                tensor_preds,
                sample_weight=tensor_sample_weight,
            )
            # Check results by comparing to results of scikit-learn r2 implementation
            sklearn_result = sklearn_r2_score(actuals,
                                              preds,
                                              sample_weight=sample_weight,
                                              multioutput=multioutput)
            check_results(r2_obj, sklearn_result)
Beispiel #3
0
 def test_r2_sklearn_comparison(self):
     """Test that RSquare behaves similarly to the scikit-learn
     implementation of the same metric, given random input.
     """
     for i in range(10):
         actuals = np.random.rand(64, 1)
         preds = np.random.rand(64, 1)
         sample_weight = np.random.rand(64, 1)
         tensor_actuals = tf.constant(actuals, dtype=tf.float32)
         tensor_preds = tf.constant(preds, dtype=tf.float32)
         tensor_sample_weight = tf.constant(sample_weight, dtype=tf.float32)
         tensor_actuals = tf.cast(tensor_actuals, dtype=tf.float32)
         tensor_preds = tf.cast(tensor_preds, dtype=tf.float32)
         tensor_sample_weight = tf.cast(tensor_sample_weight,
                                        dtype=tf.float32)
         # Initialize
         r2_obj = self.initialize_vars()
         # Update
         self.update_obj_states(r2_obj,
                                tensor_actuals,
                                tensor_preds,
                                sample_weight=tensor_sample_weight)
         # Check results by comparing to results of scikit-learn r2 implementation
         sklearn_result = sklearn_r2_score(actuals,
                                           preds,
                                           sample_weight=sample_weight)
         self.check_results(r2_obj, sklearn_result)
def r2_score(model_output: Union[torch.Tensor, np.ndarray], label: Union[torch.Tensor, np.ndarray]) \
        -> float:
    """
    Computes the coefficient of determination R2. Represents the proportion of variance explained
    by the (independent) variables in the model. R2 = 1 - Mean(SquaredErrors) / Variance(Labels)
    """
    if isinstance(label, torch.Tensor):
        label = label.detach().cpu().numpy()
    if isinstance(model_output, torch.Tensor):
        model_output = model_output.detach().cpu().numpy()
    return sklearn_r2_score(label, model_output)
Beispiel #5
0
    def scorer(y_true, y_pred, **kwargs) -> float:
        """R^2 score based on true and predicted target values.

        Parameters
        ----------
        y_true : array-like
            True labels.
        y_pred : array-like
            Predicted labels.

        Returns
        -------
        score
            float
        """
        return sklearn_r2_score(y_true, y_pred, **kwargs)
Beispiel #6
0
def test_kerasregressor_r2():
    """Test custom R^2 implementation against scikit-learn's."""
    n_samples = 50

    datasets = []
    y_true = np.arange(n_samples, dtype=float)
    y_pred = y_true + 1
    datasets.append((y_true.reshape(-1, 1), y_pred.reshape(-1, 1)))
    y_true = np.random.random_sample(size=y_true.shape)
    y_pred = np.random.random_sample(size=y_true.shape)
    datasets.append((y_true.reshape(-1, 1), y_pred.reshape(-1, 1)))

    def keras_backend_r2(y_true, y_pred):
        """Wrap Keras operations to numpy."""
        y_true = convert_to_tensor(y_true)
        y_pred = convert_to_tensor(y_pred)
        return KerasRegressor.r_squared(y_true, y_pred).numpy()

    for (y_true, y_pred) in datasets:
        np.testing.assert_almost_equal(
            keras_backend_r2(y_true, y_pred),
            sklearn_r2_score(y_true, y_pred),
            decimal=5,
        )
Beispiel #7
0
def r2_score(y_true, y_pred):
    from sklearn.metrics import r2_score as sklearn_r2_score
    y_true = convert_to_numpy(y_true)
    y_pred = convert_to_numpy(y_pred)
    return sklearn_r2_score(y_true, y_pred)