pyspark从crossvalidator中的每个子模型中检索度量(auc roc)

lsmepo6l  于 2021-05-29  发布在  Spark
关注(0)|答案(0)|浏览(246)

使用crossvalidator时,如何返回每个折叠/子模型的单个auc roc分数。
文档指出collectsubmodels=true应该保存所有模型,而不仅仅是最好的或平均的,但是在检查model.submodels之后,我找不到如何打印它们。
下面的示例只是缺少model.submodels.aucscore
期望的结果是每一个折叠的相应分数,如[fold1:0.85,fold2:0.07,fold3:0.55]

from pyspark.ml.feature import VectorAssembler
from pyspark.ml.classification import RandomForestClassifier
from pyspark.ml.tuning import CrossValidator, ParamGridBuilder
from pyspark.ml.evaluation import BinaryClassificationEvaluator

# Creating test dataframe

training = spark.createDataFrame([
    (1,0,1),
    (1,0,0),
    (0,1,1),
    (0,1,0)], ["label", "feature1", "feature2"])

# Vectorizing features for modelling

assembler = VectorAssembler(inputCols=['feature1','feature2'],outputCol="features")
prepped = assembler.transform(training).select('label','features')

# setting variables and configuring CrossValidator

rf = RandomForestClassifier(labelCol="label", featuresCol="features")
params = ParamGridBuilder().build()
evaluator = BinaryClassificationEvaluator()
folds = 3

cv = CrossValidator(estimator=rf,
estimatorParamMaps=params,
evaluator=evaluator,
numFolds=folds,
collectSubModels=True
)

# Fitting model

model = cv.fit(prepped)

# Print Metrics

print(model)
print()
print(model.avgMetrics)
print()
print(model.subModels)

>>>>>Return:
>>>>>CrossValidatorModel_3a5c95c6d8d2
>>>>>()
>>>>>[0.8333333333333333]
>>>>>()
>>>>>[[RandomForestClassificationModel (uid=RandomForestClassifier_95da3a68af93) with 20 trees], >>>>>[RandomForestClassificationModel (uid=RandomForestClassifier_95da3a68af93) with 20 trees], >>>>>[RandomForestClassificationModel (uid=RandomForestClassifier_95da3a68af93) with 20 trees]]

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题