我想為我的機器學習模型在 ROC 測驗下運行區域,但彈出屬性錯誤。以下是我的完整代碼,其中包含錯誤詳細資訊。
from pyspark.ml.classification import LogisticRegression
data = pp_df.select(
F.col("VectorAssembler_features").alias("features"),
F.col("HSCode").alias("label"),
)
model = LogisticRegression().fit(data)
model_summary.areaUnderROC
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
C:\Users\AZMANM~1\AppData\Local\Temp/ipykernel_4856/3039136250.py in <module>
----> 1 model_summary.areaUnderROC
AttributeError: 'LogisticRegressionTrainingSummary' object has no attribute 'areaUnderROC'
model.summary.pr.show()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
C:\Users\AZMANM~1\AppData\Local\Temp/ipykernel_4856/3388404637.py in <module>
----> 1 model.summary.pr.show()
AttributeError: 'LogisticRegressionTrainingSummary' object has no attribute 'pr'
uj5u.com熱心網友回復:
沒有代碼告訴我們您如何獲取model_summary變數。
你可能忘記使用model.summary.areaUnderROC而不是model_summary.areaUnderROC嗎?
以下示例對我有用:
from pyspark.sql import Row, SparkSession
from pyspark.ml.linalg import Vectors
from pyspark.ml.classification import LogisticRegression
if __name__ == "__main__":
spark = SparkSession.builder.getOrCreate()
sc = spark.sparkContext
bdf = sc.parallelize(
[
Row(label=1.0, weight=1.0, features=Vectors.dense(0.0, 5.0)),
Row(label=0.0, weight=2.0, features=Vectors.dense(1.0, 2.0)),
Row(label=1.0, weight=3.0, features=Vectors.dense(2.0, 1.0)),
Row(label=0.0, weight=4.0, features=Vectors.dense(3.0, 3.0)),
]
).toDF()
blor = LogisticRegression(weightCol="weight")
blorModel = blor.fit(bdf)
summary = blorModel.summary
aur = summary.areaUnderROC
uj5u.com熱心網友回復:
您將需要使用 BinaryClassificationEvaluator。在訓練測驗拆分后,我將訓練集命名為 train_set,將測驗資料命名為 test_set。
lr = LogisticRegression(labelCol="label",
featuresCol="features",maxIter=10 #,regParam=0.05
,threshold=0.5)
lr_model=lr.fit(train_set)
predict_train=lr_model.transform(train_set)
predict_test=lr_model.transform(test_set)
evaluator = BinaryClassificationEvaluator()
print("Test Area Under ROC: " str(evaluator.evaluate(predict_test, {evaluator.metricName: "areaUnderROC"})))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/323191.html
標籤:蟒蛇-3.x 机器学习 火花 逻辑回归 apache-spark-ml
