sklearn-onnx icon indicating copy to clipboard operation
sklearn-onnx copied to clipboard

'numpy.bool_' object has no attribute 'encode'

Open mosheduminer opened this issue 3 years ago • 3 comments

I have a sklearn.neural_network.MLPClassifier() model, where each input has two float fields, and outputs a boolean. When I call the following code:

from skl2onnx import to_onnx

onx = to_onnx(reg, X_train[:1])
with open("output.onnx", "wb") as f:
    f.write(onx.SerializeToString())

I get the following error:

Traceback (most recent call last):
  File "C:\Users\moshe\Desktop\Foolish_Wisdom\FT\ml-analysis\main.py", line 82, in <module>
    onx = to_onnx(reg, X_train[:1])
  File "C:\Users\moshe\Desktop\Foolish_Wisdom\FT\ml-analysis\venv\lib\site-packages\skl2onnx\convert.py", line 210, in to_onnx
    return convert_sklearn(model, initial_types=initial_types,
  File "C:\Users\moshe\Desktop\Foolish_Wisdom\FT\ml-analysis\venv\lib\site-packages\skl2onnx\convert.py", line 149, in convert_sklearn
    topology = parse_sklearn_model(
  File "C:\Users\moshe\Desktop\Foolish_Wisdom\FT\ml-analysis\venv\lib\site-packages\skl2onnx\_parse.py", line 623, in parse_sklearn_model
    outputs = parse_sklearn(scope, model, inputs,
  File "C:\Users\moshe\Desktop\Foolish_Wisdom\FT\ml-analysis\venv\lib\site-packages\skl2onnx\_parse.py", line 530, in parse_sklearn
    res = _parse_sklearn(
  File "C:\Users\moshe\Desktop\Foolish_Wisdom\FT\ml-analysis\venv\lib\site-packages\skl2onnx\_parse.py", line 486, in _parse_sklearn
    outputs = sklearn_parsers_map[tmodel](scope, model, inputs,
  File "C:\Users\moshe\Desktop\Foolish_Wisdom\FT\ml-analysis\venv\lib\site-packages\skl2onnx\_parse.py", line 373, in _parse_sklearn_classifier
    classes = np.array([s.encode('utf-8') for s in classes])
  File "C:\Users\moshe\Desktop\Foolish_Wisdom\FT\ml-analysis\venv\lib\site-packages\skl2onnx\_parse.py", line 373, in <listcomp>
    classes = np.array([s.encode('utf-8') for s in classes])
AttributeError: 'numpy.bool_' object has no attribute 'encode'

Is this something I can fix myself? If I need to provide more information, please tell me.

mosheduminer avatar May 28 '21 19:05 mosheduminer

Did you train it with boolean labels?

xadupre avatar Jun 03 '21 09:06 xadupre

Sorry for taking so long to respond.

Did you train it with boolean labels?

Yes, my y_train was defined as numpy.array(targets, dtype=numpy.bool_).

mosheduminer avatar Jul 08 '21 23:07 mosheduminer

Is it possible to cast the booleans into integers? That would the simplest way here. In a short term, I could include that in the error message.

xadupre avatar Jul 15 '21 13:07 xadupre