📜 ⬆️ ⬇️

JavaScript Decision Trees

As a practical application to the previous article , I want to provide a tiny JavaScript library for building trees and decision forest.


Below are demos that demonstrate the classification of points on a two-dimensional plane — determining the color of a point based on the values ​​of its coordinates (x, y):


These algorithms can also be applied to classify objects of multidimensional space. Objects of the training set can contain both numeric and non-numeric attributes:

Depending on the type of attribute, different predicates are used to split the training set.
var predicates = { //     '==': function (a, b) { return a == b }, // ,       : //         (  == ""), //    -   //     '>=': function (a, b) { return a >= b } // ,      : //     ,   18  ( >= 18), //    -   }; 


Let's look at using this library on a toy example.
')

The classification of characters from the animated series The Simpsons


We will try to determine the gender of the character, based on such signs as hair length, age and weight.

Learning sample:
CharacterHair Length (inches)Weight (pounds)AgeFloor
Homer025036M
Margeten15034F
Bart290tenM
Liza678eightF
Maggiefour20oneF
Abeone17070M
Selmaeight16041F
Ottoten18038M
Krusty620045M

We form a training sample using JavaScript
 var data = [{person: 'Homer', hairLength: 0, weight: 250, age: 36, sex: 'male'}, {person: 'Marge', hairLength: 10, weight: 150, age: 34, sex: 'female'}, {person: 'Bart', hairLength: 2, weight: 90, age: 10, sex: 'male'}, {person: 'Lisa', hairLength: 6, weight: 78, age: 8, sex: 'female'}, {person: 'Maggie', hairLength: 4, weight: 20, age: 1, sex: 'female'}, {person: 'Abe', hairLength: 1, weight: 170, age: 70, sex: 'male'}, {person: 'Selma', hairLength: 8, weight: 160, age: 41, sex: 'female'}, {person: 'Otto', hairLength: 10, weight: 180, age: 38, sex: 'male'}, {person: 'Krusty', hairLength: 6, weight: 200, age: 45, sex: 'male'}]; 


Then build the decision tree:

Code for building classifiers
 var config = { //   trainingSet: data, //  ,    ,          categoryAttr: 'sex', //  ,       ignoredAttributes: ['person'] //  ,   : //    // maxTreeDepth: 10 //  ,        // entropyThrehold: 0.05 //     ,        // minItemsCount: 3 }; //    : var decisionTree = new dt.DecisionTree(config); //       : var numberOfTrees = 3; var randomForest = new dt.RandomForest(config, numberOfTrees); 


Now, with the help of constructed classifiers, you can classify other cartoon characters, for example:

Using constructed classifiers
 var comic = {person: 'Comic guy', hairLength: 8, weight: 290, age: 38}; var decisionTreePrediction = decisionTree.predict(comic); //        //   ,       var randomForestPrediction = randomForest.predict(comic); //         //  ,     , //    -   ,  ""  , //        // //   -      - , //    ,       


If you visualize the tree, you can see that age does not affect the sex of the characters :-)


Library sources are located on GitHub
Link to a demo with a symposifier classifier: jsfiddle.net/xur98
Data is taken from the presentation: www.cs.sjj.edu/faculty/lee/cs157b/ID3-AllanNeymark.ppt

As a conclusion, I want to share an idea about a search engine that ranks search results on the client side.
In response to a search query, a list of snippets is returned. Attached to each snippet is a vector of attributes describing the corresponding site. These can be either static attributes (average number of seals on a site, PageRank, etc.) or dynamic attributes indicating the degree to which the content of the site matches the search query.
In the local storage of the browser, you can save the history of user clicks - each click will correspond to an attribute vector. On these vectors, you can train a classifier (which is also stored locally), and use it to rank search results on the client side.
Perhaps this will help to ensure a balance between anonymity and ranking the results according to the user's tastes.

PS
I was informed that demos do not work in Chrome and Yandex Browser.
Browsers in which I checked the demos: Safari 6.0 and 7.0, Firefox 8.0 and 26.0, Google Chrome 31.0 - should work in them.
I will try to understand what the problem is and fix it.

Pps
Fixed Now the demos should work in Yandex Browser and Chrome.
The reason was that I connected the JS code to jsfiddle, which is hosted on a github (more detailed description here: stackoverflow.com/questions/17341122/link-and-execute-external-javascript-file-hosted-on-github )

Source: https://habr.com/ru/post/208952/


All Articles