'ML5 at model.classify(): TypeError: Cannot convert undefined or null to object

I am using ML5 version 0.4.3, and I'm attempting to do classification in a React app.

When I call model.classify(), I'm getting the following error:

TypeError: Cannot convert undefined or null to object
    at entries (<anonymous>)
    at index.js:676
    at Array.map (<anonymous>)
    at t.<anonymous> (index.js:668)
    at x (runtime.js:62)
    at Generator._invoke (runtime.js:296)
    at Generator.t.<computed> [as next] (runtime.js:114)
    at i (asyncToGenerator.js:17)
    at asyncToGenerator.js:28

I am totally stumped. Have tried entering data as an array of a single object [{ }] and as the object itself. Can anyone help me understand what's going on and how to fix it?

My input looks something like this

let inputs = {
  male: 1,
  female: 0,
  dob: 641710800000,
  // have more, but keeping it simple for this example...
}

and my output looks like this

let output = [0, 1] // or [1, 0], depending if they have a job or not

Here's my full code below:

people_arr = json.voters_arr;
keys = ["male", "female", "dob"];

let model_options = {
  inputs: keys,
  outputs: ["job"],
  task: "classification"
};

let model = ml5.neuralNetwork(model_options);

for (let person of people_arr) {
  let inputs = {
    male: person.male, // 0 or 1
    female: person.female, // 0 or 1
    dob: person.dob // milliseconds
  };

  let output = {};
  output.job = person.job; // [0, 1] or [1, 0]
  model.addData(inputs, output);
}

model.normalizeData();

let train_options = { epochs: 100 }
model.train(train_options, whileTraining);
.then(() => {
  console.log("pre classify");
  return model.classify({ male: 0, female: 1, dob: 463726800000 }); // <-- error happening here
})
.then((err, results) => {
  if (err) { console.log("error") }

  else {
    let new_arr = results.splice(100);
    console.log("results : ", new_arr);
    setValues({...values, results: new_arr })
  }
})
.catch((err) => { console.log("err : ", err) });


Solution 1:[1]

I am having the same problem, and although this is not the best solution, this is what I found.

using nn.classify() results in a a really messy error but,

using nn.predict() does not. It seems though that it is (or will become soon) deprecated.

I do not know why the error is happening, but I also found that putting the label (your output) as a string instead of an int works too.

Instead of your output looking like [0,1] or [1,0] you can do "0" or "1"

when you call model.normalizeData() I think (not 100% sure) that it onehot encode your label for you, if it's a string apparently.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tissuebox