2015-01-28 20:14:23 -05:00
|
|
|
#
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
# (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
2015-02-20 05:31:32 -05:00
|
|
|
from pyspark.ml.util import keyword_only
|
2015-01-28 20:14:23 -05:00
|
|
|
from pyspark.ml.wrapper import JavaEstimator, JavaModel
|
|
|
|
from pyspark.ml.param.shared import HasFeaturesCol, HasLabelCol, HasPredictionCol, HasMaxIter,\
|
|
|
|
HasRegParam
|
2015-02-20 05:31:32 -05:00
|
|
|
from pyspark.mllib.common import inherit_doc
|
2015-01-28 20:14:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
__all__ = ['LogisticRegression', 'LogisticRegressionModel']
|
|
|
|
|
|
|
|
|
|
|
|
@inherit_doc
|
|
|
|
class LogisticRegression(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredictionCol, HasMaxIter,
|
|
|
|
HasRegParam):
|
|
|
|
"""
|
|
|
|
Logistic regression.
|
|
|
|
|
|
|
|
>>> from pyspark.sql import Row
|
|
|
|
>>> from pyspark.mllib.linalg import Vectors
|
2015-02-15 23:29:26 -05:00
|
|
|
>>> df = sc.parallelize([
|
|
|
|
... Row(label=1.0, features=Vectors.dense(1.0)),
|
|
|
|
... Row(label=0.0, features=Vectors.sparse(1, [], []))]).toDF()
|
|
|
|
>>> lr = LogisticRegression(maxIter=5, regParam=0.01)
|
|
|
|
>>> model = lr.fit(df)
|
|
|
|
>>> test0 = sc.parallelize([Row(features=Vectors.dense(-1.0))]).toDF()
|
2015-04-16 19:20:57 -04:00
|
|
|
>>> model.transform(test0).head().prediction
|
2015-01-28 20:14:23 -05:00
|
|
|
0.0
|
2015-02-15 23:29:26 -05:00
|
|
|
>>> test1 = sc.parallelize([Row(features=Vectors.sparse(1, [0], [1.0]))]).toDF()
|
2015-04-16 19:20:57 -04:00
|
|
|
>>> model.transform(test1).head().prediction
|
2015-01-28 20:14:23 -05:00
|
|
|
1.0
|
2015-02-15 23:29:26 -05:00
|
|
|
>>> lr.setParams("vector")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
TypeError: Method setParams forces keyword arguments.
|
2015-01-28 20:14:23 -05:00
|
|
|
"""
|
|
|
|
_java_class = "org.apache.spark.ml.classification.LogisticRegression"
|
|
|
|
|
2015-02-15 23:29:26 -05:00
|
|
|
@keyword_only
|
|
|
|
def __init__(self, featuresCol="features", labelCol="label", predictionCol="prediction",
|
|
|
|
maxIter=100, regParam=0.1):
|
|
|
|
"""
|
|
|
|
__init__(self, featuresCol="features", labelCol="label", predictionCol="prediction", \
|
|
|
|
maxIter=100, regParam=0.1)
|
|
|
|
"""
|
|
|
|
super(LogisticRegression, self).__init__()
|
2015-04-16 02:49:42 -04:00
|
|
|
self._setDefault(maxIter=100, regParam=0.1)
|
2015-02-15 23:29:26 -05:00
|
|
|
kwargs = self.__init__._input_kwargs
|
|
|
|
self.setParams(**kwargs)
|
|
|
|
|
|
|
|
@keyword_only
|
|
|
|
def setParams(self, featuresCol="features", labelCol="label", predictionCol="prediction",
|
|
|
|
maxIter=100, regParam=0.1):
|
|
|
|
"""
|
|
|
|
setParams(self, featuresCol="features", labelCol="label", predictionCol="prediction", \
|
|
|
|
maxIter=100, regParam=0.1)
|
|
|
|
Sets params for logistic regression.
|
|
|
|
"""
|
|
|
|
kwargs = self.setParams._input_kwargs
|
2015-04-16 02:49:42 -04:00
|
|
|
return self._set(**kwargs)
|
2015-02-15 23:29:26 -05:00
|
|
|
|
2015-01-28 20:14:23 -05:00
|
|
|
def _create_model(self, java_model):
|
|
|
|
return LogisticRegressionModel(java_model)
|
|
|
|
|
|
|
|
|
|
|
|
class LogisticRegressionModel(JavaModel):
|
|
|
|
"""
|
|
|
|
Model fitted by LogisticRegression.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
from pyspark.context import SparkContext
|
|
|
|
from pyspark.sql import SQLContext
|
|
|
|
globs = globals().copy()
|
|
|
|
# The small batch size here ensures that we see multiple batches,
|
|
|
|
# even in these small test examples:
|
2015-05-12 15:17:05 -04:00
|
|
|
sc = SparkContext("local[2]", "ml.classification tests")
|
2015-04-08 16:31:45 -04:00
|
|
|
sqlContext = SQLContext(sc)
|
2015-01-28 20:14:23 -05:00
|
|
|
globs['sc'] = sc
|
2015-04-08 16:31:45 -04:00
|
|
|
globs['sqlContext'] = sqlContext
|
2015-01-28 20:14:23 -05:00
|
|
|
(failure_count, test_count) = doctest.testmod(
|
|
|
|
globs=globs, optionflags=doctest.ELLIPSIS)
|
|
|
|
sc.stop()
|
|
|
|
if failure_count:
|
|
|
|
exit(-1)
|