Random Forest Classifier Flow

Node-RED flow for performing random forest classification. It takes input data for training and performs random forest classification using the RandomForestClassifier algorithm. The Docker image exposes an HTTP endpoint that accepts a POST request with the training data and returns the classification results, including the predicted labels, out-of-bag predictions, and confusion matrix in the response.

[{"id":"f6f2187d.f17ca8","type":"tab","label":"rf_classification","disabled":false,"info":""},{"id":"9ccf802411d02e4e","type":"http in","z":"f6f2187d.f17ca8","name":"","url":"/run","method":"post","upload":false,"swaggerDoc":"","x":380,"y":280,"wires":[["30f8de548f93e27c"]]},{"id":"238245449af60f85","type":"http response","z":"f6f2187d.f17ca8","name":"","statusCode":"","headers":{},"x":1010,"y":280,"wires":[]},{"id":"d99d8b8cfad711e1","type":"debug","z":"f6f2187d.f17ca8","name":"debug 7","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":960,"y":180,"wires":[]},{"id":"30f8de548f93e27c","type":"function","z":"f6f2187d.f17ca8","name":"RandomForest Classifier Function","func":"const rf_models = global.get('rf_models'); // rf_models contain a classifier and a regressor\n\nconst RFClassifier = rf_models.RandomForestClassifier;\n\n// Create a dummy training set\nconst trainingSet = msg.payload.dataset.trainingSet;\n\n// Create dummy predictions corresponding to the training set\nconst predictions = msg.payload.dataset.labels;\n\n// Define options for the classifier\nconst options = msg.payload.options || {\n    seed: 3,\n    maxFeatures: 0.8,\n    replacement: true,\n    nEstimators: 25\n};\n\n// Create a new classifier instance\nconst classifier = new RFClassifier(options);\n\n// Train the classifier using the training set and predictions\nclassifier.train(trainingSet, predictions);\n\n// Perform predictions on the training set\nconst result = classifier.predict(trainingSet);\n\n// Perform out-of-bag predictions\nconst oobResult = classifier.predictOOB();\n\n// Generate the confusion matrix\nconst confusionMatrix = classifier.getConfusionMatrix();\n\n// Assign the results to the message payload\nmsg.payload = {\n    result,\n    oobResult,\n    confusionMatrix\n};\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":280,"wires":[["238245449af60f85","d99d8b8cfad711e1"]]},{"id":"ec528cd7ef45ef1f","type":"http in","z":"f6f2187d.f17ca8","name":"","url":"/init","method":"post","upload":false,"swaggerDoc":"","x":520,"y":460,"wires":[["a648a747fc023c15"]]},{"id":"a648a747fc023c15","type":"http response","z":"f6f2187d.f17ca8","name":"","statusCode":"","headers":{},"x":690,"y":460,"wires":[]},{"id":"8bb802fc07545e26","type":"catch","z":"f6f2187d.f17ca8","name":"","scope":null,"uncaught":false,"x":520,"y":380,"wires":[["f0a477492bade661"]]},{"id":"f0a477492bade661","type":"function","z":"f6f2187d.f17ca8","name":"ADD ERROR INFO","func":"var payload=msg.payload;\nmsg.payload={};\n\nmsg.payload.error=msg.error;\nmsg.payload.error.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":380,"wires":[["238245449af60f85"]]},{"id":"18b497296883ac04","type":"inject","z":"f6f2187d.f17ca8","name":"Inject","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":370,"y":200,"wires":[["9995ed6a312a3b4d"]]},{"id":"9995ed6a312a3b4d","type":"function","z":"f6f2187d.f17ca8","name":"Test input","func":"msg.payload = {\n    dataset: {\n        trainingSet: [\n            [2, 4, 1],\n            [1, 3, 2],\n            [5, 1, 3],\n            [3, 2, 4],\n            [4, 5, 1],\n            [1, 2, 3],\n            [3, 5, 2],\n            [2, 1, 4]\n        ],\n        labels: [0, 1, 2, 1, 0, 1, 2, 0]\n    },\n    options: {\n        seed: 3,\n        maxFeatures: 2,\n        replacement: false,\n        nEstimators: 250\n    }\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":200,"wires":[["30f8de548f93e27c"]]}]

Collection Info

Flow Info

Created 2 years ago
Rating: not yet rated

Actions

Rate:

Node Types

Core
  • catch (x1)
  • debug (x1)
  • function (x3)
  • http in (x2)
  • http response (x2)
  • inject (x1)
Other
  • tab (x1)

Tags

  • rf
  • random-forest
  • classification
  • ml
  • ai
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option