卡方特征选择
operates on categorical data
ChiSqSelector
代表卡方特征选择。它对带有分类特征的标记数据进行操作
因此,这两个特征都是同样好的(尽管我们应该强调,这两个特征即使用作连续变量,也可以用来派生微不足道的完美分类器)。
import org.apache.spark.mllib.linalg.{Vectors => OldVectors}
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.stat.Statistics
Statistics.chiSqTest(sc.parallelize(data.map {
case (_, v, l) => LabeledPoint(l, OldVectors.fromML(v))
})).slice(2, 4)
Array[org.apache.spark.mllib.stat.test.ChiSqTestResult] =
Array(Chi squared test summary:
method: pearson
degrees of freedom = 2
statistic = 3.0
pValue = 0.22313016014843035
No presumption against null hypothesis: the occurrence of the outcomes is statistically independent.., Chi squared test summary:
method: pearson
degrees of freedom = 2
statistic = 3.0000000000000004
pValue = 0.22313016014843035
No presumption against null hypothesis: the occurrence of the outcomes is statistically independent..)
测试结果与其他工具一致。例如在R(
used as a reference for selector tests
):
y <- as.factor(c("1.0", "0.0", "0.0"))
x2 <- as.factor(c("18.0", "12.0", "15.0"))
x3 <- as.factor(c("1.0", "0.0", "0.1"))
chisq.test(table(x2, y))
Pearson's Chi-squared test
data: table(x2, y)
X-squared = 3, df = 2, p-value = 0.2231
Warning message:
In chisq.test(table(x2, y)) : Chi-squared approximation may be incorrect
chisq.test(table(x3, y))
Pearson's Chi-squared test
data: table(x3, y)
X-squared = 3, df = 2, p-value = 0.2231
Warning message:
In chisq.test(table(x3, y)) : Chi-squared approximation may be incorrect
自选择器
just sorts data by p-value
和
sortBy
is stable
,先到先得。如果切换功能的顺序,将选择另一个。