I'm using
Socket.IO to send some numbers and string to an array. However, it keeps converting everything to a String so I get an error message.
Additionally I'm using
ml.js for the models, the model logic is based of their
random forest alorithm
Here is the code I'm working with:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3019;
const rf = require('ml-random-forest');
const options = {
seed: 3,
maxFeatures: 0.9,
replacement: true,
nEstimators: 25
};
var RF = new rf.RandomForestClassifier(options);
let seperationSize;
let data = require('./myData.json');
let X = [], y = [];
let trainingSetX = [], trainingSetY = [], testSetX = [], testSetY = [];
seperationSize = 0.9 * data.length;
data = shuffleArray(data);
dressData();
function dressData() {
data.forEach((row) => {
let rowArray, typeNumber;
rowArray = Object.keys(row).map(key => parseFloat(row[key])).slice(1,14);
typeNumber = typesArray.indexOf(row.type);
console.log(typeNumber);
X.push(rowArray);
y.push(typeNumber);
});
trainingSetX = X.slice(0, seperationSize);
trainingSetY = y.slice(0, seperationSize);
testSetX = X.slice(seperationSize);
testSetY = y.slice(seperationSize);
train();
}
function train() {
RF.train(trainingSetX, trainingSetY);
test();
}
function test() {
const result = RF.predict(testSetX);
const testSetLength = testSetX.length
const predictionError = error(result, testSetY);
console.log(`Test Set Size = ${testSetLength} and number of Misclassifications = ${predictionError}`);
}
function error(predicted, expected) {
let misclassifications = 0;
for (var index = 0; index < predicted.length; index++) {
console.log(`truth : ${expected[index]} and prediction : ${predicted[index]}`);
if (predicted[index] !== expected[index]) {
misclassifications++;
}
}
return misclassifications;
}
function shuffleArray(array) {
typesArray = ['0','1'];
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('send data', function(msg){
msg = [[msg]];
console.log(msg);
console.log(RF.predict(msg));
io.emit('send data',
msg
);
});
});
http.listen(port, function(){
console.log('listening on *:' + port);
});
and the html:
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
#messages { margin-bottom: 40px }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();
$('form').submit(function(){
socket.emit('send data', $('#m').val());
$('#m').val('');
return false;
});
socket.on('send data', function(msg){
$('#messages').append($('<li>').text(msg));
window.scrollTo(0, document.body.scrollHeight);
});
});
</script>
</body>
</html>
Thanks so much!
What I have tried:
I tried sending the data like:
46 , 183 , 46 , 89.51666666666667 , 3109.1353107344635 ,55.75962079080581 , 46 , 5371 , 0.7339762336495166 , -1.2148916670171341 , '4:45', '12/07/2020'
into a double array
msg = [[msg]]
but it keeps converting it to
[
['46 , 183 , 46 , 89.51666666666667 , ' +
'3109.1353107344635 ,55.75962079080581 , 46 , 5371 , ' +
"0.7339762336495166 , -1.2148916670171341 , '4:45', " +
"'12/07/2020'"
]
]
instead of:
[
[
46,
183,
46,
89.51666666666667,
3109.1353107344635,
55.75962079080581,
46,
5371,
0.7339762336495166,
-1.2148916670171341,
'4:45',
'12/07/2020'
]
]