Skip to content
This repository was archived by the owner on Dec 6, 2023. It is now read-only.

Commit 15faf82

Browse files
committed
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.
1 parent 2deb260 commit 15faf82

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

pyearth/earth.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,8 +1316,13 @@ def score_samples(self, X, y=None, missing=None):
13161316
y_hat = self.predict(X, missing=missing)
13171317
if y_hat.ndim == 1:
13181318
y_hat = y_hat.reshape(-1, 1)
1319-
residual = 1 - (y - y_hat) ** 2 / np.var(y)
1320-
return residual
1319+
squared_errors = (y - y_hat) ** 2
1320+
variances = np.var(y, axis=0).reshape(1, -1)
1321+
nze = variances != 0 # non-zero variance
1322+
nze = nze.ravel()
1323+
output = np.ones(squared_errors.shape)
1324+
output[:, nze] = 1 - squared_errors[:, nze] / variances[:, nze]
1325+
return output
13211326

13221327
def transform(self, X, missing=None):
13231328
'''

0 commit comments

Comments
 (0)