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

[pyspark] rework transform to reuse same code #9292

Merged
merged 3 commits into from
Sep 4, 2023

Conversation

wbo4958
Copy link
Contributor

@wbo4958 wbo4958 commented Jun 12, 2023

To fix #9170

@wbo4958
Copy link
Contributor Author

wbo4958 commented Jun 12, 2023

@WeichenXu123 @trivialfis could you please help to review this PR? Previously, SparkXGBClassifier totally overrides the whole _transform function which causes duplicated common code, so this PR tries to unify them.

Copy link
Member

@trivialfis trivialfis left a comment

Choose a reason for hiding this comment

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

Does the refactor make the code cleaner or easier to understand? I find it quite hacky, but might be a general issue with PySpark-based libraries.

python-package/xgboost/spark/core.py Outdated Show resolved Hide resolved
result = data[pred.prediction]
if pred_contrib_col_name:
contribs = pred_contribs(model, X, base_margin)
data[pred.pred_contrib] = pd.Series(list(contribs))
Copy link
Member

Choose a reason for hiding this comment

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

Not sure how it works. Wouldn't this be super slow?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

any thoughts to rework this? using contribs.tolist() ?

python-package/xgboost/spark/core.py Outdated Show resolved Hide resolved
def _post_transform(self, dataset: DataFrame, pred_col: Column) -> DataFrame:
"""Post process of transform"""
prediction_col_name = self.getOrDefault(self.predictionCol)
single_pred = "," not in self._out_schema()
Copy link
Member

Choose a reason for hiding this comment

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

That's a bit, hmm, unconventional. Is there a way to refactor this to make it more "standard"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The _out_schema has typing hint with str returned which is a DDL formatted string, so if there're many columns, it must have at least a ",". So it's conventional when we check if it is a single column according to if "," is in the schema.

Copy link
Member

Choose a reason for hiding this comment

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

We are meta-programming by manipulating strings here.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe just use the pred_contrib_col_name as a predicate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed this issue.

python-package/xgboost/spark/core.py Show resolved Hide resolved
pred_contrib_col_name = self._get_pred_contrib_col_name()

def _predict(
model: XGBModel, X: ArrayLike, base_margin: Optional[np.ndarray]
Copy link
Member

Choose a reason for hiding this comment

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

base_margin is not necessarily np.ndarray. Let's stick with ArrayLike.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

def _post_transform(self, dataset: DataFrame, pred_col: Column) -> DataFrame:
"""Post process of transform"""
prediction_col_name = self.getOrDefault(self.predictionCol)
single_pred = "," not in self._out_schema()
Copy link
Member

Choose a reason for hiding this comment

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

Maybe just use the pred_contrib_col_name as a predicate?

@trivialfis trivialfis merged commit 419e052 into dmlc:master Sep 4, 2023
trivialfis pushed a commit to trivialfis/xgboost that referenced this pull request Sep 7, 2023
trivialfis added a commit that referenced this pull request Sep 7, 2023
@wbo4958 wbo4958 deleted the rework-transform branch April 23, 2024 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[pyspark] unify transform for classification and core framework.
2 participants