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] support a list of feature column names #8117

Merged
merged 20 commits into from Aug 8, 2022

Conversation

wbo4958
Copy link
Contributor

@wbo4958 wbo4958 commented Jul 26, 2022

This PR introduces a new Param storing a list of feature column names, so with it, users do not need to vectorize or create an array feature column beforehand, instead, they need to specify feature_col with a list of column names.

Eg,

feature_col_names = [x.name for x in raw_df.schema if x.name != label]
xgb = SparkXGBClassifier(features_col=feature_col_names, label_col=label)

why introduce this new parameter?

The internal of XGBoost is wrapping the feature columns into a vector or an array column on the JVM side, but it will unwrap the vector/array column into each feature column on the python side. So if we just pass each feature column, which can improve the performance.

But for now, there are some limitations that users must enable use_gpu when using this way and it may break some spark ml pipelines eg, OneVsRest

@wbo4958
Copy link
Contributor Author

wbo4958 commented Jul 26, 2022

@trivialfis @WeichenXu123 Could you help to review this PR? Thx

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.

Thank you for the PR. Could you please:

  • add tests
  • Add a demo or test that shows how to use it with other spark ml utilities.

@wbo4958
Copy link
Contributor Author

wbo4958 commented Jul 27, 2022

Thank you for the PR. Could you please:

  • add tests
  • Add a demo or test that shows how to use it with other spark ml utilities.

Done @trivialfis @WeichenXu123

for c in features_col_name:
if isinstance(dataset.schema[c].dataType, DoubleType):
feature_cols.append(col(c).cast(FloatType()).alias(c))
elif isinstance(dataset.schema[c].dataType, (FloatType, IntegralType)):
Copy link
Contributor

Choose a reason for hiding this comment

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

What's IntegralType type ? Do you mean IntegerType ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@trivialfis, could you correct me if xgboost supports IntegralType (Byte/Integer/Long/Short) ?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, sort of. XGBoost will convert the data to float internally.

Copy link
Member

Choose a reason for hiding this comment

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

BTW, XGBoost can convert it on the fly without creating a copy of data.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thx @trivialfis, Yeah, if the type is short/byte, it will reduce the size of the shuffle write.

@@ -341,6 +363,22 @@ def _validate_and_convert_feature_col_as_array_col(dataset, features_col_name):
return features_array_col


def _validate_and_convert_feature_col(
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the function being used ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thx, removed.

pandas_df_iter,
None,
dmatrix_kwargs,
pandas_df_iter, features_cols_names, dmatrix_kwargs
Copy link
Contributor

Choose a reason for hiding this comment

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

What if features_cols_names conflicts with label / weight / base_margin column name ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thx, I tried, it worked, no exception happened.

@trivialfis
Copy link
Member

trivialfis commented Jul 28, 2022

Could you please add a test for cross validation fro sparkml as well?
NVM. I don't think it's needed

@wbo4958
Copy link
Contributor Author

wbo4958 commented Aug 2, 2022

@trivialfis could you help to review again. Thx

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.

Thank you for the PR! Some questions in the comments.

python-package/xgboost/spark/core.py Show resolved Hide resolved
feature_col_names = self.getOrDefault(self.features_cols)
features_col = []
if (
len(feature_col_names)
Copy link
Member

Choose a reason for hiding this comment

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

This condition looks weird?

len(feature_col_names) > 0 >= len(...)

is it correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes. the pylint required this

Copy link
Member

Choose a reason for hiding this comment

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

all(c in dataset.columns for c in feature_col_names)

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, I use another set way to check.

select_cols = [features_array_col, label_col]
select_cols = [label_col]
features_cols_names = None
if len(self.getOrDefault(self.features_cols)):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if len(self.getOrDefault(self.features_cols)):
if self.getOrDefault(self.features_cols):

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, Thx

@trivialfis
Copy link
Member

note to myself: need to update the linter script.

Copy link
Contributor

@WeichenXu123 WeichenXu123 left a comment

Choose a reason for hiding this comment

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

LGTM, leave one question though.

@WeichenXu123
Copy link
Contributor

@trivialfis @wbo4958
I will file PR for #8108 once this PR merged.
And hope my next PR will go into xgboost 2.0 .
Thanks!

@trivialfis trivialfis merged commit 03cc3b3 into dmlc:master Aug 8, 2022
@wbo4958 wbo4958 deleted the features_cols branch August 8, 2022 09:07
@wbo4958
Copy link
Contributor Author

wbo4958 commented Aug 8, 2022

@trivialfis @WeichenXu123 Thx for reviewing.

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.

None yet

3 participants