python - 如何访问 Spark PipelineModel 参数

标签 python apache-spark pyspark pyspark-sql apache-spark-ml

我正在使用 Spark Pipelines 运行线性回归在 pyspark 中。线性回归模型训练完成后,如何得出系数?

这是我的管道代码:

# Get all of our features together into one array called "features".  Do not include the label!
feature_assembler = VectorAssembler(inputCols=get_column_names(df_train), outputCol="features")

# Define our model
lr = LinearRegression(maxIter=100, elasticNetParam=0.80, labelCol="label", featuresCol="features", 
                  predictionCol = "prediction")

# Define our pipeline
pipeline_baseline = Pipeline(stages=[feature_assembler, lr])

# Train our model using the training data
model_baseline = pipeline_baseline.fit(df_train)

# Use our trained model to make predictions using the validation data
output_baseline = model_baseline.transform(df_val)  #.select("features", "label", "prediction", "coefficients")
predictions_baseline = output_baseline.select("label", "prediction")

我尝试使用来自 PipelineModel class 的方法.这是我尝试获取系数的尝试,但我只得到一个空列表和一个空字典:

params = model_baseline.stages[1].params
print 'Try 1 - Parameters: %s' %(params)
params = model_baseline.stages[1].extractParamMap()
print 'Try 2 - Parameters: %s' %(params)

Out[]:
Try 1 - Parameters: []
Try 2 - Parameters: {}

PipelineModel 是否有返回训练系数的方法?

最佳答案

您看错了特性。 params 可用于提取 EstimatorTransformer Params,如输入或输出列(参见 ML Pipeline parameters docs 而不是估计值。

对于 LinearRegressionModel 使用系数:

model.stages[-1].coefficients

关于python - 如何访问 Spark PipelineModel 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38751536/

相关文章:

python - 基于 0 和非 0 向量创建 0 和 1 矩阵

用于被覆盖的属性和方法的 Python 命名约定

scala - spark数据帧爆炸功能错误

pyspark - 如何在不使用 StandardScaler 的情况下标准化 PySpark 中的列?

apache-spark - PySpark动态创建StructType

python - PySpark 将列除以其总和

python - 写入 CSV - 字符串在 Excel 中被识别为日期

python - 伯努利朴素贝叶斯错误 : ValueError: Unknown label type: (array([0, 0, 0, ..., 0, 0, 0], dtype=object),)

scala - 无法在 spark-shell 中连接到 Cassandra

apache-spark - pipeline.fit 是一个转换还是 Action