Agglomerative clustering tests 2018-11-14

Link Grammar 5.4.4, test_grammar updated 2018-10-19.
This notebook is shared as static Agglomerative-Clustering-2018-11-14_.html
Output data -- Agglomerative-Clustering-2018-11-14_ directory.

Basic settings

In [1]:
import os, sys, time
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path: sys.path.append(module_path)
from src.grammar_learner.utl import UTC
from src.grammar_learner.read_files import check_dir
from src.grammar_learner.write_files import list2file
from src.grammar_learner.widgets import html_table
from src.grammar_learner.pqa_table import table_rows, wide_rows
tmpath = module_path + '/tmp/'
check_dir(tmpath, True, 'none')
table = []
start = time.time()
print(UTC(), ':: module_path =', module_path)
2018-11-14 18:49:20 UTC :: module_path = /home/obaskov/94/language-learning

Corpus test settings

In [2]:
out_dir = module_path + '/output/Agglomerative-Clustering-' + str(UTC())[:10] + '_'
corpus = 'CDS-caps-br-text+brent9mos'
corpus = 'CDS-caps-br-text'
dataset = 'LG-English'
dataset = 'LG-English-clean-clean'  # 2018-10-29: only 100% parsed 
runs = (1,1)
if runs != (1,1): out_dir += '-multi'
kwargs = {
    'left_wall'     :   ''          ,
    'period'        :   False       ,
    'context'       :   2           ,
    'min_word_count':   1           ,
    'min_link_count':   1           ,
    'max_words'     :   100000      ,
    'max_features'  :   100000      ,
    'min_co-occurrence_count':  1   ,
    'min_co-occurrence_frequency': 1e-9,
    'word_space'    :   'sparse'    ,
    'clustering'    :   ['agglomerative', 'ward'],
    'cluster_range' :   200         ,
    'clusterng_metric' : ['silhouette', 'cosine'],
    'grammar_rules' :   2           ,   # disjuncts
    'max_disjuncts' :   100000      ,
    'tmpath'        :   tmpath      , 
    'verbose'       :   'min'       ,
    'template_path' :   'poc-turtle',
    'linkage_limit' :   1000        ,
    'categories_generalization': 'off' }
lines = [
    [33, corpus , 'LG-English'                     ,0,0, 'none'  ], 
    [34, corpus , 'LG-English'                     ,0,0, 'rules' ]] 
#    [35, corpus , 'R=6-Weight=6:R-mst-weight=+1:R' ,0,0, 'none'  ], 
#    [36, corpus , 'R=6-Weight=6:R-mst-weight=+1:R' ,0,0, 'rules' ]]
# rp = module_path + '/data/CDS-caps-br-text+brent9mos/LG-English'
rp = module_path + '/data/CDS-caps-br-text/LG-English'  # shorter test 81025
rp = module_path + '/data/CDS-caps-br-text/LG-English-clean-clean'
cp = rp  # corpus path = reference_path :: use 'gold' parses as test corpus

Varying parameters: linkage, affinity, generalization, number of clusters

Linkage = "ward", affinity = "euclidean" (the only choice)

In [3]:
%%capture
line = [lines[0]]
a, _, header, log = wide_rows(lines, out_dir, cp, rp, runs, **kwargs)
display(html_table([header] + a))
In [4]:
display(html_table([header] + a))
LineCorpusParsingSpaceLinkageAffinityGen.RulesSIPAPQF1Top 5 cluster sizes
33CDS-caps-br-textLG-EnglishdALWEdwardeuclideannone200 --- 99%98%0.98[726, 44, 36, 30, 29]
34CDS-caps-br-textLG-EnglishdALWEdwardeuclideanrules182 --- 99%97%0.97[726, 44, 36, 30, 29]

The same test with basic (not-cleaned) corpus

In [5]:
%%capture
corpus = 'CDS-caps-br-text+brent9mos'
a2, _, header, log = wide_rows(lines, out_dir, cp, rp, runs, **kwargs)
corpus = 'CDS-caps-br-text'
In [6]:
display(html_table([header] + a2))
LineCorpusParsingSpaceLinkageAffinityGen.RulesSIPAPQF1Top 5 cluster sizes
33CDS-caps-br-textLG-EnglishdALWEdwardeuclideannone200 --- 99%98%0.98[726, 44, 36, 30, 29]
34CDS-caps-br-textLG-EnglishdALWEdwardeuclideanrules182 --- 99%97%0.97[726, 44, 36, 30, 29]

