Github user avulanov commented on a diff in the pull request:
https://github.com/apache/spark/pull/1484#discussion_r23883318
--- Diff:
mllib/src/main/scala/org/apache/spark/mllib/feature/ChiSqSelector.scala ---
@@ -0,0 +1,116 @@
+/*
+ * 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
+
+/**
+ * :: Experimental ::
+ * Chi Squared selector model.
+ *
+ * @param indices list of indices to select (filter)
+ */
+@Experimental
+class ChiSqSelectorModel(indices: Array[Int]) extends VectorTransformer {
+ /**
+ * Applies transformation on a vector.
+ *
+ * @param vector vector to be transformed.
+ * @return transformed vector.
+ */
+ override def transform(vector: Vector): Vector = {
+ Compress(vector, indices)
+ }
+}
+
+/**
+ * :: 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.
+ */
+ def fit(data: RDD[LabeledPoint]): ChiSqSelectorModel = {
+ val indices = Statistics.chiSqTest(data)
+ .zipWithIndex.sortBy { case(res, _) => -res.statistic }
+ .take(numTopFeatures)
+ .map{ case(_, indices) => indices }
+ new ChiSqSelectorModel(indices)
+ }
+}
+
+/**
+ * :: Experimental ::
+ * Filters features in a given vector
+ */
+@Experimental
+object Compress {
+ /**
+ * Returns a vector with features filtered.
+ * Preserves the order of filtered features the same as their indices
are stored.
+ * @param features vector
+ * @param filterIndices indices of features to filter
+ */
+ def apply(features: Vector, filterIndices: Array[Int]): Vector = {
+ features match {
+ case SparseVector(size, indices, values) =>
+ val filterMap = filterIndices.zipWithIndex.toMap
--- End diff --
Thank you for suggestion! Do you think it is ok to require indices to be
sorted? I've put `.sorted` into `.fit` however another candidate is
`.compress`. No "sorted" requirement is needed for the latter.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]