bayes classifier for assigning bank transactions to accounts

Ernst Kloppenburg ernst.kloppenburg at gmx.de
Mon Nov 2 09:52:30 EST 2026


Hello!

the (semi-)automatic assignment of transactions downloaded from the bank to accounts 
in the ledger is a feature I appreciate a lot. 

Each month though I have a few transactions that I have to assign by hand - and it is the 
same ones each month. I "happily" did it like that for quite some time but now I have 
looked into what is going wrong there. In these cases of mine one problem appears to be 
single assignments to a wrong account in the past.

Looking into the code and into prior discussion about this topic (years back, PR #839) I 
find that a single-class classifier is used per target account ("this account" vs. "not this 
account"), and then the single-class result is taken that looks most convincing. That 
comparison is a bit problematic I guess. , Frequent tokens occurring "everywhere" count 
as evidence *against* assigning to just this account.

The actual task is a multi-class classification problem (find the most likely account among 
all accounts). One could use a single Naive Bayes classifier for that.

I also wondered why the tokenization only splits at blanks, but not at any other kind of 
delimiter.

I have made some experiments using a python script that reads a gnucash file, and then 
analyses how well the last x months of bank transactions would be assigned to accounts. 
It compares:
- algorithm used in gnucash 5.17
- proposed multiclass naive bayes algorithm using the tokenization of 5.17
- the latter combined with changed tokenization: split at all non-alphanumeric characters. 
(one can still implement that in a way that keeps compatibility of the gnucash file with 
older versions.)

My results, which I find look promising:

Book: 5 accounts with learned import data, 6148 learned entries 
Evaluated: 562 transactions, 2025-07-02 to 2025-12-31 
Weekday tokens: 7 of 7 found (Montag, Dienstag, Mittwoch, Donnerstag, Freitag, Samstag, 
Sonntag) 

                     correct   no suggestion          wrong 
GnuCash 5.17      474 (84.3%)      75 (13.3%)     13 ( 2.3%) 
naive Bayes       509 (90.6%)      45 ( 8.0%)      8 ( 1.4%) 
+ tokenizer       516 (91.8%)      33 ( 5.9%)     13 ( 2.3%)
The script is attached here (no gnucash bindings required; opens the gnucash file read-
only, of course). See the docstrings for more details on the algorithms. 

It would be interesting to see results from others in order to see how the proposed 
classifier performs on data from different usage contexts.

Based on that and depending on the discussion I could propose code for inclusion into 
gnucash.

I also thought about creating an anonymized test set from my real live data. But that 
would be of limited use I guess - I expect structure of accounts and transactions to vary 
too much across the accounting use cases.

Background and Disclaimer: I have worked with all sorts of classification algorithms in the 
past. The python script attached here has been created using AI support.

Ernst

-- 
Ernst Kloppenburg
Heimerdingen, Germany

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gnucash.org/pipermail/gnucash-devel/attachments/20261102/8faa0dbd/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gnc-bayes-eval.py
Type: text/x-python3
Size: 19849 bytes
Desc: not available
URL: <http://lists.gnucash.org/pipermail/gnucash-devel/attachments/20261102/8faa0dbd/attachment.bin>


More information about the gnucash-devel mailing list