Skip to content

Instructions for running the mimic learner with your own generator

Yuriy Sverchkov edited this page Oct 15, 2019 · 1 revision

Written for version 0.0.3. As of version 0.0.3 the only mimic learner implemented is a Trepan-like binary decision tree learner for numeric data.

Prep work:

To use the mimic learner we need to have a trained classifier and a generator that would generate our x-values during learning of the tree.

Training a classifier to mimic

For this example we could use the Iris UCI dataset, and a train an SVC classifier.

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC

# Load Iris
iris = datasets.load_iris()

# Split into training and testing
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.33)

# Train the SVC
svc = SVC()
svc.fit(x_train, y_train)

Make a generator

Realistically you would create a generator that models the data (x-values) well, but for the sake of demonstration, we can make a generator that uniformly samples all 4 features (Iris has 4 features) from the range [0,10] which covers the support of the Iris dataset.

def simple_generator(n):
   return(random.uniform(low=0, high=10, size=(n, 4)))

The generator function needs to take one parameter n: the number of samples to generate, and needs to return an n-by-d numpy array representing n d-dimensional samples from the data distribution, where d is the dimensionality of the data.

Using the mimic learner:

Usage is as follows:

from generalizedtrees.trepanlike import make_trepanlike_classifier

# The call to make_trepanlike_classifier returns a class.
# We pass the learned classifier object and the generator function to it:
TLC = make_trepanlike_classifier(classifier=svc, generator=simple_generator)

# Once we have a class, we make an instance of it
tlc = TLC()

# Build the tree
tlc.build()

# Once the tree is built, we can call predict() on the mimic tree.
print(tlc.predict(x_test))

Clone this wiki locally