Skip to content
Snippets Groups Projects
Commit 28ccf5ee authored by Alexander Ulanov's avatar Alexander Ulanov Committed by Joseph K. Bradley
Browse files

[MLLIB] SPARK-5912 Programming guide for feature selection

Added description of ChiSqSelector and few words about feature selection in general. I could add a code example, however it would not look reasonable in the absence of feature discretizer or a dataset in the `data` folder that has redundant features.

Author: Alexander Ulanov <nashb@yandex.ru>

Closes #4709 from avulanov/SPARK-5912 and squashes the following commits:

19a8a4e [Alexander Ulanov] Addressing reviewers comments @jkbradley
58d9e4d [Alexander Ulanov] Addressing reviewers comments @jkbradley
eb6b9fe [Alexander Ulanov] Typo
2921a1d [Alexander Ulanov] ChiSqSelector example of use
c845350 [Alexander Ulanov] ChiSqSelector docs
parent 651a1c01
No related branches found
No related tags found
No related merge requests found
......@@ -375,3 +375,57 @@ data2 = labels.zip(normalizer2.transform(features))
{% endhighlight %}
</div>
</div>
## Feature selection
[Feature selection](http://en.wikipedia.org/wiki/Feature_selection) allows selecting the most relevant features for use in model construction. The number of features to select can be determined using the validation set. Feature selection is usually applied on sparse data, for example in text classification. Feature selection reduces the size of the vector space and, in turn, the complexity of any subsequent operation with vectors.
### ChiSqSelector
ChiSqSelector stands for Chi-Squared feature selection. It operates on the labeled data. ChiSqSelector orders categorical features based on their values of Chi-Squared test on independence from class and filters (selects) top given features.
#### Model Fitting
[`ChiSqSelector`](api/scala/index.html#org.apache.spark.mllib.feature.ChiSqSelector) has the
following parameters in the constructor:
* `numTopFeatures` number of top features that selector will select (filter).
We provide a [`fit`](api/scala/index.html#org.apache.spark.mllib.feature.ChiSqSelector) method in
`ChiSqSelector` which can take an input of `RDD[LabeledPoint]` with categorical features, learn the summary statistics, and then
return a model which can transform the input dataset into the reduced feature space.
This model implements [`VectorTransformer`](api/scala/index.html#org.apache.spark.mllib.feature.VectorTransformer)
which can apply the Chi-Squared feature selection on a `Vector` to produce a reduced `Vector` or on
an `RDD[Vector]` to produce a reduced `RDD[Vector]`.
Note that the model that performs actual feature filtering can be instantiated independently with array of feature indices that has to be sorted ascending.
#### Example
The following example shows the basic use of ChiSqSelector.
<div class="codetabs">
<div data-lang="scala">
{% highlight scala %}
import org.apache.spark.SparkContext._
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.util.MLUtils
// load some data in libsvm format, each point is in the range 0..255
val data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt")
// discretize data in 16 equal bins
val discretizedData = data.map { lp =>
LabeledPoint(lp.label, Vectors.dense(lp.features.toArray.map { x => x / 16 } ) )
}
// create ChiSqSelector that will select 50 features
val selector = new ChiSqSelector(50)
// create ChiSqSelector model
val transformer = selector.fit(disctetizedData)
// filter top 50 features from each feature vector
val filteredData = disctetizedData.map { lp =>
LabeledPoint(lp.label, transformer.transform(lp.features))
}
{% endhighlight %}
</div>
</div>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment