RandomForest Regression Flow
Node-RED flow for performing random forest regression. It takes input values for the dataset and options, performs random forest regression on them, and returns the regression model and the predicted results in JSON format.
[{"id":"f6f2187d.f17ca8","type":"tab","label":"rf_regression","disabled":false,"info":""},{"id":"b0fecd8a70915807","type":"http in","z":"f6f2187d.f17ca8","name":"","url":"/run","method":"post","upload":false,"swaggerDoc":"","x":400,"y":240,"wires":[["224c911bafab82ce"]]},{"id":"c24b4cfe1301e67f","type":"http response","z":"f6f2187d.f17ca8","name":"","statusCode":"","headers":{},"x":1030,"y":240,"wires":[]},{"id":"7c522d496cb52462","type":"debug","z":"f6f2187d.f17ca8","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":980,"y":140,"wires":[]},{"id":"224c911bafab82ce","type":"function","z":"f6f2187d.f17ca8","name":"RandomForest Regression Function","func":"const rf_models = global.get('rf_reg'); // rf_models contain a classifier and a regressor\n\nconst RFRegression = rf_models.RandomForestRegression;\n\n// Retrieve x and y arrays from the message payload\nconst dataset = msg.payload.dataset; \nconst options = msg.payload.options || {\n seed: 3,\n maxFeatures: 2,\n replacement: false,\n nEstimators: 200\n};\nconst lookBackWindow = Number(msg.payload.lookBackWindow);\n\n// Check if the dataset is empty\nif (!dataset || dataset.length === 0) {\n msg.payload = { error: 'The dataset is empty.' };\n return msg;\n}\n\n// Data preprocessing - Splitting each vector based on a look-back window\nconst trainingSet = new Array(dataset.length);\nconst predictions = new Array(dataset.length);\n\n// Check if the lookBack value is a number and is less than or equal to the length of the input vector\nif (isNaN(lookBackWindow) || lookBackWindow < 0 || lookBackWindow >= dataset[0].length) {\n msg.payload = { error: 'Invalid lookBackWindow value.' };\n return msg;\n}\n\nfor (let i = 0; i < dataset.length; ++i) {\n trainingSet[i] = dataset[i].slice(0, lookBackWindow);\n predictions[i] = dataset[i][lookBackWindow];\n}\n\nconst regression = new RFRegression(options);\n\n/*\n//Debugging mode\nnode.warn(\"Training Set:\");\nnode.warn(trainingSet);\nnode.warn(\"Testing Set:\");\nnode.warn(predictions);\n*/\nregression.train(trainingSet, predictions);\nconst result = regression.predict(trainingSet);\n\nmsg.payload = { regression, result };\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":790,"y":240,"wires":[["c24b4cfe1301e67f","7c522d496cb52462"]]},{"id":"f5f4f8e9d076bca5","type":"http in","z":"f6f2187d.f17ca8","name":"","url":"/init","method":"post","upload":false,"swaggerDoc":"","x":540,"y":420,"wires":[["ab6ae888f16d0b6d"]]},{"id":"ab6ae888f16d0b6d","type":"http response","z":"f6f2187d.f17ca8","name":"","statusCode":"","headers":{},"x":710,"y":420,"wires":[]},{"id":"4ed8030897ef2ef3","type":"catch","z":"f6f2187d.f17ca8","name":"","scope":null,"uncaught":false,"x":540,"y":340,"wires":[["f0ce03a1ee711e82"]]},{"id":"f0ce03a1ee711e82","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":750,"y":340,"wires":[["c24b4cfe1301e67f"]]},{"id":"01a1d6484f4894a5","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":390,"y":160,"wires":[["a1fd5349408122e2"]]},{"id":"a1fd5349408122e2","type":"function","z":"f6f2187d.f17ca8","name":"Test input","func":"msg.payload = {\n dataset: [\n [73, 80, 75, 152],\n [93, 88, 93, 185],\n [89, 91, 90, 180],\n [96, 98, 100, 196],\n [73, 66, 70, 142],\n [53, 46, 55, 101],\n [69, 74, 77, 149],\n [47, 56, 60, 115],\n [87, 79, 90, 175],\n [79, 70, 88, 164],\n [69, 70, 73, 141],\n [70, 65, 74, 141],\n [93, 95, 91, 184],\n [79, 80, 73, 152],\n [70, 73, 78, 148],\n [93, 89, 96, 192],\n [78, 75, 68, 147],\n [81, 90, 93, 183],\n [88, 92, 86, 177],\n [78, 83, 77, 159],\n [82, 86, 90, 177],\n [86, 82, 89, 175],\n [78, 83, 85, 175],\n [76, 83, 71, 149],\n [96, 93, 95, 192]\n ],\n options: {\n seed: 3,\n maxFeatures: 2,\n replacement: false,\n nEstimators: 250\n },\n lookBackWindow: 3\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":160,"wires":[["224c911bafab82ce"]]}]