PolyStat

Monday, July 25, 2016

How Bag
of Words Model Can Work in Text Mining with Python?

The Bag
of Words model, BOW, is a popular representation which is commonly used in natural
language processing applications. In this model, a text document including
several sentences and paragraphs is represented as the bag of the words
included in that document, without considering syntax and the order of words.
The main idea is to quantize each extracted key point into one of categorical
words, and then represent each document by a histogram of the categorical
words. The BOW feature design creates a vocabulary out of all the words in the
training set and, for each example, creates a vector of word counts pertaining
to that instance. Since the vector holds a place for each word in the
vocabulary, the resulting BOW matrix is sparse (mostly zeros) because most
words in the vocabulary don't appear in a given example.

One of
its universal application is in document classification. In this case, the
frequency of occurrence of each word from a dictionary is considered as an
attribute for learning a classifier [1]. Salton and McGill proposed a Keyword
Classification method to classify the documents into different categories
providing a synonym dictionary for document indexing and query retrieval [2].
Furthermore, a statistical framework for generalizing the BOW representation is
presented by Zhang et.al. for image processing inspired from text mining applications
[3]. Voloshynovskiy et. al. also performs a survey for the analysis of the performance
of BOW method to present a better understanding of this technique in terms of
the robustness and accuracy in decision making. They also accomplished a
successful experiment on real image data [4].

After we
determine how many documents we have, we simply put all into a single text file. Then we
will replace all the punctuation such as $, %, & and @as well
as some digits or specific terms such as WWW and com with
blank space. After cleaning text file, we will count the frequency of each word
in our text file, and rank them according to their frequency. We will always choose
the top N words from the text file, and N is a user-defined parameter.

Here is a demo of how BOW
works on a text data. For the first paragraph of this blog post, we have:

Ever wondering what data structure is used at the core engine for class of games like, Othello, Go, etc., and almost most of the board games you can think of! I encourage you to read this blog post then ;)

Recently, AI (Artificial Intelligence) and ML (Machine Learning) have successfully developed a tool to defeat human expert at Go game that was perceived to think of as one of the most difficult board games for quite many years, but thanks to deep RL (Reinforcement Learning) that used the MCTS as the main data structure to make this dream come true! Except the undeniable role of deep learning in this regard, MCTS is the main data structure that was considered to address this problem and similar ones since 2006. The first formalization of Kocsis and Szepesvári toward upper confidence bounds for trees, that heavily owes to John Von Neumann's 1928 minimax theorem known as the breakthrough for adversarial tree search methods, makes all the way up to explore and exploit the optimal policy to win 3 out of 4 against professional Go player Lee Sedol known as the human champion of Go.

The goal of Monte Carlo tree search is to analyse the most promising moves, so that the search tree grows based on random sampling of the search space. However, MCTS is limited to the games that have a finite number of possible moves in each state, as well as a finite length. Representing all possible states of the game in the tree is the main aim of this data structure. The root node of the tree is the initial game state. For every valid move from a node, there is a child node representing the new game state if that move is chosen. MCTS can theoretically be applied to any domain that can be described in terms of (state, action) pairs and simulation used to forecast outcomes.

The process of MCTS algorithm can be broken down into the following steps:

Expansion, If L is a not a terminal node, then create one or more child nodes and select one C.

Simulation, Run a simulated playout from C until a result is achieved.

Backpropagation, Update the current move sequence with the simulation result.

Kocsis and Szepesvári recommend to choose each node of the MCTS as the first step of the above algorithm that maximizes the following expression,

\[ \frac{w_i}{n_i} + c \sqrt{\frac{\ln{t}}{n_i}} \]

where \(w_i\) stands for the number of wins after the i-th move, \(n_i\) stands for the number of simulations after the i-th move, \(c\) is the exploration parameter—theoretically equal to \(\sqrt{2}\); in practice usually chosen empirically, \(t\) stands for the total number of simulations for the node considered. It is equal to the sum of all the \(n_i\).

Sunday, July 24, 2016

This text is a bit technical. It is the result of a discussion with Andrea Lodi concerning the robustness of the support vector machines (SVM) the famous and the widely-used classifier in Machine Learning.

It is well-known that the median is more robust compared to the mean.
The mean is the solution to an L2 quadratic minimization (least squares), and median is
the solution to an L1 linear minimization or (least absolute deviation).
Quantile regression coincides with the median regression for tau=0.5. The hinge loss (SVM), is a combination of a constant and a linear loss, so the behaviour looks like a skewed median.

Here I
examine support vector machines, and compare it with these two linear estimation techniques of a line.
The subject of robustness of the SVM is rarely studied, and we do not
know much about it. It is interesting to see how much outliers affect
the support vector machine (SVM) classifier. Let's take the simplest version, i.e. the linear SVM. As the SVM classification
does not correspond to a statistical model, it is difficult to say what
outlier means in this context, but at least we can talk about the
influential observations and make a sort of cook’s distance by removing
data and re-fitting.
The linear SVM is the hinge loss, but with L2

penalization over the classification coefficients. Instead least
squares and the median regression include no penalization. Studying the
behavior of the SVM in the presence of outliers is closely related to
studying the influence function of a L2 penalized S-Estimator.
The support vector machines is a hinge loss regression while data are
separable. The penalization in the case of separable data is there only
to take care of maximizing the margin. For non-separable data it will
play a totally different role, it maximizes a hypothetical generalized
margin which has no geometrical interpretation.
For non-separable data the L2norm penalization plays more role, as often more data fall on the hypothetical generalized margin.

Let’s start with generating some data and fitting a separating line
that maximizes the margin. Now lets fit a SVM
classifier. The support vectors are highlighted in blue, least squares
in green, and least absolute (median regression) in magenta. Pay attention to magenta line while you the following plots.

Non-separable data

Of course most of classifying data are not separable. It is also
possible that somebody made a mistake and swaped +1 class with -1.
Therefore it is more realistic to consider a non-separable data set.
Therefore, we modify our simulated data a bit.

As you see there are more blue dots now. Since misclassified data now
fall on the hypothetical generalized margin. I seems SVM is more
sensitive to data separability compared with least squares, or median
regression.

SVM Breakdown

Now lets see if we can add some contamination to our data and influence the separating line.

Lesson

The lesson is hidden in magenta lines by comparing the above plots. Most of real data are non-separable. Be careful with using SVM in the
presence of mistakenly assigned lables or in the presence of outliers.
If you are unsure about the presence of outliers, always keep median regression (magenta) as a competitor for SVM.