Skip to main content
hair

hair

Tiny AI, explained. hair.py is the library: one file. Each file in eg/ is a short example that uses it, and that you can read in a few minutes.

file lines of code what
hair.py 115 tools (one-line classes, csv reader, command-line flags, table printer); then data (tables of rows, one Num or Sym summary per column, distances between rows)
eg/knn.py 33 guess a row's score from its k nearest neighbours
eg/sa.py 60 simulated annealing that reads one row's goals at a time

(make loc prints these counts: no blank lines, comments or top docstring.)

Install

git clone https://github.com/timm/hair ~/gh/hair
git clone https://github.com/timm/moot ~/gits/moot   # the data

There is nothing to build, and the examples run in place: no pip install needed. You need Python 3.10 or later. If the data is somewhere else, set $MOOT.

Run

cd ~/gh/hair/eg
python3 knn.py
python3 sa.py

Each example prints one table, with one column per data file. Each example lists its flags at the top of its file. For example:

python3 knn.py --file=misc/auto93 --k=3
python3 sa.py  --file=misc/auto93,config/SS-A --runs=20

Files are named relative to --dir (default $MOOT/optimize), without .csv. --file=all (the default) runs ten files.

knn.py

Hide the scores of 100 rows. Guess each score as the mean score of its k nearest known rows. Cells show the percent error; row mean is the error when every guess is the mean of the known rows.

      auto93  SS-A  SS-M  HSQLDB  LLVM  storm  redis  socks  coc1000  pom3d
k=1       18     3     2       1    28     34     46      3       19     34
k=2       17     3     2       0    22     30     50      3       16     31
k=3       16     3     2       0    20     26     49      3       15     29
k=4       16     3     2       1    19     25     50      4       15     27
k=5       16     3     2       1    18     29     51      4       15     26
mean      37    30    59      60    50     53     49     49       16     29

sa.py

An "eval" reads the goals of one row. Cells show the "win" after that many evals: 100 means the search found the best row in all the data; 0 means it found nothing better than the median row.

      auto93  SS-A  SS-M  HSQLDB  LLVM  storm  redis  socks  coc1000  pom3d
25        94    94    82      93    95    100     91     94       57     91
50        97    95    99      98   100    100     95     98       73     96
100       99    98   100      99   100    100     97    100       80     99
200      100    99   100     100   100    100    100    100       89    100
500      100   100   100     100   100    100    100    100       97    100
1000     100   100   100     100   100    100    100    100      100    100

Data

Comma-separated text. Row one names the columns. In each name, the first character gives the type and the last gives the role:

Upper case first   numeric            (else symbolic)
+  -   last        goal to maximize / minimize
!      last        the class
X      last        ignore this column
else               an input

A cell that holds ? is unknown. For example, auto93.csv starts:

Clndrs, Volume, HpX, Model, origin, Lbs-, Acc+, Mpg+
     4,     90,  48,    78,      2, 1985, 21.5,   40

Two distances come from hair.py. distx is the distance between two rows, measured on the inputs. disty is the distance from one row to the best possible goal values: 0 is best, so a smaller disty is a better row. The "score" in the examples is disty.

Write your own example

Put a new file in eg/. Start it like this:

"""
mine: one line that says what this does.

OPTIONS:
  --dir    data directory  = $MOOT/optimize
  --file   file(s) | all   = all
  --seed   random seed     = 1
"""
import sys; sys.path[0] += "/.."; sys.dont_write_bytecode = True
from hair import *

def main():
  cli(the)
  for f in files(the):
    tbl = TAB(csv(f))
    print(name(f), len(tbl.rows), tbl.mid())

the = settings(__doc__)
if __name__ == "__main__": main()

The first import line lets the file find hair.py one directory up, and stops Python from writing __pycache__. settings reads the defaults from the OPTIONS lines. cli updates them from --key=val flags. table prints columns.

Make

make in the top directory, or in eg/, lists the rules:

make loc        lines of code in hair.py and each eg/ file
make hi it=F    render file F as colored html into docs/
make publish    render all sources, push them to the web site
make sh         a shell with a short prompt and some aliases
make it=F       edit file F in nvim

Pretty-printed sources: https://timm.github.io/hair/

Use as a package

pip install .        # from a clone; installs one module, hair
python3 -c "from hair import *; print(TAB(csv('my.csv')).mid())"

Or copy hair.py next to your own code. The examples in eg/ are not installed.

Author

Tim Menzies timm@ieee.org, MIT license.

Metadata

Release files for hair 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hair 0.1.0
File Size Uploaded Provenance Actions
hair-0.1.0.tar.gz 16.4 kB Without provenance Details for hair-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hair 0.1.0
File Interpreter ABI Platform Actions
hair-0.1.0-py3-none-any.whl Python 3 none any Details for hair-0.1.0-py3-none-any.whl

Total release size: 33.9 kB

Release files / hair-0.1.0.tar.gz

Download URL hair-0.1.0.tar.gz
Size 16.4 kB
Tags Source
SHA-256 checksum
How to use checksums
17ae6d169e6931d3ac3561583ddc1434ed775f77db74031885e745339cbf79d5
BLAKE2b-256 checksum
How to use checksums
3056f72d47ece1dde8400ba8a20cdacdf34afe0046dbc7cba20e854575c76f9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.7

Release files / hair-0.1.0-py3-none-any.whl

Download URL hair-0.1.0-py3-none-any.whl
Size 17.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
361e6d57aacef7bd51bcef9dbb8b3f1c525d5c538a39b2ba5c6dba96540a10c3
BLAKE2b-256 checksum
How to use checksums
6d5598f503e111409359ecf09ca6c47922b6f33d24e68267ddfe26ffae9cd436
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.7

Security for hair 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page