Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need JavaScript API to map multi-class category prediction array indexes to original label names #116

Open
CodingDoug opened this issue Jul 4, 2024 · 0 comments

Comments

@CodingDoug
Copy link

I have a multi-class category task using RandomForestModel, and I'm trying to make predictions using the JS API.

For the Python API, I had to reverse engineer the fact (without the help of documentation!) that the prediction array contains probabilities for each of the label's possible categories, and more crucially, that the prediction array is ordered indexed the same as the category strings available in model.label_classes(). (The documentation needs to be improved here, but this is not the main point.)

Basically, I am doing something like this (to run my complete input dataset back through the model for some of my own diagnostics):

def pred():
    raw_preds = model.predict(df)
    preds = np.argmax(raw_preds, axis=1).tolist()
    label_classes = model.label_classes()
    cooked_preds = [ label_classes[p] for p in preds ]
    # cooked_preds now contains the label strings for all the predictions for all the input samples

For the JavaScript API, I have no similar programmatic way of mapping the prediction array to the original label classes. I would have to first know the order provided by the model as seen by the Python API, then make the assumption that the prediction array is ordered the same using the JS API. This seems fragile since the ordering isn't specified anywhere and might be non-deterministic based on how the model was built. It seems to me that it minimally needs something similar to python's model.label_classes().

As far as I can see, using Object.getOwnPropertyNames(model), the JS model object has only the following properties, which are not terribly helpful. And TBH I'm not sure how predict() even works here:

[
  'internalModel',
  'createdTFDFSignature',
  'inputFeatures',
  'numericalFeaturesIndex',
  'booleanFeaturesIndex',
  'categoricalIntFeaturesIndex',
  'categoricalSetIntFeaturesIndex'
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant