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

    https://github.com/apache/spark/pull/5137#discussion_r27162318
  
    --- Diff: python/pyspark/mllib/classification.py ---
    @@ -126,16 +145,38 @@ def predict(self, x):
                 return x.map(lambda v: self.predict(v))
     
             x = _convert_to_vector(x)
    -        margin = self.weights.dot(x) + self._intercept
    -        if margin > 0:
    -            prob = 1 / (1 + exp(-margin))
    +        if self.numClasses == 2:
    +            margin = self.weights.dot(x) + self._intercept
    +            if margin > 0:
    +                prob = 1 / (1 + exp(-margin))
    +            else:
    +                exp_margin = exp(margin)
    +                prob = exp_margin / (1 + exp_margin)
    +            if self._threshold is None:
    +                return prob
    +            else:
    +                return 1 if prob > self._threshold else 0
             else:
    -            exp_margin = exp(margin)
    -            prob = exp_margin / (1 + exp_margin)
    -        if self._threshold is None:
    -            return prob
    -        else:
    -            return 1 if prob > self._threshold else 0
    +            data_with_bias_size = self.weights.size / (self.numClasses - 1)
    +            if not isinstance(self.weights, DenseVector):
    +                raise ValueError("weights only supports dense vector but 
got type "
    +                                 + type(self.weights))
    +            weights_matrix = 
self.weights.toArray().reshape(self.numClasses - 1,
    +                                                            
data_with_bias_size)
    +            margins = []
    --- End diff --
    
    There's no need to store margins right now.  You could track best_class 
within this loop to avoid 2 loops.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to