[Javascript] Classify JSON text data with machine learning in Natural

Source: Internet
Author: User
Tags readfile

In this lesson, we'll learn how to train a Naive Bayes classifier and a Logistic Regression Classifier-basic machine L Earning algorithms-on JSON text data, and classify it into categories.

While the this dataset is still considered a small dataset – only a couple hundred points of data--we'll start to get Bette R results.

The general rule was that the Logistic Regression would work better than Naive Bayes, and only if there was enough data. Since This is still a pretty small datasets, Naive Bayes works better here. Generally, Logistic Regression takes longer to train as well.

This uses data from Ana cachopo:http://ana.cachopo.org/datasets-for-single-label-text-categorization.

// Train Data  ' xxxxxx ', Label: ' Space '}]

//Load train data form the files and trainvarNatural = require (' natural '));varFS = require (' FS ');varClassifier =NewNatural. Bayesclassifier (); Fs.readfile (' Training_data.json ', ' utf-8 ',function(err, data) {if(Err) {Console.log (err); } Else {        varTrainingdata =json.parse (data);    Train (Trainingdata); }});functionTrain (Trainingdata) {Console.log ("Training"); Trainingdata.foreach (function(item) {classifier.adddocument (Item.text, Item.label);    }); varStartTime =NewDate ();    Classifier.train (); varEndTime =NewDate (); varTrainingtime = (endtime-starttime)/1000.0; Console.log ("Training Time:", Trainingtime, "seconds"); Loadtestdata ();}functionLoadtestdata () {Console.log ("Loading test Data"); Fs.readfile (' Test_data.json ', ' utf-8 ',function(err, data) {if(Err) {Console.log (err); } Else {            varTestData =json.parse (data);        Testclassifier (TestData); }    });}functionTestclassifier (testData) {Console.log ("Testing Classifier"); varNumcorrect = 0; Testdata.foreach (function(item) {varLabelguess =classifier.classify (Item.text); if(Labelguess = = =Item.label) {Numcorrect++;    }    }); Console.log ("Correct%:", numcorrect/testdata.length);
Saveclassifier (classifier)}
function Saveclassifier (classifier) {    classifier.save (function(err, classifier) {          If  (err) {            console.log (err);         Else {            console.log ("Classifier saved!" );        }    });}

In a new project, we can test the train result by:

var natural = require (' natural '); natural. Logisticregressionclassifier.load (nullfunction(err, classifier) {    if  (Err) {        console.log (err);     Else {        var testcomment = "Is this about the sun and moon?" ;        Console.log (Classifier.classify (testcomment));}    );

[Javascript] Classify JSON text data with machine learning in Natural

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.