Reading a Text File of Numbers into a JavaScript Matrix Using Node.js

I’ve been looking at the idea of creating a neural network using plain JavaScript running in the Node.js system. A basic utility task is to read a text file of training data into memory as a numeric matrix.

For my demo, I created a small text file that has five lines of data from the well-known Iris Dataset:

5.5, 2.5, 4.0, 1.3, 0, 1, 0
6.3, 3.3, 6.0, 2.5, 0, 0, 1
5.8, 2.7, 5.1, 1.9, 0, 0, 1
7.1, 3.0, 5.9, 2.1, 0, 0, 1
6.3, 2.9, 5.6, 1.8, 0, 0, 1

Before starting, I coded up function to create and print a two-dimensional numeric matrix.

There are many ways to read data from file in using the Node.js system. My approach reads the entire file contents into memory, splits on “\n” into an array of strings, then parses out each line:

let fs = require('fs');
let all = fs.readFileSync('iris_five.txt', "utf8");
all = all.trim();  // final crlf in file
let lines = all.split("\n");
let n = lines.length;
let m = matrixMake(n, 7, 0.0);  // numeric

for (let i = 0; i < n; ++i) {  // each line
  let tokens = lines[i].split(",");
  for (let j = 0; j < 7; ++j) {  // each val curr line
    m[i][j] = parseFloat(tokens[j]);
  }
}
matrixPrint(m, 1);  // 1 decimal

This approach isn’t very robust, and won’t work for huge files, but it’s simple and effective for basic neural network purposes. Here’s a version of the code that’s been refactored into a function that resembles the Python NumPy loadtxt() function:

let fs = require('fs');
function loadTxt(fn, delimit, usecols) {
  let all = fs.readFileSync(fn, "utf8");  // giant string
  all = all.trim();  // strip final crlf in file
  let lines = all.split("\n");
  let rows = lines.length;
  let cols = usecols.length;
  let result = matrixMake(rows, cols, 0.0); 
  for (let i = 0; i < rows; ++i) {  // each line
    let tokens = lines[i].split(delimit);
    for (let j = 0; j < cols; ++j) {
      result[i][j] = parseFloat(tokens[usecols[j]]);
    }
  }
  return result;
}

data_x = loadTxt(".\\iris_train.txt", ",", [0,1,2,3]);
data_y = loadTxt(".\\iris_train.txt", ",", [4,5,6]); 


From the move “The Matrix” (1999) – the white rabbit girl, the mysterious woman in the red dress, Trinity, Persephone.

This entry was posted in JavaScript, Machine Learning. Bookmark the permalink.