【问题标题】:How to get all parameters of estimator in PySpark如何在 PySpark 中获取估计器的所有参数
【发布时间】:2017-07-21 19:28:48
【问题描述】:

我有一个RandomForestRegressorGBTRegressor,我想获取它们的所有参数。我发现它的唯一方法可以通过几个 get 方法来完成,例如:

from pyspark.ml.regression import RandomForestRegressor, GBTRegressor
est = RandomForestRegressor()
est.getMaxDepth()
est.getSeed()

但是RandomForestRegressorGBTRegressor 有不同的参数,所以硬核所有这些方法并不是一个好主意。 解决方法可能是这样的:

get_methods = [method for method in dir(est) if method.startswith('get')]

params_est = {}
for method in get_methods:
    try:
        key = method[3:]
        params_est[key] = getattr(est, method)() 
    except TypeError:
        pass

那么输出会是这样的:

params_est

{'CacheNodeIds': False,
 'CheckpointInterval': 10,
 'FeatureSubsetStrategy': 'auto',
 'FeaturesCol': 'features',
 'Impurity': 'variance',
 'LabelCol': 'label',
 'MaxBins': 32,
 'MaxDepth': 5,
 'MaxMemoryInMB': 256,
 'MinInfoGain': 0.0,
 'MinInstancesPerNode': 1,
 'NumTrees': 20,
 'PredictionCol': 'prediction',
 'Seed': None,
 'SubsamplingRate': 1.0}

但我认为应该有更好的方法来做到这一点。

【问题讨论】:

    标签: apache-spark pyspark apache-spark-ml


    【解决方案1】:

    extractParamMap 可用于获取每个估算器的所有参数,例如:

    >>> est = RandomForestRegressor()
    >>> {param[0].name: param[1] for param in est.extractParamMap().items()}
    {'numTrees': 20, 'cacheNodeIds': False, 'impurity': 'variance', 'predictionCol': 'prediction', 'labelCol': 'label', 'featuresCol': 'features', 'minInstancesPerNode': 1, 'seed': -5851613654371098793, 'maxDepth': 5, 'featureSubsetStrategy': 'auto', 'minInfoGain': 0.0, 'checkpointInterval': 10, 'subsamplingRate': 1.0, 'maxMemoryInMB': 256, 'maxBins': 32}
    >>> est = GBTRegressor()
    >>> {param[0].name: param[1] for param in est.extractParamMap().items()}
    {'cacheNodeIds': False, 'impurity': 'variance', 'predictionCol': 'prediction', 'labelCol': 'label', 'featuresCol': 'features', 'stepSize': 0.1, 'minInstancesPerNode': 1, 'seed': -6363326153609583521, 'maxDepth': 5, 'maxIter': 20, 'minInfoGain': 0.0, 'checkpointInterval': 10, 'subsamplingRate': 1.0, 'maxMemoryInMB': 256, 'lossType': 'squared', 'maxBins': 32}
    

    【讨论】:

    • 我看到了那个方法,但我错过了它的价值……谢谢。
    • 优先于访问私人_java_obj。尽管令人困惑的是为什么他们不只是将此功能提供给 getParam()...
    【解决方案2】:

    How to print best model params in pyspark pipeline中所述,您可以使用以下结构获取任何模型的原始JVM对象中可用的任何模型参数

        <yourModel>.stages[<yourModelStage>]._java_obj.<getYourParameter>()
    

    所有获取参数都可以在这里找到 https://spark.apache.org/docs/latest/api/java/org/apache/spark/ml/classification/RandomForestClassificationModel.html

    例如,如果您想在交叉验证后获取 RandomForest 的 MaxDepth(getMaxDepth 在 PySpark 中不可用),您可以使用

        cvModel.bestModel.stages[-1]._java_obj.getMaxDepth()
    

    【讨论】:

      猜你喜欢
      • 2021-01-13
      • 2013-04-13
      • 2018-02-11
      • 2018-09-18
      • 2019-02-28
      • 1970-01-01
      • 1970-01-01
      • 2018-11-13
      • 2017-01-24
      相关资源
      最近更新 更多