2015-01-28 20:14:23 -05:00
|
|
|
#
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
# The ASF licenses this file to You 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.
|
|
|
|
#
|
|
|
|
|
2015-04-16 19:20:57 -04:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2015-01-28 20:14:23 -05:00
|
|
|
header = """#
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
# The ASF licenses this file to You 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.
|
|
|
|
#"""
|
|
|
|
|
2015-04-16 02:49:42 -04:00
|
|
|
# Code generator for shared params (shared.py). Run under this folder with:
|
|
|
|
# python _shared_params_code_gen.py > shared.py
|
2015-01-28 20:14:23 -05:00
|
|
|
|
2015-04-16 02:49:42 -04:00
|
|
|
|
2016-03-23 14:20:44 -04:00
|
|
|
def _gen_param_header(name, doc, defaultValueStr, typeConverter):
|
2015-01-28 20:14:23 -05:00
|
|
|
"""
|
2015-05-12 15:17:05 -04:00
|
|
|
Generates the header part for shared variables
|
2015-01-28 20:14:23 -05:00
|
|
|
|
|
|
|
:param name: param name
|
|
|
|
:param doc: param doc
|
|
|
|
"""
|
|
|
|
template = '''class Has$Name(Params):
|
|
|
|
"""
|
2015-10-20 19:51:32 -04:00
|
|
|
Mixin for param $name: $doc
|
2015-01-28 20:14:23 -05:00
|
|
|
"""
|
|
|
|
|
2016-03-23 14:20:44 -04:00
|
|
|
$name = Param(Params._dummy(), "$name", "$doc", typeConverter=$typeConverter)
|
2015-01-28 20:14:23 -05:00
|
|
|
|
|
|
|
def __init__(self):
|
2016-01-26 18:53:48 -05:00
|
|
|
super(Has$Name, self).__init__()'''
|
|
|
|
|
2015-05-20 18:16:12 -04:00
|
|
|
if defaultValueStr is not None:
|
|
|
|
template += '''
|
|
|
|
self._setDefault($name=$defaultValueStr)'''
|
2015-05-12 15:17:05 -04:00
|
|
|
|
|
|
|
Name = name[0].upper() + name[1:]
|
2016-03-23 14:20:44 -04:00
|
|
|
if typeConverter is None:
|
|
|
|
typeConverter = str(None)
|
2015-05-12 15:17:05 -04:00
|
|
|
return template \
|
|
|
|
.replace("$name", name) \
|
|
|
|
.replace("$Name", Name) \
|
|
|
|
.replace("$doc", doc) \
|
2016-01-06 13:43:03 -05:00
|
|
|
.replace("$defaultValueStr", str(defaultValueStr)) \
|
2016-03-23 14:20:44 -04:00
|
|
|
.replace("$typeConverter", typeConverter)
|
2015-01-28 20:14:23 -05:00
|
|
|
|
2015-05-12 15:17:05 -04:00
|
|
|
|
|
|
|
def _gen_param_code(name, doc, defaultValueStr):
|
|
|
|
"""
|
|
|
|
Generates Python code for a shared param class.
|
|
|
|
|
|
|
|
:param name: param name
|
|
|
|
:param doc: param doc
|
|
|
|
:param defaultValueStr: string representation of the default value
|
|
|
|
:return: code string
|
|
|
|
"""
|
|
|
|
# TODO: How to correctly inherit instance attributes?
|
|
|
|
template = '''
|
2015-01-28 20:14:23 -05:00
|
|
|
def get$Name(self):
|
|
|
|
"""
|
|
|
|
Gets the value of $name or its default value.
|
|
|
|
"""
|
2015-04-16 02:49:42 -04:00
|
|
|
return self.getOrDefault(self.$name)'''
|
2015-01-28 20:14:23 -05:00
|
|
|
|
2015-04-16 02:49:42 -04:00
|
|
|
Name = name[0].upper() + name[1:]
|
2015-01-28 20:14:23 -05:00
|
|
|
return template \
|
|
|
|
.replace("$name", name) \
|
2015-04-16 02:49:42 -04:00
|
|
|
.replace("$Name", Name) \
|
2015-01-28 20:14:23 -05:00
|
|
|
.replace("$doc", doc) \
|
2015-04-16 02:49:42 -04:00
|
|
|
.replace("$defaultValueStr", str(defaultValueStr))
|
2015-01-28 20:14:23 -05:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2015-04-16 19:20:57 -04:00
|
|
|
print(header)
|
|
|
|
print("\n# DO NOT MODIFY THIS FILE! It was generated by _shared_params_code_gen.py.\n")
|
2016-03-23 14:20:44 -04:00
|
|
|
print("from pyspark.ml.param import *\n\n")
|
2015-01-28 20:14:23 -05:00
|
|
|
shared = [
|
2016-03-23 14:20:44 -04:00
|
|
|
("maxIter", "max number of iterations (>= 0).", None, "TypeConverters.toInt"),
|
|
|
|
("regParam", "regularization parameter (>= 0).", None, "TypeConverters.toFloat"),
|
|
|
|
("featuresCol", "features column name.", "'features'", "TypeConverters.toString"),
|
|
|
|
("labelCol", "label column name.", "'label'", "TypeConverters.toString"),
|
|
|
|
("predictionCol", "prediction column name.", "'prediction'", "TypeConverters.toString"),
|
2015-05-13 18:13:09 -04:00
|
|
|
("probabilityCol", "Column name for predicted class conditional probabilities. " +
|
|
|
|
"Note: Not all models output well-calibrated probability estimates! These probabilities " +
|
2016-03-23 14:20:44 -04:00
|
|
|
"should be treated as confidences, not precise probabilities.", "'probability'",
|
|
|
|
"TypeConverters.toString"),
|
2016-01-06 13:43:03 -05:00
|
|
|
("rawPredictionCol", "raw prediction (a.k.a. confidence) column name.", "'rawPrediction'",
|
2016-03-23 14:20:44 -04:00
|
|
|
"TypeConverters.toString"),
|
|
|
|
("inputCol", "input column name.", None, "TypeConverters.toString"),
|
|
|
|
("inputCols", "input column names.", None, "TypeConverters.toListString"),
|
|
|
|
("outputCol", "output column name.", "self.uid + '__output'", "TypeConverters.toString"),
|
2018-01-11 19:20:30 -05:00
|
|
|
("outputCols", "output column names.", None, "TypeConverters.toListString"),
|
2019-09-05 23:50:45 -04:00
|
|
|
("numFeatures", "Number of features. Should be greater than 0.", "262144",
|
|
|
|
"TypeConverters.toInt"),
|
2015-11-20 01:14:01 -05:00
|
|
|
("checkpointInterval", "set checkpoint interval (>= 1) or disable checkpoint (-1). " +
|
2018-01-10 02:32:47 -05:00
|
|
|
"E.g. 10 means that the cache will get checkpointed every 10 iterations. Note: " +
|
|
|
|
"this setting will be ignored if the checkpoint directory is not set in the SparkContext.",
|
|
|
|
None, "TypeConverters.toInt"),
|
2016-03-23 14:20:44 -04:00
|
|
|
("seed", "random seed.", "hash(type(self).__name__)", "TypeConverters.toInt"),
|
2016-05-13 02:52:06 -04:00
|
|
|
("tol", "the convergence tolerance for iterative algorithms (>= 0).", None,
|
2016-03-23 14:20:44 -04:00
|
|
|
"TypeConverters.toFloat"),
|
2019-10-31 01:52:28 -04:00
|
|
|
("relativeError", "the relative target precision for the approximate quantile " +
|
|
|
|
"algorithm. Must be in the range [0, 1]", "0.001", "TypeConverters.toFloat"),
|
2016-05-13 02:52:06 -04:00
|
|
|
("stepSize", "Step size to be used for each iteration of optimization (>= 0).", None,
|
2016-03-23 14:20:44 -04:00
|
|
|
"TypeConverters.toFloat"),
|
2015-09-10 23:43:38 -04:00
|
|
|
("handleInvalid", "how to handle invalid entries. Options are skip (which will filter " +
|
2016-05-13 02:52:06 -04:00
|
|
|
"out rows with bad values), or error (which will throw an error). More options may be " +
|
2016-04-15 15:14:41 -04:00
|
|
|
"added later.", None, "TypeConverters.toString"),
|
2015-09-11 11:50:35 -04:00
|
|
|
("elasticNetParam", "the ElasticNet mixing parameter, in range [0, 1]. For alpha = 0, " +
|
2016-03-23 14:20:44 -04:00
|
|
|
"the penalty is an L2 penalty. For alpha = 1, it is an L1 penalty.", "0.0",
|
|
|
|
"TypeConverters.toFloat"),
|
|
|
|
("fitIntercept", "whether to fit an intercept term.", "True", "TypeConverters.toBoolean"),
|
2015-09-11 11:50:35 -04:00
|
|
|
("standardization", "whether to standardize the training features before fitting the " +
|
2016-03-23 14:20:44 -04:00
|
|
|
"model.", "True", "TypeConverters.toBoolean"),
|
2015-09-11 11:50:35 -04:00
|
|
|
("thresholds", "Thresholds in multi-class classification to adjust the probability of " +
|
|
|
|
"predicting each class. Array must have length equal to the number of classes, with " +
|
2016-09-24 03:15:55 -04:00
|
|
|
"values > 0, excepting that at most one value may be 0. " +
|
|
|
|
"The class with largest value p/t is predicted, where p is the original " +
|
|
|
|
"probability of that class and t is the class's threshold.", None,
|
2016-03-23 14:20:44 -04:00
|
|
|
"TypeConverters.toListFloat"),
|
2017-01-27 19:03:53 -05:00
|
|
|
("threshold", "threshold in binary classification prediction, in range [0, 1]",
|
|
|
|
"0.5", "TypeConverters.toFloat"),
|
2015-10-07 20:50:35 -04:00
|
|
|
("weightCol", "weight column name. If this is not set or empty, we treat " +
|
2016-03-23 14:20:44 -04:00
|
|
|
"all instance weights as 1.0.", None, "TypeConverters.toString"),
|
2015-10-28 11:54:20 -04:00
|
|
|
("solver", "the solver algorithm for optimization. If this is not set or empty, " +
|
2016-04-08 13:47:05 -04:00
|
|
|
"default value is 'auto'.", "'auto'", "TypeConverters.toString"),
|
|
|
|
("varianceCol", "column name for the biased sample variance of prediction.",
|
2016-08-25 05:26:33 -04:00
|
|
|
None, "TypeConverters.toString"),
|
|
|
|
("aggregationDepth", "suggested depth for treeAggregate (>= 2).", "2",
|
2017-09-12 13:02:27 -04:00
|
|
|
"TypeConverters.toInt"),
|
|
|
|
("parallelism", "the number of threads to use when running parallel algorithms (>= 1).",
|
2017-12-20 20:51:42 -05:00
|
|
|
"1", "TypeConverters.toInt"),
|
2018-04-16 12:31:24 -04:00
|
|
|
("collectSubModels", "Param for whether to collect a list of sub-models trained during " +
|
|
|
|
"tuning. If set to false, then only the single best sub-model will be available after " +
|
|
|
|
"fitting. If set to true, then all sub-models will be available. Warning: For large " +
|
|
|
|
"models, collecting all sub-models can cause OOMs on the Spark driver.",
|
|
|
|
"False", "TypeConverters.toBoolean"),
|
2018-06-28 17:07:28 -04:00
|
|
|
("loss", "the loss function to be optimized.", None, "TypeConverters.toString"),
|
|
|
|
("distanceMeasure", "the distance measure. Supported options: 'euclidean' and 'cosine'.",
|
2018-12-07 16:53:35 -05:00
|
|
|
"'euclidean'", "TypeConverters.toString"),
|
|
|
|
("validationIndicatorCol", "name of the column that indicates whether each row is for " +
|
|
|
|
"training or for validation. False indicates training; true indicates validation.",
|
2020-02-09 00:14:30 -05:00
|
|
|
None, "TypeConverters.toString"),
|
|
|
|
("blockSize", "block size for stacking input data in matrices. Data is stacked within "
|
|
|
|
"partitions. If block size is more than remaining data in a partition then it is "
|
|
|
|
"adjusted to the size of this data.", None, "TypeConverters.toInt")]
|
2015-10-07 20:50:35 -04:00
|
|
|
|
2015-01-28 20:14:23 -05:00
|
|
|
code = []
|
2016-03-23 14:20:44 -04:00
|
|
|
for name, doc, defaultValueStr, typeConverter in shared:
|
|
|
|
param_code = _gen_param_header(name, doc, defaultValueStr, typeConverter)
|
2015-05-12 15:17:05 -04:00
|
|
|
code.append(param_code + "\n" + _gen_param_code(name, doc, defaultValueStr))
|
|
|
|
|
2015-04-16 19:20:57 -04:00
|
|
|
print("\n\n\n".join(code))
|