SAS Users

Word scatter plot with SAS

Word embedding has been widely used in Natural Language Processing, where words or phrases from the vocabulary are mapped to vectors of real numbers. There are several open source tools that can be used to build word embedding models. Two of the most popular tools are word2vec and Glove, and in my experiment I used Glove. GloVe is an unsupervised learning algorithm for obtaining vector representations for words. Training is performed on aggregated global word-word co-occurrence statistics from a corpus, and the resulting representations showcase interesting linear substructures of the word vector space.

Suppose you have obtained the term frequencies from documents with SAS Text Miner and downloaded the word embedding model from Next you can extract vectors of terms using PROC SQL.

libname outlib ‘D:\temp’;
* Rank terms according to frequencies;
proc sort data=outlib.abstract_stem_freq;
by descending freq;

data ranking;
set outlib.abstract_stem_freq;

data glove;
infile “d:\temp\glove_100d_tab.txt” dlm=”09″x firstobs=2;
input term :$100. vector1-vector100;

proc sql;
create table outlib.abstract_stem_vector as
select glove.*, ranking, freq
from glove, ranking
where glove.term = ranking.word;

Now you have term vectors, and there are two ways to project 100-dimension vector data in two dimensions. One is SAS PROC MDS (multiple dimensional scaling) and the other is T-SNE (t-Distributed Stochastic Neighbor Embedding). T-SNE is a machine learning algorithm for dimensionality reduction developed by Geoffrey Hinton and Laurens van der Maaten. It is a nonlinear dimensionality reduction technique that is particularly well-suited for embedding high-dimensional data into a space of two or three dimensions, which can then be visualized in a scatter plot.

Let’s try SAS PROC MDS first. Before running PROC MDS, you need to run PROC DISTANCE to calculate distance or similarity of each pair of words. According to Glove website, the Euclidean distance (or cosine similarity) between two word vectors provides an effective method for measuring the linguistic or semantic similarity of the corresponding words. Sometimes, the nearest neighbors according to this metric reveal rare but relevant words that lie outside an average human’s vocabulary. I used Euclidean distance in my experiment and showed the top 10 words on the scatter plot as seen in Figure-1.

Word scatter plot with SAS

proc distance data=outlib.abstract_stem_vector method=EUCLID out=distances;
var interval(vector1-vector100);
id term;

ods graphics off;
proc mds data=distances level=absolute out=outdim;
id term;

data top10;
set outlib.abstract_stem_vector;
if ranking le 10 then label=term;
else label=”;
drop ranking freq;

proc sql;
create table mds_plot as
select outdim.*, label
from outdim
left join top10
on outdim.term = top10.term;

ods graphics on;
proc sgplot data = mds_plot;
scatter x=dim1 y= dim2
/ datalabel = label
markerattrs=(symbol=plus size=5)
datalabelattrs = (family=”Arial” size=10pt color=red);

SAS does not have t-sne implementation, so I used PROC IML to call the RTSNE library. There are two libraries in R that can be used for t-sne plot: TSNE and RTSNE. RTSNE was acclaimed faster than TSNE. Just as I did with the SAS MDS plot, I showed the top 10 words only, but their font sizes are varied according to their frequencies in documents.

Word scatter plot with SAS

data top10;
length label $ 20;
length color 3;
length font_size 8;
set outlib.abstract_stem_vector;
if ranking le 10 then label=term;
else label=’+’;
if ranking le 10 then color=1;
else color=2;
font_size = max(freq/25, 0.5);
drop ranking freq;

proc iml;
call ExportDataSetToR(“top10”, “vectors”);

submit /R;

set.seed(1) # for reproducibility

If you compare the two figures, you may feel that MDS plot is more symmetric than T-SNE plot, but the T-SNE plot seems more reasonable from a linguistic/semantic perspective. In Colah’s blog, he did an exploration and comparison of various dimensionality reduction techniques with well-known computer vision dataset MNIST. I totally agree with his opinions below.

It’s easy to slip into a mindset of thinking one of these techniques is better than others, but I think they’re all complementary. There’s no way to map high-dimensional data into low dimensions and preserve all the structure. So, an approach must make trade-offs, sacrificing one property to preserve another. PCA tries to preserve linear structure, MDS tries to preserve global geometry, and t-SNE tries to preserve topology (neighborhood structure).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s