-
Notifications
You must be signed in to change notification settings - Fork 28.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MLLIB] SPARK-5491 (ex SPARK-1473): Chi-square feature selection #1484
Changes from 15 commits
0bd8434
2ade254
ca49e80
e24eee4
aab9b73
66e0333
2bacdc7
f356365
150a3e0
80363ca
f9b070a
427ca4e
010acff
714b878
a6ad82a
755d358
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.mllib.feature | ||
|
||
import org.apache.spark.annotation.Experimental | ||
import org.apache.spark.mllib.linalg.{DenseVector, SparseVector, Vectors, Vector} | ||
import org.apache.spark.mllib.regression.LabeledPoint | ||
import org.apache.spark.mllib.stat.Statistics | ||
import org.apache.spark.rdd.RDD | ||
|
||
import scala.collection.mutable.ArrayBuilder | ||
|
||
/** | ||
* :: Experimental :: | ||
* Chi Squared selector model. | ||
* | ||
* @param indices list of indices to select (filter). Must be ordered asc | ||
*/ | ||
@Experimental | ||
class ChiSqSelectorModel private[mllib] (indices: Array[Int]) extends VectorTransformer { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove one space before |
||
/** | ||
* Applies transformation on a vector. | ||
* | ||
* @param vector vector to be transformed. | ||
* @return transformed vector. | ||
*/ | ||
override def transform(vector: Vector): Vector = { | ||
compress(vector, indices) | ||
} | ||
|
||
/** | ||
* Returns a vector with features filtered. | ||
* Preserves the order of filtered features the same as their indices are stored. | ||
* Might be moved to Vector as .slice | ||
* @param features vector | ||
* @param filterIndices indices of features to filter, must be ordered asc | ||
*/ | ||
private def compress(features: Vector, filterIndices: Array[Int]): Vector = { | ||
features match { | ||
case SparseVector(size, indices, values) => | ||
val newSize = filterIndices.length | ||
val newValues = new ArrayBuilder.ofDouble | ||
val newIndices = new ArrayBuilder.ofInt | ||
var i: Int = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove |
||
var j: Int = 0 | ||
while(i < indices.length && j < filterIndices.length) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. space after |
||
if(indices(i) == filterIndices(j)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. space after |
||
newIndices += j | ||
newValues += values(i) | ||
j += 1 | ||
i += 1 | ||
} else { | ||
if(indices(i) > filterIndices(j)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. space after |
||
j += 1 | ||
} else { | ||
i += 1 | ||
} | ||
} | ||
} | ||
/** Sparse representation might be ineffective if (newSize ~= newValues.size) */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point! Use a normal comment style |
||
Vectors.sparse(newSize, newIndices.result(), newValues.result()) | ||
case DenseVector(values) => | ||
val values = features.toArray | ||
Vectors.dense(filterIndices.map(i => values(i))) | ||
case other => | ||
throw new UnsupportedOperationException( | ||
s"Only sparse and dense vectors are supported but got ${other.getClass}.") | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* :: Experimental :: | ||
* Creates a ChiSquared feature selector. | ||
* @param numTopFeatures number of features that selector will select | ||
* (ordered by statistic value descending) | ||
*/ | ||
@Experimental | ||
class ChiSqSelector (val numTopFeatures: Int) { | ||
|
||
/** | ||
* Returns a ChiSquared feature selector. | ||
* | ||
* @param data data used to compute the Chi Squared statistic. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be worth noting that the values are factors, as in https://github.com/apache/spark/blob/master/mllib/src/main/scala/org/apache/spark/mllib/stat/Statistics.scala#L160 |
||
*/ | ||
def fit(data: RDD[LabeledPoint]): ChiSqSelectorModel = { | ||
val indices = Statistics.chiSqTest(data) | ||
.zipWithIndex.sortBy { case(res, _) => -res.statistic } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. space after |
||
.take(numTopFeatures) | ||
.map{ case(_, indices) => indices } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. space before |
||
.sorted | ||
new ChiSqSelectorModel(indices) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.mllib.feature | ||
|
||
import org.scalatest.FunSuite | ||
|
||
import org.apache.spark.mllib.linalg.Vectors | ||
import org.apache.spark.mllib.regression.LabeledPoint | ||
import org.apache.spark.mllib.util.MLlibTestSparkContext | ||
|
||
class ChiSqSelectorSuite extends FunSuite with MLlibTestSparkContext { | ||
|
||
/* | ||
* Contingency tables | ||
* feature0 = {8.0, 0.0} | ||
* class 0 1 2 | ||
* 8.0||1|0|1| | ||
* 0.0||0|2|0| | ||
* | ||
* feature1 = {7.0, 9.0} | ||
* class 0 1 2 | ||
* 7.0||1|0|0| | ||
* 9.0||0|2|1| | ||
* | ||
* feature2 = {0.0, 6.0, 8.0, 5.0} | ||
* class 0 1 2 | ||
* 0.0||1|0|0| | ||
* 6.0||0|1|0| | ||
* 8.0||0|1|0| | ||
* 5.0||0|0|1| | ||
* | ||
* Use chi-squared calculator from Internet | ||
*/ | ||
|
||
test("ChiSqSelector transform test (sparse & dense vector)") { | ||
val labeledDiscreteData = sc.parallelize( | ||
Seq(new LabeledPoint(0.0, Vectors.sparse(3, Array((0, 8.0), (1, 7.0)))), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
new LabeledPoint(1.0, Vectors.sparse(3, Array((1, 9.0), (2, 6.0)))), | ||
new LabeledPoint(1.0, Vectors.dense(Array(0.0, 9.0, 8.0))), | ||
new LabeledPoint(2.0, Vectors.dense(Array(8.0, 9.0, 5.0))) | ||
), 2) | ||
val preFilteredData = | ||
Set(new LabeledPoint(0.0, Vectors.dense(Array(0.0))), | ||
new LabeledPoint(1.0, Vectors.dense(Array(6.0))), | ||
new LabeledPoint(1.0, Vectors.dense(Array(8.0))), | ||
new LabeledPoint(2.0, Vectors.dense(Array(5.0))) | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor: In Spark, we don't make a new line for the ending |
||
val model = new ChiSqSelector(1).fit(labeledDiscreteData) | ||
val filteredData = labeledDiscreteData.map { lp => | ||
new LabeledPoint(lp.label, model.transform(lp.features)) | ||
}.collect().toSet | ||
assert(filteredData == preFilteredData) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
organize imports (If you use idea intellij, there is a useful plugin: https://plugins.jetbrains.com/plugin/7350)