In [122]:
%load_ext autoreload
%autoreload 2
import re
import aphrodite.results
import sklearn.metrics
import pandas as pd
import vislab
import vislab.results
import vislab.datasets
In [123]:
label_df = vislab.datasets.behance.get_illustration_df()
In [124]:
label_df
Out[124]:
In [125]:
n_elem = len( label_df.index )
print n_elem
In [126]:
tags = label_df.columns
tags = [ tag for tag in tags if 'tag' in tag ]
print tags
In [127]:
n_tags = len( tags )
print n_tags
In [128]:
H = np.zeros( (n_tags, n_tags) )
In [129]:
for i, t1 in enumerate( tags ):
for j, t2 in enumerate( tags ):
H[i,j] = len( label_df[ label_df[t1]==True ][ label_df[t2]==True ] )
In [130]:
H[:5,:5]
Out[130]:
In [148]:
main_counts = zip( tags, list( H.diagonal() ) )
for e in main_counts:
print e[0], '\t', int( e[1] )
In [131]:
Ht = np.triu( H, 1 )
In [132]:
t_pairs = []
for i in range( 1, Ht.shape[0] ):
for j in range( i, Ht.shape[1] ):
t_pairs += [ ( tags[i], tags[j], Ht[i,j] ) ]
In [133]:
taglist1, taglist2, scorelist = zip( *t_pairs )
In [134]:
frame = pd.DataFrame( { 'tag1':taglist1, 'tag2':taglist2, 'score':scorelist } )
In [135]:
frame = frame.sort_index( by='score', ascending=False )
In [136]:
frame.head(20)
Out[136]:
In [137]:
from jinja2 import Template
TEMPLATE='''
<table>
<tr>
<td></td>
{% for row in table %}
<td>{{ row.label }}</td>
{% endfor %}
</tr>
{% for row in table %}
<tr>
<td>{{ row.label }}</td>
{% for element in row.elements %}
<td style="background-color:
rgb( {{ (128*element/hmax + 127) | round | int }},
{{ (128*element/hmax + 127) | round | int }},
{{ (128*element/hmax + 127) | round | int }} );
">{{ element | int }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
'''
def hist_to_table( h, labels ):
h = h.copy()
for i in range( h.shape[0] ):
h[i,i] = 0
hmax = h.max()
table = []
# table += [ [ '' ] + labels ]
for i in range( h.shape[0] ):
table += [ {'label':labels[i], 'elements':list(h[i,:]) } ]
template = Template( TEMPLATE )
result = template.render( table=table, hmax=hmax )
from IPython.display import HTML
return HTML( result )
DIAGONAL ELEMENTS HAVE BEEN ZEROED-OUT TO BETTER VISUALIZE
In [138]:
res = hist_to_table( H, tags ); res
Out[138]:
In [138]: