【问题标题】:Pyspark: Random forest featureSubsetStrategy not accepting int or floatPyspark:随机森林 featureSubsetStrategy 不接受 int 或 float
【发布时间】:2019-02-16 02:03:36
【问题描述】:

我正在使用 pyspark 构建一个随机森林分类器。我想将featureSubsetStrategy 设置为数字而不是autosqrt 等。文档指出:

featureSubsetStrategy = Param(parent='undefined', name='featureSubsetStrategy', doc='The number of features to consider for splits at each tree node. Supported options: auto, all, onethird, sqrt, log2, (0.0-1.0], [1-n].')

但是,例如,当我选择诸如 0.2 之类的数字时,我收到以下错误:

TypeError: Invalid param value given for param "featureSubsetStrategy". Could not convert <class 'float'> to string type

如果我使用featureSubsetStrategy=5,也会发生同样的情况。如何设置它使其可以是 int 或 float?

例子:

# setting target label
label_col = 'veh_pref_Economy'

# random forest parameters
max_depth = 2
subset_strategy = 0.2037
impurity = 'gini'
min_instances_per_node = 41
num_trees = 1
seed = 1246

rf_econ_gen = (RandomForestClassifier()
                 .setLabelCol(label_col)
                 .setFeaturesCol("features")
                 .setMaxDepth(max_depth)
                 .setFeatureSubsetStrategy(subset_strategy)
                 .setImpurity(impurity)
                 .setMinInstancesPerNode(min_instances_per_node)
                 .setNumTrees(num_trees)
                 .setSeed(seed))

这会返回:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/spark-2.2.1-bin-hadoop2.7/python/pyspark/ml/param/__init__.py in _set(self, **kwargs)
    418                 try:
--> 419                     value = p.typeConverter(value)
    420                 except TypeError as e:

~/spark-2.2.1-bin-hadoop2.7/python/pyspark/ml/param/__init__.py in toString(value)
    203         else:
--> 204             raise TypeError("Could not convert %s to string type" % type(value))
    205 

TypeError: Could not convert <class 'float'> to string type

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-28-71b9c2a0f1a0> in <module>()
      3                  .setFeaturesCol("features")
      4                  .setMaxDepth(max_depth)
----> 5                  .setFeatureSubsetStrategy(subset_strategy)
      6                  .setImpurity(impurity)
      7                  .setMinInstancesPerNode(min_instances_per_node)

~/spark-2.2.1-bin-hadoop2.7/python/pyspark/ml/regression.py in setFeatureSubsetStrategy(self, value)
    632         Sets the value of :py:attr:`featureSubsetStrategy`.
    633         """
--> 634         return self._set(featureSubsetStrategy=value)
    635 
    636     @since("1.4.0")

~/spark-2.2.1-bin-hadoop2.7/python/pyspark/ml/param/__init__.py in _set(self, **kwargs)
    419                     value = p.typeConverter(value)
    420                 except TypeError as e:
--> 421                     raise TypeError('Invalid param value given for param "%s". %s' % (p.name, e))
    422             self._paramMap[p] = value
    423         return self

TypeError: Invalid param value given for param "featureSubsetStrategy". Could not convert <class 'float'> to string type

【问题讨论】:

  • 传入字符串有效:subset_strategy = "0.2037",似乎它可能是错误的文档。
  • 是的,我自己尝试将它作为字符串传递,但没有收到错误,但它似乎不正确,并且不确定引擎盖下发生了什么。这是一个经典的火花问题。
  • pyspark 实现只是 Java 版本的包装器。 Java docs 表示featureSubsetStrategyString
  • @pault:谢谢,还有一个问题。它说它也可以取值[1-n],这是否意味着如果有50个特征并且我想为每棵树使用其中的10个,我输入9还是10。还是我完全误解了它的工作原理?
  • 我的解释是您必须输入10 才能获得10 features。您必须在每棵树上至少选择一个特征,这就是为什么下限是1。您可以通过使用 1 棵树构建一个 Forest 并查看选择了多少特征来进行测试。

标签: python pyspark random-forest


【解决方案1】:

尝试将其放在字符串中。

subset_strategy = "0.2037"

rf_econ_gen = (RandomForestClassifier()
                 .setFeatureSubsetStrategy(subset_strategy))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 2015-11-11
    • 1970-01-01
    • 2019-09-05
    • 2020-10-15
    • 2018-04-21
    • 2017-03-15
    相关资源
    最近更新 更多