Python 中的 Spark ML 逻辑回归:设置模型阈值以最大化 F-Measure
Spark ML Logistic Regression in Python: Set the model threshold to maximize F-Measure
我已经使用流水线在 Spark 中训练了逻辑回归。它 运行 我正在查看模型诊断。
我创建了模型摘要(lr_summary = lrModel.stages[-1].summary)。
之后我几乎从 this webpage 复制了代码。这一切都有效,直到我尝试使用此示例 Python 代码:
确定基于 F-measure 的最佳阈值
# Set the model threshold to maximize F-Measure
fMeasure = lr_summary.fMeasureByThreshold
maxFMeasure = fMeasure.groupBy().max('F-Measure').select('max(F-Measure)').head()
bestThreshold = fMeasure.where(fMeasure['F-Measure'] == maxFMeasure['max(F-Measure)']).select('threshold').head()['threshold']
lr.setThreshold(bestThreshold)
不幸的是,我在第 3 行 (bestThreshold = ) 中遇到错误:
类型错误:'NoneType' 对象没有属性 'getitem'
有什么建议吗?
非常感谢!
我无法重现此问题,但模型可能没有摘要(在这种情况下,我希望 maxFMeasure = ...
行中出现属性错误)。您可以检查模型是否有:
lrModel.stages[-1].hasSummary
您还可以使此代码更简单:
bestThreshold = fMeasure.orderBy(fMeasure['F-Measure'].desc()).first().threshold
我已经使用流水线在 Spark 中训练了逻辑回归。它 运行 我正在查看模型诊断。
我创建了模型摘要(lr_summary = lrModel.stages[-1].summary)。 之后我几乎从 this webpage 复制了代码。这一切都有效,直到我尝试使用此示例 Python 代码:
确定基于 F-measure 的最佳阈值# Set the model threshold to maximize F-Measure
fMeasure = lr_summary.fMeasureByThreshold
maxFMeasure = fMeasure.groupBy().max('F-Measure').select('max(F-Measure)').head()
bestThreshold = fMeasure.where(fMeasure['F-Measure'] == maxFMeasure['max(F-Measure)']).select('threshold').head()['threshold']
lr.setThreshold(bestThreshold)
不幸的是,我在第 3 行 (bestThreshold = ) 中遇到错误: 类型错误:'NoneType' 对象没有属性 'getitem'
有什么建议吗?
非常感谢!
我无法重现此问题,但模型可能没有摘要(在这种情况下,我希望 maxFMeasure = ...
行中出现属性错误)。您可以检查模型是否有:
lrModel.stages[-1].hasSummary
您还可以使此代码更简单:
bestThreshold = fMeasure.orderBy(fMeasure['F-Measure'].desc()).first().threshold