human-learn: Create a Human Learning Model by Drawing
By: Khuyen Tran
Tags: Data Visualization, Semi Supervised Learning, Data Science, Supervised Learning, Machine Learning

human-learn: Create a Human Learning Model by Drawing

GIF by Author

Nowadays, data scientists often give machine learning model data with labels so that it can figure out the rules. These rules can be used to predict the labels of new data.

Image by Author

This is convenient, but some information may be lost in this process. It is also difficult to know what is going on under the hood and why the machine learning model comes up with a particular prediction.

Instead of letting a machine learning model figure out everything, is there a way that we can use our domain knowledge to set the rules for data labeling?

Image by Author

Yes, that can be done with human-learn.

What is human-learn?

Human-learn is a tool that allows you to set the rules for data labeling using interactive drawings and custom models. In this article, we will explore how to create a model with interactive drawings using human-learn.

To install human-learn, type

pip install human-learn

I will use Iris data from sklearn to show how human-learn works.

Interactive Drawing

Drawing

Human-learn allows you to draw over your datasets, then use your drawings to convert them to models. To demonstrate how this can be useful, imagine you create a scatter plot of your dataset like below:

Image by Author

When looking at the plot above, you can see how you can split them into 3 different regions like below:

Image by Author

However, it can be difficult to write your drawing as rules and put them into a function. That is when human-learn’s interactive drawing comes in handy.

Now you can start drawing over your chart like below!

GIF by Author

Drawing instruction: Use double-click to start drawing a polygon. Then click to create an edge for the polygon. Use double-click again to stop drawing the current polygon. If this is not clear to you, you can watch a video here.

We also do the same things with other columns:

Image by Author

Image by Author

Image by Author

Creating a Model and Predict

Once we finish drawing over our dataset, we can create a model using these drawings:

Cool! By feeding our drawings to the class InteractiveClassifier , we are able to use similar methods to sklearn’s models such as fit and predict_proba .

The shape of the predictions preds is (150, 3) . Let’s peek at the first 5 rows of the preds :

Note that predict_proba gives the probability that a sample has a specific label. For example, the first prediction of [5.71326574e-01 4.28530630e-01 1.42795945e-04] means that there is a 57.13% chance that the sample has label 1, 42.85% chance that the sample has label 2, and 0.014% chance that the sample has label 0.

Predict New Data

Now let’s use our model to predict new data and compare the prediction with the real label.

Output:

The prediction is 0

The real label is 0

Nice! The prediction is the same as the real label!

Interpret the Result

To understand how the model comes up with that prediction, let’s visualize where our new sample is in scatter plots that we drew previously.

Start with creating a function to visualize our new sample.

Use the function above to plot a new sample on the petal_length vs petal_width plot whose points are colored by the probability that they have label 0.

Image by Author

Note that the yellow dots indicate the high probability that these dots have a label 0. The more purple the dots are, the less likely that these dots have a label 0.

We can see that the red dot (the new sample) is in the region with many yellow dots. But are there similar patterns in other plots? Let’s find out by applying the same function to other pairs of columns.

Image by Author

Image by Author

Image by Author

In all plots, we can see the red dot are in the region with many yellow dots! That explains why the model predicts the new sample to have a label 0. Pretty cool, isn’t it?

Predict and Evaluate Test Data

Now let’s use the model to predict all samples in the test data and evaluate its performance. Start evaluating using a confusion matrix:

array([[13, 0, 0],

[ 0, 15, 1],

[ 0, 0, 9]])

Wow cool! There are 37 true predictions and only 1 false prediction.

We can also evaluate the results using F1 score:

0.9736842105263158

Nice!

Conclusion

Congratulations! You have just learned how to generate rules to label your data by drawing on the dataset. This is not to say you should eliminate machine learning models completely but to incorporate some kind of human supervision when working with data.

I hope human-learn will encourage you to visualize your data before applying any machine learning algorithms. In the next article, I will show you other ways to create rules using human-learn.

The source code of this article could be found here:

I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.

Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: