1 """ 2 data: tables of rows, each column summarized by a Num or a Sym. 3 (c) 2026, Tim Menzies <timm@ieee.org>, MIT license 4 5 A Num is just a (n, mu, m2) tuple grown by Welford's algorithm; 6 a Sym is just a dict of counts. `add` grows either (inc=-1 7 shrinks); `mid` and `div` are their centers and spreads. 8 9 A csv file's first row names the columns. First character upper 10 case means numeric (Num), else symbolic (Sym). Last character: 11 + - goal to maximize / minimize (a y column) 12 ! the class (tbl.klass) 13 X ignore this column 14 else an input (an x column) 15 Cells holding "?" are unknown. 16 """ 17 from math import exp, log2 18 from .lib import o, isa, TINY 19 20 Sym = dict 21 Num = lambda n=0, mu=0, m2=0: (n, mu, m2) 22 23 def add(col, v, inc=1): 24 "Grow a Num or Sym by v (inc=-1 shrinks); return the new col" 25 if v == "?": return col 26 if isa(col, Sym): 27 if (c := col.get(v,0) + inc) > 0: col[v] = c 28 else: col.pop(v, None) 29 return col 30 n, mu, m2 = col 31 if (n := n + inc) <= 0: return Num() 32 d = v - mu; mu += inc * d / n 33 return (n, mu, max(0, m2 + inc * d * (v - mu))) 34 35 def adds(src, out=None): 36 "Fold a stream of values into a col (a fresh Num by default)" 37 out = Num() if out is None else out 38 for v in src: out = add(out, v) 39 return out 40 41 def fold(a, b, inc=1): 42 "Merge summary b into a (inc=-1 folds it back out)" 43 if isa(a, Sym): 44 return {k: v for k in a.keys() | b.keys() 45 if (v := a.get(k,0) + inc*b.get(k,0)) > 0} 46 (n1,mu1,m21), (n2,mu2,m22) = a, b 47 n = n1 + inc*n2 48 if n <= 0: return Num() 49 d = mu2 - mu1 50 mu = (n1*mu1 + inc*n2*mu2) / n 51 return (n, mu, max(0, m21 + inc*m22 + inc * d*d * n1*n2 / n)) 52 53 def mid(col): 54 "Center: mode of a Sym, mean of a Num" 55 return max(col, key=col.get) if isa(col, Sym) else col[1] 56 57 def div(col) -> float: 58 "Spread: entropy of a Sym, standard deviation of a Num" 59 if isa(col, Sym): 60 N = sum(col.values()) or 1 61 return -sum(v/N * log2(v/N) for v in col.values() if v) 62 n, _, m2 = col 63 return 0 if n < 2 else (m2/(n - 1))**.5 64 65 def norm(col, v) -> float|str: 66 "Map a Num's value to 0..1 by a logistic z-score" 67 if v == "?": return v 68 z = (v - col[1]) / (div(col) + TINY) 69 return 1 / (1 + exp(-1.7 * max(-3, min(3, z)))) 70 71 def TAB(src) -> o: 72 "First row names and types the columns; the rest are data" 73 tbl = o(names=next(src := iter(src)), cols={}, x=[], y=[], 74 goal={}, klass=None, rows=[], mids=None) 75 for at,s in enumerate(tbl.names): 76 tbl.cols[at] = Num() if s[0].isupper() else Sym() 77 if s[-1] == "X": continue 78 elif s[-1] == "!": tbl.klass = at; tbl.y += [at] 79 elif s[-1] in "+-": tbl.y += [at]; tbl.goal[at] = s[-1]=="+" 80 else: tbl.x += [at] 81 for row in src: addRow(tbl, row) 82 return tbl 83 84 def addRow(tbl: o, row: list, inc=1) -> None: 85 tbl.mids = None 86 (tbl.rows.append if inc > 0 else tbl.rows.remove)(row) 87 for at in tbl.cols: tbl.cols[at] = add(tbl.cols[at], row[at], inc) 88 89 def clone(tbl: o, rows=[]) -> o: 90 "Fresh Tab with the same header" 91 return TAB([tbl.names] + list(rows)) 92 93 def mids(tbl: o) -> list: 94 "Cached centroid, one mid per column; addRow invalidates" 95 tbl.mids = tbl.mids or [mid(col) for col in tbl.cols.values()] 96 return tbl.mids 97