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