Github user sethah commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11989#discussion_r57978546
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/mllib/tree/impl/DecisionTreeMetadata.scala
 ---
    @@ -183,11 +183,40 @@ private[spark] object DecisionTreeMetadata extends 
Logging {
             }
           case _ => featureSubsetStrategy
         }
    +
    +    object featureSubsetNumber {
    +      def unapply(strategy: String): Option[Int] = try {
    +        val number = strategy.toInt
    +        if (0 < number) {
    +          Some(number)
    +        } else {
    +          None
    +        }
    +      } catch {
    +        case _ : java.lang.NumberFormatException => None
    +      }
    +    }
    +
    +    object featureSubsetFraction {
    +      def unapply(strategy: String): Option[Double] = try {
    +        val fraction = strategy.toDouble
    +        if (0.0 < fraction && fraction <= 1.0) {
    +          Some(fraction)
    +        } else {
    +          None
    +        }
    +      } catch {
    +        case _ : java.lang.NumberFormatException => None
    +      }
    +    }
    +
         val numFeaturesPerNode: Int = _featureSubsetStrategy match {
           case "all" => numFeatures
           case "sqrt" => math.sqrt(numFeatures).ceil.toInt
           case "log2" => math.max(1, (math.log(numFeatures) / 
math.log(2)).ceil.toInt)
           case "onethird" => (numFeatures / 3.0).ceil.toInt
    +      case featureSubsetNumber(number) => if (number > numFeatures) 
numFeatures else number
    +      case featureSubsetFraction(fraction) => (fraction * 
numFeatures).ceil.toInt
    --- End diff --
    
    Again, I think regex can simplify things a bit here. I haven't tried the 
following, but it should work.
    
    ```scala
    val isIntRegex = "(\\d+)".r
    val isFractionRegex = "(0?\\.\\d+|1.0+)".r
    val numFeaturesPerNode: Int = _featureSubsetStrategy match {
          case "all" => numFeatures
          case "sqrt" => math.sqrt(numFeatures).ceil.toInt
          case "log2" => math.max(1, (math.log(numFeatures) / 
math.log(2)).ceil.toInt)
          case "onethird" => (numFeatures / 3.0).ceil.toInt
          case isIntRegex(number) => if (number > numFeatures) numFeatures else 
number
          case isFractionRegex(fraction) => (fraction * numFeatures).ceil.toInt
        }
    ```
    
    Then we could avoid adding the `featureSubset*` objects.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to