Tutorial - Implementing a custom mixer in Lightwood

Introduction

Mixers are the center piece of lightwood, tasked with learning the mapping between the encoded feature and target representation

Objective

In this tutorial we'll be trying to implement a sklearn random forest as a mixer that handles categorical and binary targets.

Step 1: The Mixer Interface

The Mixer interface is defined by the BaseMixer class, a mixer needs methods for 4 tasks:

Step 2: Writing our mixer

I'm going to create a file called random_forest_mixer.py inside /etc/lightwood_modules, this is where lightwood sources custom modules from.

Inside of it I'm going to write the following code:

Step 3: Using our mixer

We're going to use our mixer for diagnosing heart disease using this dataset: https://github.com/mindsdb/benchmarks/blob/main/benchmarks/datasets/heart_disease/data.csv

First, since we don't want to bother writing a Json AI for this dataset from scratch, we're going to let lightwood auto generate one.

Now we have to edit the mixers key of this json ai to tell lightwood to use our custom mixer. We can use it together with the others, and have it ensembled with them at the end, or standalone. In this case I'm going to replace all existing mixers with this one

Then we'll generate some code, and finally turn that code into a predictor object and fit it on the original data.

Finally, we can use the trained predictor to make some predictions, or save it to a pickle for later use

That's it, all it takes to solve a predictive problem with lightwood using your own custom mixer.