如何在 pyspark 的 LogisticRegressionWithLBFGS 中打印预测概率 [英] How to print the probability of prediction in LogisticRegressionWithLBFGS for pyspark

查看:30
本文介绍了如何在 pyspark 的 LogisticRegressionWithLBFGS 中打印预测概率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Spark 1.5.1 并且,在pyspark中,在我使用以下方法拟合模型后:

I am using Spark 1.5.1 and, In pyspark, after I fit the model using:

model = LogisticRegressionWithLBFGS.train(parsedData)

我可以使用以下方法打印预测:

I can print the prediction using:

model.predict(p.features)

是否有同时打印概率分数和预测的函数?

Is there a function to print the probability score also along with the prediction?

推荐答案

你必须首先清除阈值,这仅适用于二进制分类:

You have to clear the threshold first, and this works only for binary classification:

 from pyspark.mllib.classification import LogisticRegressionWithLBFGS, LogisticRegressionModel
 from pyspark.mllib.regression import LabeledPoint

 parsed_data = [LabeledPoint(0.0, [4.6,3.6,1.0,0.2]),
                LabeledPoint(0.0, [5.7,4.4,1.5,0.4]),
                LabeledPoint(1.0, [6.7,3.1,4.4,1.4]),
                LabeledPoint(0.0, [4.8,3.4,1.6,0.2]),
                LabeledPoint(1.0, [4.4,3.2,1.3,0.2])]   

 model = LogisticRegressionWithLBFGS.train(sc.parallelize(parsed_data))
 model.threshold
 # 0.5
 model.predict(parsed_data[2].features)
 # 1

 model.clearThreshold()
 model.predict(parsed_data[2].features)
 # 0.9873840020002339

这篇关于如何在 pyspark 的 LogisticRegressionWithLBFGS 中打印预测概率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