Varying linkage, affinity, rules generalization

In [7]:
%%capture
tbl = []
n = 0
for linkage in ['ward', 'complete', 'average']:
    for affinity in ['euclidean', 'manhattan', 'cosine']:
        if linkage == 'ward' and affinity != 'euclidean': continue
        lines[0][0] = n*2 + 1
        lines[1][0] = n*2 + 2
        n += 1
        kwargs['clustering'] = ['agglomerative', linkage, affinity]
        a, _, header, log = wide_rows(lines, out_dir, cp, rp, runs, **kwargs)
        tbl.extend(a)
        table.extend(a)
In [8]:
display(html_table([header] + table))
LineCorpusParsingSpaceLinkageAffinityGen.RulesSIPAPQF1Top 5 cluster sizes
1CDS-caps-br-textLG-EnglishdALWEdwardeuclideannone200 --- 99%98%0.98[726, 44, 36, 30, 29]
2CDS-caps-br-textLG-EnglishdALWEdwardeuclideanrules182 --- 99%97%0.97[726, 44, 36, 30, 29]
3CDS-caps-br-textLG-EnglishdALCEdcompleteeuclideannone200 --- 99%96%0.97[975, 19, 12, 4, 2]
4CDS-caps-br-textLG-EnglishdALCEdcompleteeuclideanrules182 --- 99%95%0.96[975, 19, 12, 4, 2]
5CDS-caps-br-textLG-EnglishdALCMdcompletemanhattannone200 --- 99%96%0.97[975, 19, 12, 4, 2]
6CDS-caps-br-textLG-EnglishdALCMdcompletemanhattanrules182 --- 99%95%0.96[975, 19, 12, 4, 2]
7CDS-caps-br-textLG-EnglishdALCCdcompletecosinenone200 --- 99%80%0.81[637, 42, 38, 21, 15]
8CDS-caps-br-textLG-EnglishdALCCdcompletecosinerules200 --- 99%79%0.81[637, 42, 38, 21, 15]
9CDS-caps-br-textLG-EnglishdALAEdaverageeuclideannone200 --- 99%96%0.96[1023, 1]
10CDS-caps-br-textLG-EnglishdALAEdaverageeuclideanrules177 --- 99%94%0.95[1023, 1]
11CDS-caps-br-textLG-EnglishdALAMdaveragemanhattannone200 --- 99%96%0.96[1023, 1]
12CDS-caps-br-textLG-EnglishdALAMdaveragemanhattanrules177 --- 99%94%0.95[1023, 1]
13CDS-caps-br-textLG-EnglishdALACdaveragecosinenone200 --- 99%72%0.73[1009, 13, 2, 1]
14CDS-caps-br-textLG-EnglishdALACdaveragecosinerules200 --- 99%72%0.73[1009, 13, 2, 1]

"Single" linkage setting: clustering fails

In [9]:
%%capture
n = 0
t2 = []
for linkage in ['single']:
    for affinity in ['euclidean', 'manhattan', 'cosine']:
        if linkage == 'ward' and affinity != 'euclidean': continue
        line[0][0] = n + 1
        n += 1
        kwargs['clustering'] = ['agglomerative', linkage, affinity]
        a, _, header, log = wide_rows(line, out_dir, cp, rp, runs, **kwargs)
        t2.extend(a)
        table.extend(a)
In [10]:
display(html_table([header] + t2))
LineCorpusParsingSpaceLinkageAffinityGen.RulesSIPAPQF1Top 5 cluster sizes
1CDS-caps-br-textLG-EnglishdALSEdsingleeuclideannonefail --- 0%0% --- []
2CDS-caps-br-textLG-EnglishdALSMdsinglemanhattannonefail --- 0%0% --- []
3CDS-caps-br-textLG-EnglishdALSCdsinglecosinenonefail --- 0%0% --- []

Varying number of clusters

In [11]:
%%capture
n = 0
t3 = []
for linkage in ['ward', 'complete', 'average']:
    for affinity in ['euclidean', 'manhattan', 'cosine']:
        if linkage == 'ward' and affinity != 'euclidean': continue
        print('AL' + linkage[0] + affinity[0])
        line[0][0] = n + 1
        n += 1
        for kwargs['cluster_range'] in range(100,501,100):
            kwargs['clustering'] = ['agglomerative', linkage, affinity]
            a, _, header, log = wide_rows(line, out_dir, cp, rp, runs, **kwargs)
            t3.extend(a)
            table.extend(a)
