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

Initialize PySpark API #7609

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions jvm-packages/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
spark-xgboost is a xgboost4j-spark/xgboost4j-spark-gpu scala wrapper which
can make xgboost run on PySpark env.
Empty file.
Empty file.
Empty file.
Empty file.
22 changes: 22 additions & 0 deletions jvm-packages/python/ml/dmlc/xgboost4j/scala/spark/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#
# Copyright (c) 2022 by Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from ml.dmlc.xgboost4j.scala.spark.xgboost import XGBoostClassifier, XGBoostRegressor, XGBoostClassificationModel, \
XGBoostRegressionModel

__all__ = ["XGBoostClassifier", "XGBoostRegressor", "XGBoostClassificationModel", "XGBoostRegressionModel"]
__version__ = "1.6.0-SNAPSHOT"

93 changes: 93 additions & 0 deletions jvm-packages/python/ml/dmlc/xgboost4j/scala/spark/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#
# Copyright (c) 2022 by Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import re

from pyspark.ml.param import Params
from pyspark.ml.util import _jvm, JavaMLWritable
from pyspark.ml.wrapper import JavaModel, JavaEstimator

from ml.dmlc.xgboost4j.scala.spark.util import XGBoostReadable


class ParamGettersSetters(Params):
"""
Mixin class used to generate the setters/getters for all params.
"""

def _create_param_getters_and_setters(self):
for param in self.params:
param_name = param.name
fg_attr = "get" + re.sub(r"(?:^|_)(.)", lambda m: m.group(1).upper(), param_name)
fs_attr = "set" + re.sub(r"(?:^|_)(.)", lambda m: m.group(1).upper(), param_name)
# Generates getter and setter only if not exists
try:
getattr(self, fg_attr)
except AttributeError:
setattr(self, fg_attr, self._get_param_value(param_name))
try:
getattr(self, fs_attr)
except AttributeError:
setattr(self, fs_attr, self._set_param_value(param_name))

def _get_param_value(self, param_name):
def r():
try:
return self.getOrDefault(param_name)
except KeyError:
return None
return r

def _set_param_value(self, param_name):
def r(v):
self.set(self.getParam(param_name), v)
return self
return r


class XGboostEstimator(JavaEstimator, XGBoostReadable, JavaMLWritable, ParamGettersSetters):
"""
Mixin class for XGBoost estimators, like XGBoostClassifier and XGBoostRegressor.
"""

def __init__(self, classname):
super(XGboostEstimator, self).__init__()
self.__class__._java_class_name = classname
self._java_obj = self._new_java_obj(classname, self.uid)
self._create_params_from_java()
self._create_param_getters_and_setters()


def setFeaturesCols(self, features_cols):
"""
Sets the value of featuresCols which is used to GPU pipeline.
"""
self._java_obj.setFeaturesCols(_jvm().PythonUtils.toSeq(features_cols))
return self


class XGboostModel(JavaModel, XGBoostReadable, JavaMLWritable, ParamGettersSetters):
"""
Mixin class for XGBoost models, like XGBoostClassificationModel and XGBoostRegressionModel.
"""

def __init__(self, classname, java_model=None):
super(XGboostModel, self).__init__(java_model=java_model)
if classname and not java_model:
self.__class__._java_class_name = classname
self._java_obj = self._new_java_obj(classname, self.uid)
if java_model is not None:
self._transfer_params_from_java()
self._create_param_getters_and_setters()
40 changes: 40 additions & 0 deletions jvm-packages/python/ml/dmlc/xgboost4j/scala/spark/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# Copyright (c) 2022 by Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from pyspark.ml.util import JavaMLReadable, JavaMLReader


class XGBoostReadable(JavaMLReadable):
"""
Mixin class that provides a read() method for XGBoostReader.
"""

@classmethod
def read(cls):
"""Returns an XGBoostReader instance for this class."""
return XGBoostReader(cls)


class XGBoostReader(JavaMLReader):
"""
A reader mixin class for XGBoost objects.
"""

@classmethod
def _java_loader_class(cls, clazz):
if hasattr(clazz, '_java_class_name') and clazz._java_class_name is not None:
return clazz._java_class_name
else:
return JavaMLReader._java_loader_class(clazz)
Loading