【发布时间】:2018-12-15 06:17:35
【问题描述】:
我的数据框有一列密集向量,即多类分类预测概率。我想将该列转换为 numpy 数组并面临形状不匹配的问题。有些东西我试过了。
我在here 上找到的一个答案确实将这些值转换为 numpy 数组,但在原始数据框中,它有
4653观察值,但 numpy 数组的形状是(4712, 21)。我不明白它是如何增加的,并且在使用相同代码 numpy 数组形状的另一次尝试中减少了原始数据帧的计数。我不明白为什么?我也试过
predictions.select("probability").toPandas().values.shape,但形状再次不匹配。我使用 pyspark 数据帧的count()方法来检查数据帧的长度。我还尝试了 UTF 与
toArray()pyspark 数据帧列的方法,导致像这样的奇怪错误org.apache.spark.SparkException: Job aborted due to stage failure: Task 2 in stage 116.0 failed 4 times, most recent failure: Lost task 2.3 in stage 116.0 (TID 6254, 10.2.1.54, executor 0): net.razorvine.pickle.PickleException: expected zero arguments for construction of ClassDict (for numpy.core.multiarray._reconstruct)
这就是我正在做的事情
rf = RandomForestClassifier(
featuresCol="features",
labelCol=TARGET_COL,
predictionCol=TARGET_COL + "_predicted",
# impurity="entropy"
# maxDepth=5,
# numTrees=1000,
# minInfoGain=0.2,
# subsamplingRate=0.8
)
evaluator = MulticlassClassificationEvaluator(
predictionCol=TARGET_COL + "_predicted",
labelCol=TARGET_COL,
metricName="accuracy"
)
paramGrid = ParamGridBuilder(). \
addGrid(rf.maxDepth, [3, 5, 7, 9, 11]). \
addGrid(rf.numTrees, [20, 50, 100, 200, 500]). \
addGrid(rf.minInfoGain, [0.0, 0.2, 0.5, 1.0]). \
addGrid(rf.subsamplingRate, [0.5, 0.8, 1.0]). \
addGrid(rf.impurity, ["entropy", "gini"]). \
build()
paramGrid = ParamGridBuilder(). \
addGrid(rf.maxDepth, [3]). \
addGrid(rf.numTrees, [2]). \
addGrid(rf.minInfoGain, [0.0]). \
addGrid(rf.subsamplingRate, [0.5]). \
addGrid(rf.impurity, ["entropy"]). \
build()
tvs = TrainValidationSplit(estimator=rf,
estimatorParamMaps=paramGrid,
evaluator=evaluator,
trainRatio=0.8)
print("~~~~~~~~~~~ Model Training Started ~~~~~~~~~~~")
model = tvs.fit(train_df)
best_model = model.bestModel
print(best_model._java_obj.parent().getImpurity())
print(best_model._java_obj.parent().getMaxDepth())
print(best_model._java_obj.parent().getNumTrees())
print(best_model._java_obj.parent().getMinInfoGain())
print(best_model._java_obj.parent().getSubsamplingRate())
prob_array = []
predictions = model.transform(test_df)
print(predictions.count())
print(test_df.count())
pprint(predictions.select("probability").head(1)[0].probability)
pprint(predictions.select("probability").head(1)[0].probability.toArray())
pprint(type(predictions.select("probability").head(1)[0].probability.toArray()))
pprint(predictions.select("probability").head(1)[0].probability.toArray().shape)
print(predictions.select("probability").count())
print(predictions.select("probability").toPandas())
print(predictions.select("probability").toPandas().values.shape)
【问题讨论】:
-
您也可以发布您的代码吗?它将帮助我们了解问题所在。
-
到底是哪里出了问题,what 是确切的错误跟踪??
-
没有编译时错误。这都是逻辑错误,形状不匹配是问题
-
你能打印出“预测”的样子吗?
-
[Row(probability=DenseVector([0.2066, 0.1184, 0.1138, 0.1158, 0.0876, 0.0548, 0.0628, 0.0713, 0.041, 0.0306, 0.0258, 0.0271, 0.0177, 0.0081, 0.0085, 0.0044, 0.0032, 0.0015, 0.0005, 0.0005, 0.0])), Row(probability=DenseVector([0.1902, 0.0679, 0.1281, 0.0939, 0.0719, 0.0205, 0.0977, 0.0471, 0.0946, 0.0491, 0.0425, 0.0292, 0.0113, 0.0328, 0.0098, 0.0048, 0.0029, 0.0036, 0.0016, 0.0002, 0.0003]))]
标签: python numpy apache-spark pyspark classification