【问题标题】:Pyspark trained Logistic Regression model doesn't predict() and predictProbability() functionPyspark 训练的 Logistic 回归模型没有 predict() 和 predictProbability() 函数
【发布时间】:2021-10-16 00:29:52
【问题描述】:

我使用 PySpark MLlib 内置类 LogisticRegression 训练了一个逻辑回归模型。但是,当它被训练时,它不能用于预测其他数据帧,因为AttributeError: 'LogisticRegression' object has no attribute 'predictProbability' OR AttributeError: 'LogisticRegression' object has no attribute 'predict'。

from pyspark.ml.classification import LogisticRegression
model = LogisticRegression(regParam=0.5, elasticNetParam=1.0)

# define the input feature & output column
model.setFeaturesCol('features')
model.setLabelCol('WinA')

model.fit(df_train)

model.setPredictionCol('WinA')
model.predictProbability(df_val['features'])
model.predict(df_val['features'])
AttributeError: 'LogisticRegression' object has no attribute 'predictProbability'

属性:

PySpark 版本:

>>import pyspark
>>pyspark.__version__
3.1.2

JDK版本:

>>!java -version
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.18.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.18.04, mixed mode, sharing)

环境:Google Colab

【问题讨论】:

    标签: machine-learning pyspark apache-spark-ml


    【解决方案1】:

    你的代码在这里

    model.fit(df_train)
    

    实际上并没有给你一个训练有素的模型,因为变量 model 的类型仍然是 pyspark.ml.classification.LogisticRegression 类

    type(model)
    
    # pyspark.ml.classification.LogisticRegression
    

    因此,您应该通过将返回的对象分配给变量或覆盖 model 变量来捕获返回的对象,然后它将为您提供经过训练的 pyspark.ml.classification.LogisticRegressionModel 类逻辑回归模型

    model = model.fit(df_train)
    type(model)
    
    # pyspark.ml.classification.LogisticRegressionModel
    

    最后,.predict 和 .predictProbability 方法需要 pyspark.ml.linalg.DenseVector 对象的参数。因此,我认为您想改用.transform,因为它将预测标签和概率作为列添加到输入数据帧。应该是这样的

    predicted_df = model.transform(df_val)
    

    【讨论】:

      猜你喜欢
      • 2018-12-06
      • 2020-05-02
      • 1970-01-01
      • 2014-05-07
      • 2016-11-15
      • 2018-05-01
      • 2022-01-12
      • 2021-03-03
      • 2018-01-03
      相关资源
      最近更新 更多