-
Notifications
You must be signed in to change notification settings - Fork 30
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
Support gpu version crossvalidator for RandomForestRegressor #303
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
import pandas as pd | ||
from pyspark import Row, keyword_only | ||
from pyspark.ml.common import _py2java | ||
from pyspark.ml.evaluation import Evaluator, RegressionEvaluator | ||
from pyspark.ml.linalg import Vector, Vectors, _convert_to_vector | ||
from pyspark.ml.regression import LinearRegressionModel as SparkLinearRegressionModel | ||
from pyspark.ml.regression import LinearRegressionSummary | ||
|
@@ -56,9 +57,12 @@ | |
_CumlModelWithPredictionCol, | ||
_EvaluateFunc, | ||
_TransformFunc, | ||
alias, | ||
param_alias, | ||
pred, | ||
transform_evaluate, | ||
) | ||
from .metrics.RegressionMetrics import RegressionMetrics, reg_metrics | ||
from .params import HasFeaturesCols, P, _CumlClass, _CumlParams | ||
from .tree import ( | ||
_RandomForestClass, | ||
|
@@ -68,6 +72,9 @@ | |
) | ||
from .utils import PartitionDescriptor, _get_spark_session, cudf_to_cuml_array, java_uid | ||
|
||
if TYPE_CHECKING: | ||
from pyspark.ml._typing import ParamMap | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
|
@@ -784,6 +791,9 @@ def _is_classification(self) -> bool: | |
def _create_pyspark_model(self, result: Row) -> "RandomForestRegressionModel": | ||
return RandomForestRegressionModel.from_row(result) | ||
|
||
def _supportsTransformEvaluate(self, evaluator: Evaluator) -> bool: | ||
return True if isinstance(evaluator, RegressionEvaluator) else False | ||
|
||
|
||
class RandomForestRegressionModel( | ||
_RandomForestRegressorClass, | ||
|
@@ -833,3 +843,84 @@ def cpu(self) -> SparkRandomForestRegressionModel: | |
|
||
def _is_classification(self) -> bool: | ||
return False | ||
|
||
def _get_cuml_transform_func( | ||
self, dataset: DataFrame, category: str = transform_evaluate.transform | ||
) -> Tuple[_ConstructFunc, _TransformFunc, Optional[_EvaluateFunc],]: | ||
_construct_rf, _predict, _ = super()._get_cuml_transform_func(dataset, category) | ||
|
||
def _evaluate( | ||
input: TransformInputType, | ||
transformed: TransformInputType, | ||
) -> pd.DataFrame: | ||
# calculate the count of (label, prediction) | ||
comb = pd.DataFrame( | ||
{ | ||
"label": input[alias.label], | ||
"prediction": transformed, | ||
} | ||
) | ||
comb.insert(1, "label-prediction", comb["label"] - comb["prediction"]) | ||
total_cnt = comb.shape[0] | ||
return pd.DataFrame( | ||
data={ | ||
reg_metrics.mean: [comb.mean().to_list()], | ||
reg_metrics.m2n: [(comb.var(ddof=0) * total_cnt).to_list()], | ||
reg_metrics.m2: [comb.pow(2).sum().to_list()], | ||
reg_metrics.l1: [comb.abs().sum().to_list()], | ||
reg_metrics.total_count: total_cnt, | ||
} | ||
) | ||
|
||
return _construct_rf, _predict, _evaluate | ||
|
||
def _transformEvaluate( | ||
self, | ||
dataset: DataFrame, | ||
evaluator: Evaluator, | ||
params: Optional["ParamMap"] = None, | ||
) -> List[float]: | ||
""" | ||
Transforms and evaluates the input dataset with optional parameters in a single pass. | ||
|
||
Parameters | ||
---------- | ||
dataset : :py:class:`pyspark.sql.DataFrame` | ||
a dataset that contains labels/observations and predictions | ||
evaluator: :py:class:`pyspark.ml.evaluation.Evaluator` | ||
an evaluator user intends to use | ||
params : dict, optional | ||
an optional param map that overrides embedded params | ||
|
||
Returns | ||
------- | ||
float | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. list of float? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Done |
||
metric | ||
""" | ||
|
||
if not isinstance(evaluator, RegressionEvaluator): | ||
raise NotImplementedError(f"{evaluator} is unsupported yet.") | ||
|
||
if self.getLabelCol() not in dataset.schema.names: | ||
raise RuntimeError("Label column is not existing.") | ||
|
||
dataset = dataset.withColumnRenamed(self.getLabelCol(), alias.label) | ||
|
||
schema = StructType( | ||
[ | ||
StructField(pred.model_index, IntegerType()), | ||
StructField(reg_metrics.mean, ArrayType(FloatType())), | ||
StructField(reg_metrics.m2n, ArrayType(FloatType())), | ||
StructField(reg_metrics.m2, ArrayType(FloatType())), | ||
StructField(reg_metrics.l1, ArrayType(FloatType())), | ||
StructField(reg_metrics.total_count, IntegerType()), | ||
] | ||
) | ||
|
||
rows = super()._transform_evaluate_internal(dataset, schema).collect() | ||
num_models = ( | ||
len(self._treelite_model) if isinstance(self._treelite_model, list) else 1 | ||
) | ||
|
||
metrics = RegressionMetrics.from_rows(num_models, rows) | ||
return [metric.evaluate(evaluator) for metric in metrics] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
count -> metrics
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Done