2021-12-14 | Blog 008

Two tweets for a couple of small, Christmas related Python projects.

First one was a Christmas tree using a Raspberry Pi. The final product was pretty simple, it just does the blinking and colour changing routine. I did pend quite a while trying to get a more complicated version working, with buttons to turn it on/off. Got sick of it so just made run on Raspberry Pi startup. The script is Python, using the GPIOzero module.

The other tweet was from trying to work out a pun between “Christmas tree” and “Isolation Forest”, the anomaly detection algorithm. A simple tree was plotted in Python by randomly generating points on the surface of a cone. A triangular distribution was used for a random height, and branch direction was also randomly generated. The correct branch length was calculated, but a bit of noise was added to make the whole thing look less cone-like and more tree-like.

After that, the iForest algorithm was applied across a range of assumed contamination ratios, 1-99%. I then made a plot for each percentage but when I put them all into a GIF I found that the colours jumped around quite a lot. This is due to the random nature of iForest, i.e. just because an instance was an anomaly at 10% contamination, doesn’t mean it’s guaranteed to be picked up if you ran it again, or even at 11%. So, to smooth this, I took the average of all the previous results. A happy accident was that the colours became non-binary, which looks really nice in my opinion.

Code here. More for my future reference than anything else.