aboutsummaryrefslogtreecommitdiff
path: root/python/pyspark/mllib/classification.py
diff options
context:
space:
mode:
authorDavies Liu <davies.liu@gmail.com>2014-09-19 15:01:11 -0700
committerXiangrui Meng <meng@databricks.com>2014-09-19 15:01:11 -0700
commitfce5e251d636c788cda91345867e0294280c074d (patch)
tree4bded23a826bcfeb02deef73bd735cf0a05d4ee7 /python/pyspark/mllib/classification.py
parenta03e5b81e91d9d792b6a2e01d1505394ea303dd8 (diff)
downloadspark-fce5e251d636c788cda91345867e0294280c074d.tar.gz
spark-fce5e251d636c788cda91345867e0294280c074d.tar.bz2
spark-fce5e251d636c788cda91345867e0294280c074d.zip
[SPARK-3491] [MLlib] [PySpark] use pickle to serialize data in MLlib
Currently, we serialize the data between JVM and Python case by case manually, this cannot scale to support so many APIs in MLlib. This patch will try to address this problem by serialize the data using pickle protocol, using Pyrolite library to serialize/deserialize in JVM. Pickle protocol can be easily extended to support customized class. All the modules are refactored to use this protocol. Known issues: There will be some performance regression (both CPU and memory, the serialized data increased) Author: Davies Liu <davies.liu@gmail.com> Closes #2378 from davies/pickle_mllib and squashes the following commits: dffbba2 [Davies Liu] Merge branch 'master' of github.com:apache/spark into pickle_mllib 810f97f [Davies Liu] fix equal of matrix 032cd62 [Davies Liu] add more type check and conversion for user_product bd738ab [Davies Liu] address comments e431377 [Davies Liu] fix cache of rdd, refactor 19d0967 [Davies Liu] refactor Picklers 2511e76 [Davies Liu] cleanup 1fccf1a [Davies Liu] address comments a2cc855 [Davies Liu] fix tests 9ceff73 [Davies Liu] test size of serialized Rating 44e0551 [Davies Liu] fix cache a379a81 [Davies Liu] fix pickle array in python2.7 df625c7 [Davies Liu] Merge commit '154d141' into pickle_mllib 154d141 [Davies Liu] fix autobatchedpickler 44736d7 [Davies Liu] speed up pickling array in Python 2.7 e1d1bfc [Davies Liu] refactor 708dc02 [Davies Liu] fix tests 9dcfb63 [Davies Liu] fix style 88034f0 [Davies Liu] rafactor, address comments 46a501e [Davies Liu] choose batch size automatically df19464 [Davies Liu] memorize the module and class name during pickleing f3506c5 [Davies Liu] Merge branch 'master' into pickle_mllib 722dd96 [Davies Liu] cleanup _common.py 0ee1525 [Davies Liu] remove outdated tests b02e34f [Davies Liu] remove _common.py 84c721d [Davies Liu] Merge branch 'master' into pickle_mllib 4d7963e [Davies Liu] remove muanlly serialization 6d26b03 [Davies Liu] fix tests c383544 [Davies Liu] classification f2a0856 [Davies Liu] mllib/regression d9f691f [Davies Liu] mllib/util cccb8b1 [Davies Liu] mllib/tree 8fe166a [Davies Liu] Merge branch 'pickle' into pickle_mllib aa2287e [Davies Liu] random f1544c4 [Davies Liu] refactor clustering 52d1350 [Davies Liu] use new protocol in mllib/stat b30ef35 [Davies Liu] use pickle to serialize data for mllib/recommendation f44f771 [Davies Liu] enable tests about array 3908f5c [Davies Liu] Merge branch 'master' into pickle c77c87b [Davies Liu] cleanup debugging code 60e4e2f [Davies Liu] support unpickle array.array for Python 2.6
Diffstat (limited to 'python/pyspark/mllib/classification.py')
-rw-r--r--python/pyspark/mllib/classification.py61
1 files changed, 27 insertions, 34 deletions
diff --git a/python/pyspark/mllib/classification.py b/python/pyspark/mllib/classification.py
index 71ab46b61d..ac142fb49a 100644
--- a/python/pyspark/mllib/classification.py
+++ b/python/pyspark/mllib/classification.py
@@ -15,19 +15,14 @@
# limitations under the License.
#
+from math import exp
+
import numpy
+from numpy import array
-from numpy import array, shape
-from pyspark import SparkContext
-from pyspark.mllib._common import \
- _dot, _get_unmangled_rdd, _get_unmangled_double_vector_rdd, \
- _serialize_double_matrix, _deserialize_double_matrix, \
- _serialize_double_vector, _deserialize_double_vector, \
- _get_initial_weights, _serialize_rating, _regression_train_wrapper, \
- _linear_predictor_typecheck, _get_unmangled_labeled_point_rdd
-from pyspark.mllib.linalg import SparseVector
-from pyspark.mllib.regression import LabeledPoint, LinearModel
-from math import exp, log
+from pyspark import SparkContext, PickleSerializer
+from pyspark.mllib.linalg import SparseVector, _convert_to_vector
+from pyspark.mllib.regression import LabeledPoint, LinearModel, _regression_train_wrapper
__all__ = ['LogisticRegressionModel', 'LogisticRegressionWithSGD', 'SVMModel',
@@ -67,8 +62,7 @@ class LogisticRegressionModel(LinearModel):
"""
def predict(self, x):
- _linear_predictor_typecheck(x, self._coeff)
- margin = _dot(x, self._coeff) + self._intercept
+ margin = self.weights.dot(x) + self._intercept
if margin > 0:
prob = 1 / (1 + exp(-margin))
else:
@@ -81,7 +75,7 @@ class LogisticRegressionWithSGD(object):
@classmethod
def train(cls, data, iterations=100, step=1.0, miniBatchFraction=1.0,
- initialWeights=None, regParam=1.0, regType=None, intercept=False):
+ initialWeights=None, regParam=1.0, regType="none", intercept=False):
"""
Train a logistic regression model on the given data.
@@ -106,11 +100,12 @@ class LogisticRegressionWithSGD(object):
are activated or not).
"""
sc = data.context
- if regType is None:
- regType = "none"
- train_func = lambda d, i: sc._jvm.PythonMLLibAPI().trainLogisticRegressionModelWithSGD(
- d._jrdd, iterations, step, miniBatchFraction, i, regParam, regType, intercept)
- return _regression_train_wrapper(sc, train_func, LogisticRegressionModel, data,
+
+ def train(jdata, i):
+ return sc._jvm.PythonMLLibAPI().trainLogisticRegressionModelWithSGD(
+ jdata, iterations, step, miniBatchFraction, i, regParam, regType, intercept)
+
+ return _regression_train_wrapper(sc, train, LogisticRegressionModel, data,
initialWeights)
@@ -141,8 +136,7 @@ class SVMModel(LinearModel):
"""
def predict(self, x):
- _linear_predictor_typecheck(x, self._coeff)
- margin = _dot(x, self._coeff) + self._intercept
+ margin = self.weights.dot(x) + self.intercept
return 1 if margin >= 0 else 0
@@ -150,7 +144,7 @@ class SVMWithSGD(object):
@classmethod
def train(cls, data, iterations=100, step=1.0, regParam=1.0,
- miniBatchFraction=1.0, initialWeights=None, regType=None, intercept=False):
+ miniBatchFraction=1.0, initialWeights=None, regType="none", intercept=False):
"""
Train a support vector machine on the given data.
@@ -175,11 +169,12 @@ class SVMWithSGD(object):
are activated or not).
"""
sc = data.context
- if regType is None:
- regType = "none"
- train_func = lambda d, i: sc._jvm.PythonMLLibAPI().trainSVMModelWithSGD(
- d._jrdd, iterations, step, regParam, miniBatchFraction, i, regType, intercept)
- return _regression_train_wrapper(sc, train_func, SVMModel, data, initialWeights)
+
+ def train(jrdd, i):
+ return sc._jvm.PythonMLLibAPI().trainSVMModelWithSGD(
+ jrdd, iterations, step, regParam, miniBatchFraction, i, regType, intercept)
+
+ return _regression_train_wrapper(sc, train, SVMModel, data, initialWeights)
class NaiveBayesModel(object):
@@ -220,7 +215,8 @@ class NaiveBayesModel(object):
def predict(self, x):
"""Return the most likely class for a data vector x"""
- return self.labels[numpy.argmax(self.pi + _dot(x, self.theta.transpose()))]
+ x = _convert_to_vector(x)
+ return self.labels[numpy.argmax(self.pi + x.dot(self.theta.transpose()))]
class NaiveBayes(object):
@@ -242,12 +238,9 @@ class NaiveBayes(object):
@param lambda_: The smoothing parameter
"""
sc = data.context
- dataBytes = _get_unmangled_labeled_point_rdd(data)
- ans = sc._jvm.PythonMLLibAPI().trainNaiveBayes(dataBytes._jrdd, lambda_)
- return NaiveBayesModel(
- _deserialize_double_vector(ans[0]),
- _deserialize_double_vector(ans[1]),
- _deserialize_double_matrix(ans[2]))
+ jlist = sc._jvm.PythonMLLibAPI().trainNaiveBayes(data._to_java_object_rdd(), lambda_)
+ labels, pi, theta = PickleSerializer().loads(str(sc._jvm.SerDe.dumps(jlist)))
+ return NaiveBayesModel(labels.toArray(), pi.toArray(), numpy.array(theta))
def _test():