Example

Consider sequences that are increasingly different. EDeN allows to turn them into vectors, whose similarity is decreasing.


In [1]:
%matplotlib inline

Build an artificial dataset: starting from the string 'abcdefghijklmnopqrstuvwxyz', generate iteratively strings by swapping two characters at random. In this way instances are progressively more dissimilar


In [2]:
import random

def make_data(size):
    text = ''.join([str(unichr(97+i)) for i in range(26)])
    seqs = []

    def swap_two_characters(seq):
        '''define a function that swaps two characters at random positions in a string '''
        line = list(seq)
        id_i = random.randint(0,len(line)-1)
        id_j = random.randint(0,len(line)-1)
        line[id_i], line[id_j] = line[id_j], line[id_i]
        return ''.join(line)

    for i in range(size):
        text = swap_two_characters( text )
        seqs.append( text )
        print text
        
    return seqs

In [3]:
seqs = make_data(25)


abcdefghijkmlnopqrstuvwxyz
abcdxfghijkmlnopqrstuvweyz
abcdxfgkijhmlnopqrstuvweyz
obcdxfgkijhmlnapqrstuvweyz
obcdxfgkijhvlnapqrstumweyz
obcdxfgkijhvlnapqrstumweyz
obcdxfgkujhvlnapqrstimweyz
otcdxfgkujhvlnapqrsbimweyz
otcdxfgkujhvlnapbrsqimweyz
ytcdxfgkujhvlnapbrsqimweoz
atcdxfgkujhvlnypbrsqimweoz
atcdxfikujhvlnypbrsqgmweoz
atbdxfikujhvlnypcrsqgmweoz
atbdjfikuxhvlnypcrsqgmweoz
atbdjhikuxfvlnypcrsqgmweoz
atbdjhzkuxfvlnypcrsqgmweoi
atbdjhzkuxfvlnyicrsqgmweop
awbdjhzkuxfvlnyicrsqgmteop
awbdjhzkuxfvlsyicrnqgmteop
awbdjhzkuxfvlsyicrnqtmgeop
awbsjhzkuxfvldyicrnqtmgeop
agbsjhzkuxfvldyicrnqtmweop
agbsjdzkuxfvlhyicrnqtmweop
agbsjdqkuxfvlhyicrnztmweop
agbsjdqkuxfvlhyicwnztmreop

define a function that builds a graph from a string, i.e. the path graph with the characters as node labels


In [4]:
import networkx as nx

def sequence_to_graph(seq):
    '''convert a sequence into a EDeN 'compatible' graph
    i.e. a graph with the attribute 'label' for every node and edge'''
    G = nx.Graph()
    for id,character in enumerate(seq):
        G.add_node(id, label = character )
        if id > 0:
            G.add_edge(id-1, id, label = '-')
    return G

make a generator that yields graphs: generators are 'good' as they allow functional composition


In [5]:
def pre_process(iterable):
    for seq in iterable:
        yield sequence_to_graph(seq)

initialize the vectorizer object with the desired 'resolution'


In [6]:
%%time
from eden.graph import Vectorizer
vectorizer = Vectorizer( complexity = 4 )


CPU times: user 139 ms, sys: 110 ms, total: 249 ms
Wall time: 331 ms

obtain an iterator over the sequences processed into graphs


In [7]:
%%time
graphs = pre_process( seqs )


CPU times: user 3 µs, sys: 2 µs, total: 5 µs
Wall time: 6.2 µs

compute the vector encoding of each instance in a sparse data matrix


In [8]:
%%time
X = vectorizer.transform( graphs )
print 'Instances: %d ; Features: %d with an avg of %d features per instance' % (X.shape[0], X.shape[1],  X.getnnz()/X.shape[0])


Instances: 25 ; Features: 1048577 with an avg of 599 features per instance
CPU times: user 460 ms, sys: 15.8 ms, total: 476 ms
Wall time: 473 ms

compute the pairwise similarity as the dot product between the vector representations of each sequence


In [9]:
from sklearn import metrics

K=metrics.pairwise.pairwise_kernels(X, metric='linear')
print K


