我有一个包含 42 个特征和 1 个标签的数据集。 我想在执行决策树之前应用 Spark ML 库的选择方法卡方选择器来检测异常,但在应用卡方选择器期间遇到此错误:
org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 17.0 failed 1 times, most recent failure: Lost task 0.0 in stage 17.0 (TID 45, localhost, executor driver): org.apache.spark.SparkException: Chi-square test expect factors (categorical values) but found more than 10000 distinct values in column 11.
这是我的源代码:
from pyspark.ml.feature import ChiSqSelector
selector = ChiSqSelector(numTopFeatures=1, featuresCol="features",outputCol="features2", labelCol="label")
result = selector.fit(dfa1).transform(dfa1)
result.show()
最佳答案
正如您在错误消息中看到的,您的 features
列包含向量中超过 10000 个不同的值,并且看起来它们是连续的而不是分类的,ChiSq 只能处理 10k 类别,并且您无法增加此值值。
/**
* Max number of categories when indexing labels and features
*/
private[spark] val maxCategories: Int = 10000
在这种情况下,您可以使用 VectorIndexer
和 .setMaxCategories()
参数 < 10k 来准备数据。您可以尝试其他方法来准备数据,但只有在向量中不同值的计数> 10k 时,它才会起作用。
关于python - Spark异常: Chi-square test expect factors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58608274/