I am working with a kNN model that I have built and I would like to export as an .mlmodel file. I have done so already, but it is something that could use some work in terms of efficiency.
I have python3.6, sklearn 0.19.2, and the latest version of mlcoretools.
Initially, I training my model with x_train
and x_test
as array of float64 and y_train
and y_test
as Array of int8. The y values are either 0 or 1. Using:
import coremltools
coreml_model = coremltools.converters.sklearn.convert(model)
I get this error:
ValueError: Class labels must be all of type int or all of type string.
Fine. I change the y values to int32 and it works. But the reason I wanted int8 was for memory reasons in my app. Any reason why int8 won't work?
The other issue is with the output. Currently, with my labels they are 0 or 1. However, is there a way to have model output the strings go
or stop
instead of 1 or 0? Seems that within the documentation, in the input I can have a dict, but not for outputs. Ideally, something like this would be great for output, but I cannot get it to work: labels = {“stop” : 0, “go” : 1}
CoreML currently does not support int8 as an input data type.
If you want the model to predict strings, you should use labels that are strings. That said, it's possible to change the model so that it outputs strings instead of numbers.
You will have to edit the mlmodel file, grab the "spec" object, and fill in the stringClassLabels
field of the spec.kNearestNeighborsClassifier
.