In [12]:
display(html_table([header] + t3))
LineCorpusParsingSpaceLinkageAffinityGen.RulesSIPAPQF1Top 5 cluster sizes
1CDS-caps-br-textLG-EnglishdALWEdwardeuclideannone100 --- 99%96%0.97[859, 94, 54, 39, 30]
1CDS-caps-br-textLG-EnglishdALWEdwardeuclideannone200 --- 99%98%0.98[726, 44, 36, 30, 29]
1CDS-caps-br-textLG-EnglishdALWEdwardeuclideannone300 --- 99%98%0.98[566, 41, 23, 22, 19]
1CDS-caps-br-textLG-EnglishdALWEdwardeuclideannone400 --- 99%97%0.98[484, 39, 23, 13, 12]
1CDS-caps-br-textLG-EnglishdALWEdwardeuclideannone500 --- 99%98%0.98[408, 29, 20, 12, 11]
2CDS-caps-br-textLG-EnglishdALCEdcompleteeuclideannone100 --- 99%94%0.94[1079, 30, 5, 4, 3]
2CDS-caps-br-textLG-EnglishdALCEdcompleteeuclideannone200 --- 99%96%0.97[975, 19, 12, 4, 2]
2CDS-caps-br-textLG-EnglishdALCEdcompleteeuclideannone300 --- 99%96%0.97[807, 38, 29, 18, 14]
2CDS-caps-br-textLG-EnglishdALCEdcompleteeuclideannone400 --- 99%96%0.97[698, 25, 16, 15, 14]
2CDS-caps-br-textLG-EnglishdALCEdcompleteeuclideannone500 --- 99%96%0.97[498, 89, 13, 12, 11]
3CDS-caps-br-textLG-EnglishdALCMdcompletemanhattannone100 --- 99%94%0.94[1079, 30, 5, 4, 3]
3CDS-caps-br-textLG-EnglishdALCMdcompletemanhattannone200 --- 99%96%0.97[975, 19, 12, 4, 2]
3CDS-caps-br-textLG-EnglishdALCMdcompletemanhattannone300 --- 99%96%0.97[807, 38, 29, 18, 14]
3CDS-caps-br-textLG-EnglishdALCMdcompletemanhattannone400 --- 99%96%0.97[698, 25, 16, 15, 14]
3CDS-caps-br-textLG-EnglishdALCMdcompletemanhattannone500 --- 99%96%0.97[498, 89, 13, 12, 11]
4CDS-caps-br-textLG-EnglishdALCCdcompletecosinenone100 --- 99%75%0.76[935, 38, 15, 13, 11]
4CDS-caps-br-textLG-EnglishdALCCdcompletecosinenone200 --- 99%80%0.81[637, 42, 38, 21, 15]
4CDS-caps-br-textLG-EnglishdALCCdcompletecosinenone300 --- 99%86%0.87[403, 42, 38, 28, 21]
4CDS-caps-br-textLG-EnglishdALCCdcompletecosinenone400 --- 99%91%0.92[217, 42, 38, 28, 21]
4CDS-caps-br-textLG-EnglishdALCCdcompletecosinenone500 --- 99%95%0.96[50, 42, 38, 28, 21]
5CDS-caps-br-textLG-EnglishdALAEdaverageeuclideannone100 --- 99%92%0.93[1123, 1]
5CDS-caps-br-textLG-EnglishdALAEdaverageeuclideannone200 --- 99%96%0.96[1023, 1]
5CDS-caps-br-textLG-EnglishdALAEdaverageeuclideannone300 --- 99%96%0.97[921, 2, 1]
5CDS-caps-br-textLG-EnglishdALAEdaverageeuclideannone400 --- 99%96%0.97[822, 2, 1]
5CDS-caps-br-textLG-EnglishdALAEdaverageeuclideannone500 --- 99%96%0.97[713, 4, 3, 2, 1]
6CDS-caps-br-textLG-EnglishdALAMdaveragemanhattannone100 --- 99%92%0.93[1123, 1]
6CDS-caps-br-textLG-EnglishdALAMdaveragemanhattannone200 --- 99%96%0.96[1023, 1]
6CDS-caps-br-textLG-EnglishdALAMdaveragemanhattannone300 --- 99%96%0.97[921, 2, 1]
6CDS-caps-br-textLG-EnglishdALAMdaveragemanhattannone400 --- 99%96%0.97[822, 2, 1]
6CDS-caps-br-textLG-EnglishdALAMdaveragemanhattannone500 --- 99%96%0.97[712, 4, 3, 2, 1]
7CDS-caps-br-textLG-EnglishdALACdaveragecosinenone100 --- 99%72%0.72[1009, 114, 2, 1]
7CDS-caps-br-textLG-EnglishdALACdaveragecosinenone200 --- 99%72%0.73[1009, 13, 2, 1]
7CDS-caps-br-textLG-EnglishdALACdaveragecosinenone300 --- 99%78%0.79[394, 243, 48, 41, 25]
7CDS-caps-br-textLG-EnglishdALACdaveragecosinenone400 --- 99%96%0.96[117, 115, 41, 34, 24]
7CDS-caps-br-textLG-EnglishdALACdaveragecosinenone500 --- 99%97%0.97[82, 69, 24, 23, 19]

