Project: Apache Spark (App Definition and Development / Streaming & Messaging)
File: mllib-feature-extraction.md

layout: global title: Feature Extraction and Transformation - RDD-based API displayTitle: Feature Extraction and Transformation - RDD-based API license: | 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. Table of contents {:toc} Note We recommend using the DataFrame-based API, which is detailed in the [ML user guide on TF-IDF](ml-features.html#tf-idf). is a feature vectorization method widely used in text mining to reflect the importance of a term to a document in the corpus. Denote a term by `$t$`, a document by `$d$`, and the corpus by `$D$`. Term frequency `$TF(t, d)$` is the number of times that term `$t$` appears in document `$d$`, while document frequency `$DF(t, D)$` is the number of documents that contains term `$t$`. If we only use term frequency to measure the importance, it is very easy to over-emphasize terms that appear very often but carry little information about the document, e.g., "a", "the", and "of". If a term appears very often across the corpus, it means it doesn't carry special information about a particular document. Inverse document frequency is a numerical measure of how much information a term provides: `\[ IDF(t, D) = \log \frac{|D| + 1}{DF(t, D) + 1}, \]` where `$|D|$` is the total number of documents in the corpus. Since logarithm is used, if a term appears in all documents, its IDF value becomes 0. Note that a smoothing term is applied to avoid dividing by zero for terms outside the corpus. The TF-IDF measure is simply the product of TF and IDF: `\[ TFIDF(t, d, D) = TF(t, d) \cdot IDF(t, D). \]` There are several variants on the definition of term frequency and document frequency. In `spark.mllib`, we separate TF and IDF to make them flexible. Our implementation of term frequency utilizes the . A raw feature is mapped into an index (term) by applying a hash function. Then term frequencies are calculated based on the mapped indices. This approach avoids the need to compute a global term-to-index map, which can be expensive for a large corpus, but it suffers from potential hash collisions, where different raw features may become the same term after hashing. To reduce the chance of collision, we can increase the target feature dimension, i.e., the number of buckets of the hash

The default feature dimension is `$2^{20} = 1,048,576$`. Note: `spark.mllib` doesn't provide tools for text segmentation. We refer users to the and . <div class="codetabs"> <div data-lang="python" markdown="1"> TF and IDF are implemented in and . `HashingTF` takes an RDD of list as the input. Each record could be an iterable of strings or other types. Refer to the for details on the API. {% includeexample python/mllib/tfidf_example.py %} </div> <div data-lang="scala" markdown="1"> TF and IDF are implemented in and . `HashingTF` takes an `RDD[Iterable[_]]` as the input. Each record could be an iterable of strings or other types. Refer to the for details on the API. {% include_example scala/org/apache/spark/examples/mllib/TFIDFExample.scala %} </div> </div> computes distributed vector representation of words. The main advantage of the distributed representations is that similar words are close in the vector space, which makes generalization to novel patterns easier and model estimation more robust. Distributed vector representation is showed to be useful in many natural language processing applications such as named entity recognition, disambiguation, parsing, tagging and machine translation. In our implementation of Word2Vec, we used skip-gram model. The training objective of skip-gram is to learn word vector representations that are good at predicting its context in the same sentence. Mathematically, given a sequence of training words `$w1, w2, \dots, w_T$`, the objective of the skip-gram model is to maximize the average log-likelihood `\[ \frac{1}{T} \sum{t = 1}^{T}\sum{j=-k}^{j=k} \log p(w{t+j} | wt) \]` where $k$ is the size of the training window. In the skip-gram model, every word $w$ is associated with two vectors $uw$ and $vw$ which are vector representations of $w$ as word and context respectively. The probability of correctly predicting word $wi$ given word $wj$ is determined by the softmax model, which is `\[ p(wi | wj ) = \frac{\exp(u{wi}^{\top}v{wj})}{\sum{l=1}^{V} \exp(ul^{\top}v{wj})} \]` where $V$ is the vocabulary size. The skip-gram model with softmax is expensive because the cost of computing $\log p(wi | wj)$ is proportional to $V$, which can be easily in order of millions. To speed up training of Word2Vec, we used hierarchical softmax, which reduced the complexity of computing of $\log p(wi | wj)$ to $O(\log(V))$ The example below demonstrates how to load a text file, parse it as an RDD of `Seq[String]`, construct a `Word2Vec` instance and then fit a `Word2VecModel` with the input data. Finally, we display the top 40 synonyms of the specified word. To run the example, first download the data and extract it to your preferred directory. Here we assume the extracted file is `text8` and in same directory as you run the spark shell. <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details on the API. {% includeexample python/mllib/word2vecexample.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for details on the API. {% include_example

%} </div> </div> Standardizes features by scaling to unit variance and/or removing the mean using column summary statistics on the samples in the training set. This is a very common pre-processing step. For example, RBF kernel of Support Vector Machines or the L1 and L2 regularized linear models typically work better when all features have unit variance and/or zero mean. Standardization can improve the convergence rate during the optimization process, and also prevents against features with very large variances exerting an overly large influence during model training. has the following parameters in the constructor: `withMean` False by default. Centers the data with mean before scaling. It will build a dense output, so take care when applying to sparse input. `withStd` True by default. Scales the data to unit standard deviation. We provide a method in `StandardScaler` which can take an input of `RDD[Vector]`, learn the summary statistics, and then return a model which can transform the input dataset into unit standard deviation and/or zero mean features depending how we configure the `StandardScaler`. This model implements which can apply the standardization on a `Vector` to produce a transformed `Vector` or on an `RDD[Vector]` to produce a transformed `RDD[Vector]`. Note that if the variance of a feature is zero, it will return default `0.0` value in the `Vector` for that feature. The example below demonstrates how to load a dataset in libsvm format, and standardize the features so that the new features have unit standard deviation and/or zero mean. <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details on the API. {% includeexample python/mllib/standardscaler_example.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for details on the API. {% include_example scala/org/apache/spark/examples/mllib/StandardScalerExample.scala %} </div> </div> Normalizer scales individual samples to have unit $L^p$ norm. This is a common operation for text classification or clustering. For example, the dot product of two $L^2$ normalized TF-IDF vectors is the cosine similarity of the vectors. has the following parameter in the constructor: `p` Normalization in $L^p$ space, $p = 2$ by default. `Normalizer` implements which can apply the normalization on a `Vector` to produce a transformed `Vector` or on an `RDD[Vector]` to produce a transformed `RDD[Vector]`. Note that if the norm of the input is zero, it will return the input vector. The example below demonstrates how to load a dataset in libsvm format, and normalizes the features with $L^2$ norm, and $L^\infty$ norm. <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details on the API. {% includeexample python/mllib/normalizerexample.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for details on the API. {% include_example scala/org/apache/spark/examples/mllib/NormalizerExample.scala %} </div> </div> tries to identify relevant features for use in model construction. It reduces the size of the feature space, which can improve both speed and statistical learning behavior. implements Chi-Squared feature selection. It operates on labeled data with categorical features. ChiSqSelector uses the to decide which features to

It supports five selection methods: `numTopFeatures`, `percentile`, `fpr`, `fdr`, `fwe`: `numTopFeatures` chooses a fixed number of top features according to a chi-squared test. This is akin to yielding the features with the most predictive power. `percentile` is similar to `numTopFeatures` but chooses a fraction of all features instead of a fixed number. `fpr` chooses all features whose p-values are below a threshold, thus controlling the false positive rate of selection. `fdr` uses the to choose all features whose false discovery rate is below a threshold. `fwe` chooses all features whose p-values are below a threshold. The threshold is scaled by 1/numFeatures, thus controlling the family-wise error rate of selection. By default, the selection method is `numTopFeatures`, with the default number of top features set to 50. The user can choose a selection method using `setSelectorType`. The number of features to select can be tuned using a held-out validation set. The method takes an input of `RDD[LabeledPoint]` with categorical features, learns the summary statistics, and then returns a `ChiSqSelectorModel` which can transform an input dataset into the reduced feature space. The `ChiSqSelectorModel` can be applied either to a `Vector` to produce a reduced `Vector`, or to an `RDD[Vector]` to produce a reduced `RDD[Vector]`. Note that the user can also construct a `ChiSqSelectorModel` by hand by providing an array of selected feature indices (which must be sorted in ascending order). The following example shows the basic use of ChiSqSelector. The data set used has a feature matrix consisting of greyscale values that vary from 0 to 255 for each feature. <div class="codetabs"> <div data-lang="scala" markdown="1"> Refer to the for details on the API. {% include_example scala/org/apache/spark/examples/mllib/ChiSqSelectorExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for details on the API. {% include_example java/org/apache/spark/examples/mllib/JavaChiSqSelectorExample.java %} </div> </div> `ElementwiseProduct` multiplies each input vector by a provided "weight" vector, using element-wise multiplication. In other words, it scales each column of the dataset by a scalar multiplier. This represents the between the input vector, `v` and transforming vector, `scalingVec`, to yield a result vector. Denoting the `scalingVec` as "`w`", this transformation may be written as: `\[ \begin{pmatrix} v_1 \\ \vdots \\ v_N \end{pmatrix} \circ \begin{pmatrix} w_1 \\ \vdots \\ w_N \end{pmatrix} = \begin{pmatrix} v1 w1 \\ \vdots \\ vN wN \end{pmatrix} \]` has the following parameter in the constructor: `scalingVec`: the transforming vector. `ElementwiseProduct` implements which can apply the weighting on a `Vector` to produce a transformed `Vector` or on an `RDD[Vector]` to produce a transformed `RDD[Vector]`. This example below demonstrates how to transform vectors using a transforming vector value. <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details on the API. {% includeexample python/mllib/elementwiseproduct_example.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for details on the API. {% include_example scala/org/apache/spark/examples/mllib/ElementwiseProductExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for details on the API. {% include_example java/org/apache/spark/examples/mllib/JavaElementwiseProductExample.java %} </div> </div> A feature transformer that projects vectors to a low-dimensional space using PCA. Details you can read at .