Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use numpy divide in explained variance #1691

Merged
merged 3 commits into from
Mar 31, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion recommenders/evaluation/spark_evaluation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import numpy as np
try:
from pyspark.mllib.evaluation import RegressionMetrics, RankingMetrics
from pyspark.sql import Window, DataFrame
Expand Down Expand Up @@ -158,7 +159,8 @@ def exp_var(self):
0
]
var2 = self.y_pred_true.selectExpr("variance(label)").collect()[0][0]
return 1 - var1 / var2
# numpy divide is more tolerant to var2 being zero
return 1 - np.divide(var1, var2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm looking at the test here: https://github.com/microsoft/recommenders/blob/main/tests/unit/recommenders/evaluation/test_spark_evaluation.py#L239, should we maybe add the edge case for when var2 is zero?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, although it is a very unusual case in practice (everything in the data is rated equally).



class SparkRankingEvaluation:
Expand Down