Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Faces application to dimreduction, added word embedding example
- Loading branch information
Showing
2 changed files
with
53 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/usr/bin/env python | ||
|
||
# This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation; either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# Written (W) 2011 Sergey Lisitsyn | ||
# Copyright (C) 2011 Sergey Lisitsyn | ||
|
||
from numpy import * | ||
from pylab import * | ||
from modshogun import * | ||
import random | ||
import difflib | ||
|
||
def word_kernel(words): | ||
N = len(words) | ||
dist_matrix = zeros([N,N]) | ||
for i in range(N): | ||
for j in range(i,N): | ||
s = difflib.SequenceMatcher(None,words[i],words[j]) | ||
dist_matrix[i,j] = s.ratio() | ||
dist_matrix = 0.5*(dist_matrix+dist_matrix.T) | ||
return CustomKernel(dist_matrix) | ||
|
||
print 'loading' | ||
words = [] | ||
f = open("../../data/toy/words.dat") | ||
for line in f: | ||
words.append(line[:-1]) | ||
f.close() | ||
print 'loaded' | ||
|
||
converter = KernelLocallyLinearEmbedding() | ||
converter.set_k(10) | ||
converter.set_target_dim(2) | ||
converter.parallel.set_num_threads(1) | ||
embedding = converter.embed_kernel(word_kernel(words[:200])) | ||
embedding_matrix = embedding.get_feature_matrix() | ||
fig = figure() | ||
ax = fig.add_subplot(1,1,1) | ||
ax.scatter(embedding_matrix[0,:],embedding_matrix[1,:],alpha=0.0) | ||
|
||
for i,word in enumerate(words[:100]): | ||
text(embedding_matrix[0,i],embedding_matrix[1,i],word,fontsize=8) | ||
axis('off') | ||
show() | ||
|