supports multinomial logistic 28softmax 29 and binomial logistic regression

Solutions on MaxInterview for supports multinomial logistic 28softmax 29 and binomial logistic regression by the best coders in the world

we are a community of more than 2 million smartest coders
registration for
employee referral programs
are now open
get referred to google, amazon, flipkart and more
register now
  
pinned-register now
showing results for - "supports multinomial logistic 28softmax 29 and binomial logistic regression"
Gabriel
14 Oct 2016
1# supports multinomial logistic (softmax) and binomial logistic regression
2
3from pyspark.sql import Row
4from pyspark.ml.linearalg import Vectors
5bdf = sc.parallelize([
6  Row(label=1.0, weight=2.0, features=Vectors.dense(1.0)),
7  Row(label=0.0, weight=2.0, features=Vectors.sparse(1, [], []))]).toDF()
8blor = LogisticRegression(maxIter=5, regParam=0.01, weightCol="weight")
9blorModel = blor.fit(bdf)
10blorModel.coefficients
11# DenseVector([5.5...])
12blorModel.intercept
13# -2.68...
14mdf = sc.parallelize([
15  Row(label=1.0, weight=2.0, features=Vectors.dense(1.0)),
16  Row(label=0.0, weight=2.0, features=Vectors.sparse(1, [], [])),
17  Row(label=2.0, weight=2.0, features=Vectors.dense(3.0))]).toDF()
18mlor = LogisticRegression(maxIter=5, regParam=0.01, weightCol="weight",
19                          family="multinomial")
20mlorModel = mlor.fit(mdf)
21print(mlorModel.coefficientMatrix)
22# DenseMatrix([[-2.3...],
23#              [ 0.2...],
24#              [ 2.1... ]])
25mlorModel.interceptVector
26# DenseVector([2.0..., 0.8..., -2.8...])
27test0 = sc.parallelize([Row(features=Vectors.dense(-1.0))]).toDF()
28result = blorModel.transform(test0).head()
29result.prediction
30# 0.0
31result.probability
32# DenseVector([0.99..., 0.00...])
33result.rawPrediction
34# DenseVector([0.99..., 0.00...])
35test1 = sc.parallelize([Row(features=Vectors.sparse(1, [0], [1.0]))]).toDF()
36blorModel.transform(test1).head().prediction
371.0
38blor.setParams("vector")
39# Traceback (most recent call last):
40#     ...
41# TypeError: Method setParams forces keyword arguments.
42lr_path = temp_path + "/lr"
43blor.save(lr_path)
44lr2 = LogisticRegression.load(lr_path)
45lr2.getMaxIter()
46# 5
47model_path = temp_path + "/lr_path"
48blorModel.save(model_path)
49model2 = LogisticRegressionModel.load(model_path)
50blorModel.coefficients[0] == model2.coefficients[0]
51# True
52blorModel.intercept == model2.intercept
53# True