[[ 1.          0.56228675  0.33769148  0.19905347  0.14222445  0.14222445
   0.11383999  0.09230998  0.07920908  0.07614308  0.08296049  0.07606405
   0.07120395  0.06749513  0.06568933  0.0623502   0.05895679  0.05887773
   0.05385955  0.05030224  0.04856311  0.05030224  0.05197472  0.05204137
   0.05552622]
 [ 0.56228675  1.          0.60896138  0.39792015  0.19601458  0.19601458
   0.16382356  0.13059198  0.11749108  0.09866611  0.1073017   0.092395
   0.08272978  0.07079764  0.06888933  0.06388353  0.06049012  0.05881107
   0.05379289  0.05023557  0.04849644  0.05183557  0.05350806  0.0535747
   0.05545955]
 [ 0.33769148  0.60896138  1.          0.5938797   0.29512062  0.29512062
   0.1921417   0.15388681  0.14078591  0.12196094  0.12892405  0.10251645
   0.09452371  0.07758577  0.07230751  0.06730171  0.0639083   0.06222925
   0.05898928  0.05365375  0.05009644  0.05525375  0.05350224  0.05694698
   0.06046482]
 [ 0.19905347  0.39792015  0.5938797   1.          0.48318995  0.48318995
   0.33217904  0.20345757  0.16263458  0.13514089  0.12196094  0.09555334
   0.08922726  0.07410751  0.06882925  0.06382345  0.05883004  0.05715099
   0.05213281  0.04857549  0.04675731  0.05191462  0.05016311  0.05190224
   0.05378709]
 [ 0.14222445  0.19601458  0.29512062  0.48318995  1.          1.
   0.45516702  0.3094078   0.23179593  0.18978087  0.15341082  0.12518504
   0.11885896  0.09572896  0.08571223  0.07888825  0.07025848  0.06714348
   0.05547852  0.05192121  0.05010303  0.06006692  0.0583154   0.05838642
   0.05709284]
 [ 0.14222445  0.19601458  0.29512062  0.48318995  1.          1.
   0.45516702  0.3094078   0.23179593  0.18978087  0.15341082  0.12518504
   0.11885896  0.09572896  0.08571223  0.07888825  0.07025848  0.06714348
   0.05547852  0.05192121  0.05010303  0.06006692  0.0583154   0.05838642
   0.05709284]
 [ 0.11383999  0.16382356  0.1921417   0.33217904  0.45516702  0.45516702
   1.          0.56602361  0.45194732  0.38840225  0.30952144  0.16177326
   0.15544719  0.10439458  0.08926954  0.08404557  0.07723397  0.07241646
   0.06412069  0.06056337  0.05707852  0.0653743   0.06195612  0.06187706
   0.06047194]
 [ 0.09230998  0.13059198  0.15388681  0.20345757  0.3094078   0.3094078
   0.56602361  1.          0.61263436  0.43219334  0.35331252  0.17667996
   0.16067117  0.10787943  0.09093621  0.08571223  0.07890064  0.07067733
   0.06238155  0.06056337  0.05867852  0.0669743   0.06355612  0.06347706
   0.05866614]
 [ 0.07920908  0.11749108  0.14078591  0.16263458  0.23179593  0.23179593
   0.45194732  0.61263436  1.          0.71893216  0.44912766  0.21006965
   0.17782149  0.12680796  0.10808653  0.10104437  0.09059641  0.07881579
   0.06738155  0.06556337  0.06207852  0.07211343  0.06869525  0.06354373
   0.05873281]
 [ 0.07614308  0.09866611  0.12196094  0.13514089  0.18978087  0.18978087
   0.38840225  0.43219334  0.71893216  1.          0.5938797   0.24686073
   0.21627923  0.16348749  0.14654428  0.1108553   0.10040734  0.08382159
   0.07238735  0.07056917  0.06708432  0.08192435  0.07850617  0.07335466
   0.0637386 ]
 [ 0.08296049  0.1073017   0.12892405  0.12196094  0.15341082  0.15341082
   0.30952144  0.35331252  0.44912766  0.5938797   1.          0.46054252
   0.27637296  0.19492565  0.16955188  0.1338629   0.11365317  0.09392896
   0.07593808  0.07411989  0.0707141   0.08373595  0.08031777  0.07516625
   0.06381107]
 [ 0.07606405  0.092395    0.10251645  0.09555334  0.12518504  0.12518504
   0.16177326  0.17667996  0.21006965  0.24686073  0.46054252  1.
   0.52707015  0.32898489  0.29693881  0.18929535  0.17072134  0.11339166
   0.08248813  0.07405323  0.0723141   0.0818453   0.0802453   0.07509379
   0.06207194]
 [ 0.07120395  0.08272978  0.09452371  0.08922726  0.11885896  0.11885896
   0.15544719  0.16067117  0.17782149  0.21627923  0.27637296  0.52707015
   1.          0.5786609   0.51929046  0.38702988  0.22668159  0.14020718
   0.09439696  0.08414387  0.08080474  0.09033595  0.08873595  0.0818453
   0.06722345]
 [ 0.06749513  0.07079764  0.07758577  0.07410751  0.09572896  0.09572896
   0.10439458  0.10787943  0.12680796  0.16348749  0.19492565  0.32898489
   0.5786609   0.99673333  0.56120377  0.42211921  0.2601352   0.15903236
   0.1112215   0.10096841  0.09267264  0.10402203  0.10562203  0.09873138
   0.08410953]
 [ 0.06568933  0.06888933  0.07230751  0.06882925  0.08571223  0.08571223
   0.08926954  0.09093621  0.10808653  0.14654428  0.16955188  0.29693881
   0.51929046  0.56120377  0.99833333  0.59940671  0.35240116  0.22983642
   0.15403034  0.14377726  0.12885817  0.14020756  0.13506263  0.12817199
   0.11355014]
 [ 0.0623502   0.06388353  0.06730171  0.06382345  0.07888825  0.07888825
   0.08404557  0.08571223  0.10104437  0.1108553   0.1338629   0.18929535
   0.38702988  0.42211921  0.59940671  0.99833333  0.60463069  0.38143914
   0.27330291  0.26304982  0.19658666  0.20793605  0.16007749  0.12643286
   0.11355014]
 [ 0.05895679  0.06049012  0.0639083   0.05883004  0.07025848  0.07025848
   0.07723397  0.07890064  0.09059641  0.10040734  0.11365317  0.17072134
   0.22668159  0.2601352   0.35240116  0.60463069  0.99833333  0.61983243
   0.30497677  0.29108732  0.22462416  0.25854412  0.21068556  0.17522274
   0.13496433]
 [ 0.05887773  0.05881107  0.06222925  0.05715099  0.06714348  0.06714348
   0.07241646  0.07067733  0.07881579  0.08382159  0.09392896  0.11339166
   0.14020718  0.15903236  0.22983642  0.38143914  0.61983243  1.
   0.54519437  0.45993613  0.25113412  0.22629083  0.17843227  0.14470858
   0.13838251]
 [ 0.05385955  0.05379289  0.05898928  0.05213281  0.05547852  0.05547852
   0.06412069  0.06238155  0.06738155  0.07238735  0.07593808  0.08248813
   0.09439696  0.1112215   0.15403034  0.27330291  0.30497677  0.54519437
   1.          0.71985284  0.28577596  0.25081358  0.20295502  0.15965422
   0.14356637]
 [ 0.05030224  0.05023557  0.05365375  0.04857549  0.05192121  0.05192121
   0.06056337  0.06056337  0.06556337  0.07056917  0.07411989  0.07405323
   0.08414387  0.10096841  0.14377726  0.26304982  0.29108732  0.45993613
   0.71985284  1.          0.50897584  0.29576424  0.24790569  0.16843551
   0.15060853]
 [ 0.04856311  0.04849644  0.05009644  0.04675731  0.05010303  0.05010303
   0.05707852  0.05867852  0.06207852  0.06708432  0.0707141   0.0723141
   0.08080474  0.09267264  0.12885817  0.19658666  0.22462416  0.25113412
   0.28577596  0.50897584  1.          0.61983243  0.26151542  0.17524386
   0.15741688]
 [ 0.05030224  0.05183557  0.05525375  0.05191462  0.06006692  0.06006692
   0.0653743   0.0669743   0.07211343  0.08192435  0.08373595  0.0818453
   0.09033595  0.10402203  0.14020756  0.20793605  0.25854412  0.22629083
   0.25081358  0.29576424  0.61983243  1.          0.49672721  0.27230242
   0.19238978]
 [ 0.05197472  0.05350806  0.05350224  0.05016311  0.0583154   0.0583154
   0.06195612  0.06355612  0.06869525  0.07850617  0.08031777  0.0802453
   0.08873595  0.10562203  0.13506263  0.16007749  0.21068556  0.17843227
   0.20295502  0.24790569  0.26151542  0.49672721  1.          0.44994304
   0.31617024]
 [ 0.05204137  0.0535747   0.05694698  0.05190224  0.05838642  0.05838642
   0.06187706  0.06347706  0.06354373  0.07335466  0.07516625  0.07509379
   0.0818453   0.09873138  0.12817199  0.12643286  0.17522274  0.14470858
   0.15965422  0.16843551  0.17524386  0.27230242  0.44994304  1.
   0.60437868]
 [ 0.05552622  0.05545955  0.06046482  0.05378709  0.05709284  0.05709284
   0.06047194  0.05866614  0.05873281  0.0637386   0.06381107  0.06207194
   0.06722345  0.08410953  0.11355014  0.11355014  0.13496433  0.13838251
   0.14356637  0.15060853  0.15741688  0.19238978  0.31617024  0.60437868
   1.        ]]

visualize it as a picture is worth thousand words...


In [10]:
import pylab as plt
plt.figure( figsize=(8,8) )
img = plt.imshow( K, interpolation='none', cmap=plt.get_cmap( 'YlOrRd' ) )
plt.show()