All tests

In [13]:
display(html_table([header] + table))
LineCorpusParsingSpaceLinkageAffinityGen.RulesSIPAPQF1Top 5 cluster sizes
1CDS-caps-br-textLG-EnglishdALWEdwardeuclideannone200 --- 99%98%0.98[726, 44, 36, 30, 29]
2CDS-caps-br-textLG-EnglishdALWEdwardeuclideanrules182 --- 99%97%0.97[726, 44, 36, 30, 29]
3CDS-caps-br-textLG-EnglishdALCEdcompleteeuclideannone200 --- 99%96%0.97[975, 19, 12, 4, 2]
4CDS-caps-br-textLG-EnglishdALCEdcompleteeuclideanrules182 --- 99%95%0.96[975, 19, 12, 4, 2]
5CDS-caps-br-textLG-EnglishdALCMdcompletemanhattannone200 --- 99%96%0.97[975, 19, 12, 4, 2]
6CDS-caps-br-textLG-EnglishdALCMdcompletemanhattanrules182 --- 99%95%0.96[975, 19, 12, 4, 2]
7CDS-caps-br-textLG-EnglishdALCCdcompletecosinenone200 --- 99%80%0.81[637, 42, 38, 21, 15]
8CDS-caps-br-textLG-EnglishdALCCdcompletecosinerules200 --- 99%79%0.81[637, 42, 38, 21, 15]
9CDS-caps-br-textLG-EnglishdALAEdaverageeuclideannone200 --- 99%96%0.96[1023, 1]
10CDS-caps-br-textLG-EnglishdALAEdaverageeuclideanrules177 --- 99%94%0.95[1023, 1]
11CDS-caps-br-textLG-EnglishdALAMdaveragemanhattannone200 --- 99%96%0.96[1023, 1]
12CDS-caps-br-textLG-EnglishdALAMdaveragemanhattanrules177 --- 99%94%0.95[1023, 1]
13CDS-caps-br-textLG-EnglishdALACdaveragecosinenone200 --- 99%72%0.73[1009, 13, 2, 1]
14CDS-caps-br-textLG-EnglishdALACdaveragecosinerules200 --- 99%72%0.73[1009, 13, 2, 1]
1CDS-caps-br-textLG-EnglishdALSEdsingleeuclideannonefail --- 0%0% --- []
2CDS-caps-br-textLG-EnglishdALSMdsinglemanhattannonefail --- 0%0% --- []
3CDS-caps-br-textLG-EnglishdALSCdsinglecosinenonefail --- 0%0% --- []
1CDS-caps-br-textLG-EnglishdALWEdwardeuclideannone100 --- 99%96%0.97[859, 94, 54, 39, 30]
1CDS-caps-br-textLG-EnglishdALWEdwardeuclideannone200 --- 99%98%0.98[726, 44, 36, 30, 29]
1CDS-caps-br-textLG-EnglishdALWEdwardeuclideannone300 --- 99%98%0.98[566, 41, 23, 22, 19]
1CDS-caps-br-textLG-EnglishdALWEdwardeuclideannone400 --- 99%97%0.98[484, 39, 23, 13, 12]
1CDS-caps-br-textLG-EnglishdALWEdwardeuclideannone500 --- 99%98%0.98[408, 29, 20, 12, 11]
2CDS-caps-br-textLG-EnglishdALCEdcompleteeuclideannone100 --- 99%94%0.94[1079, 30, 5, 4, 3]
2CDS-caps-br-textLG-EnglishdALCEdcompleteeuclideannone200 --- 99%96%0.97[975, 19, 12, 4, 2]
2CDS-caps-br-textLG-EnglishdALCEdcompleteeuclideannone300 --- 99%96%0.97[807, 38, 29, 18, 14]
2CDS-caps-br-textLG-EnglishdALCEdcompleteeuclideannone400 --- 99%96%0.97[698, 25, 16, 15, 14]
2CDS-caps-br-textLG-EnglishdALCEdcompleteeuclideannone500 --- 99%96%0.97[498, 89, 13, 12, 11]
3CDS-caps-br-textLG-EnglishdALCMdcompletemanhattannone100 --- 99%94%0.94[1079, 30, 5, 4, 3]
3CDS-caps-br-textLG-EnglishdALCMdcompletemanhattannone200 --- 99%96%0.97[975, 19, 12, 4, 2]
3CDS-caps-br-textLG-EnglishdALCMdcompletemanhattannone300 --- 99%96%0.97[807, 38, 29, 18, 14]
3CDS-caps-br-textLG-EnglishdALCMdcompletemanhattannone400 --- 99%96%0.97[698, 25, 16, 15, 14]
3CDS-caps-br-textLG-EnglishdALCMdcompletemanhattannone500 --- 99%96%0.97[498, 89, 13, 12, 11]
4CDS-caps-br-textLG-EnglishdALCCdcompletecosinenone100 --- 99%75%0.76[935, 38, 15, 13, 11]
4CDS-caps-br-textLG-EnglishdALCCdcompletecosinenone200 --- 99%80%0.81[637, 42, 38, 21, 15]
4CDS-caps-br-textLG-EnglishdALCCdcompletecosinenone300 --- 99%86%0.87[403, 42, 38, 28, 21]
4CDS-caps-br-textLG-EnglishdALCCdcompletecosinenone400 --- 99%91%0.92[217, 42, 38, 28, 21]
4CDS-caps-br-textLG-EnglishdALCCdcompletecosinenone500 --- 99%95%0.96[50, 42, 38, 28, 21]
5CDS-caps-br-textLG-EnglishdALAEdaverageeuclideannone100 --- 99%92%0.93[1123, 1]
5CDS-caps-br-textLG-EnglishdALAEdaverageeuclideannone200 --- 99%96%0.96[1023, 1]
5CDS-caps-br-textLG-EnglishdALAEdaverageeuclideannone300 --- 99%96%0.97[921, 2, 1]
5CDS-caps-br-textLG-EnglishdALAEdaverageeuclideannone400 --- 99%96%0.97[822, 2, 1]
5CDS-caps-br-textLG-EnglishdALAEdaverageeuclideannone500 --- 99%96%0.97[713, 4, 3, 2, 1]
6CDS-caps-br-textLG-EnglishdALAMdaveragemanhattannone100 --- 99%92%0.93[1123, 1]
6CDS-caps-br-textLG-EnglishdALAMdaveragemanhattannone200 --- 99%96%0.96[1023, 1]
6CDS-caps-br-textLG-EnglishdALAMdaveragemanhattannone300 --- 99%96%0.97[921, 2, 1]
6CDS-caps-br-textLG-EnglishdALAMdaveragemanhattannone400 --- 99%96%0.97[822, 2, 1]
6CDS-caps-br-textLG-EnglishdALAMdaveragemanhattannone500 --- 99%96%0.97[712, 4, 3, 2, 1]
7CDS-caps-br-textLG-EnglishdALACdaveragecosinenone100 --- 99%72%0.72[1009, 114, 2, 1]
7CDS-caps-br-textLG-EnglishdALACdaveragecosinenone200 --- 99%72%0.73[1009, 13, 2, 1]
7CDS-caps-br-textLG-EnglishdALACdaveragecosinenone300 --- 99%78%0.79[394, 243, 48, 41, 25]
7CDS-caps-br-textLG-EnglishdALACdaveragecosinenone400 --- 99%96%0.96[117, 115, 41, 34, 24]
7CDS-caps-br-textLG-EnglishdALACdaveragecosinenone500 --- 99%97%0.97[82, 69, 24, 23, 19]
In [14]:
print(UTC(), ':: finished, elapsed', str(round((time.time()-start)/3600.0, 1)), 'hours')
table_str = list2file(table, out_dir + '/table.txt')
print('Results saved to', out_dir + '/table.txt')
2018-11-14 19:00:38 UTC :: finished, elapsed 0.2 hours
Results saved to /home/obaskov/94/language-learning/output/Agglomerative-Clustering-2018-11-14_/table.txt