From 2deb260cad6355aa4ee3482b9a686bbcbe84be88 Mon Sep 17 00:00:00 2001 From: DRudel Date: Tue, 12 Jun 2018 22:31:59 -0600 Subject: [PATCH 1/2] Modified score_samples to keep predictions and actual labels the same shape with unidimensional outputs. Replace denominator of scoring function with np.var(y). This means samples with equal error will have equal score. It also allows the average value of the samples to equal the r2_score for the entire set in the case of unidimensional data. --- pyearth/earth.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyearth/earth.py b/pyearth/earth.py index e027d6a..ced9dad 100644 --- a/pyearth/earth.py +++ b/pyearth/earth.py @@ -1314,7 +1314,9 @@ def score_samples(self, X, y=None, missing=None): X, y, sample_weight, output_weight, missing = self._scrub( X, y, None, None, missing) y_hat = self.predict(X, missing=missing) - residual = 1 - (y - y_hat) ** 2 / y**2 + if y_hat.ndim == 1: + y_hat = y_hat.reshape(-1, 1) + residual = 1 - (y - y_hat) ** 2 / np.var(y) return residual def transform(self, X, missing=None): From 15faf8262642c8e2a2c696a6f62f17c17acc155c Mon Sep 17 00:00:00 2001 From: DRudel Date: Tue, 12 Jun 2018 23:58:39 -0600 Subject: [PATCH 2/2] Modified score_samples to keep predictions and actual labels the same shape with unidimensional outputs. Replace denominator of scoring function with np.var(y). This means samples with equal error will have equal score. It also allows the average value of the samples to equal the r2_score for the entire set in the case of unidimensional data. --- pyearth/earth.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pyearth/earth.py b/pyearth/earth.py index ced9dad..a8185df 100644 --- a/pyearth/earth.py +++ b/pyearth/earth.py @@ -1316,8 +1316,13 @@ def score_samples(self, X, y=None, missing=None): y_hat = self.predict(X, missing=missing) if y_hat.ndim == 1: y_hat = y_hat.reshape(-1, 1) - residual = 1 - (y - y_hat) ** 2 / np.var(y) - return residual + squared_errors = (y - y_hat) ** 2 + variances = np.var(y, axis=0).reshape(1, -1) + nze = variances != 0 # non-zero variance + nze = nze.ravel() + output = np.ones(squared_errors.shape) + output[:, nze] = 1 - squared_errors[:, nze] / variances[:, nze] + return output def transform(self, X, missing=None): '''