apple / coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
https://coremltools.readme.io
BSD 3-Clause "New" or "Revised" License
4.42k stars 640 forks source link

coremltools converter not supporting sparse matrix #374

Open mininny opened 5 years ago

mininny commented 5 years ago

On coremltools@b3.0, converting Scikit-KNN model into MLModel doesn't work as expected. The converter doesn't supports sparse matrix, but is able to convert a dense matrix.

KNN models are formatted with sparse matrix, and the converter doesn't accept the values. The following error describes the converter not converting knnmodels in sparse matrix. File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/coremltools/converters/sklearn/_converter.py", line 147, in convert sk_obj, input_features, output_feature_names, class_labels = None) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/coremltools/converters/sklearn/_converter_internal.py", line 300, in _convert_sklearn_model last_spec = last_sk_m.convert(last_sk_obj, current_input_features, output_features)._spec File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/coremltools/converters/sklearn/_k_neighbors_classifier.py", line 51, in convert return _MLModel(_convert_k_neighbors_classifier(model, input_name, output_name)) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/coremltools/converters/sklearn/_k_neighbors_classifier.py", line 100, in _convert_k_neighbors_classifier _extract_training_data(model, spec) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/coremltools/converters/sklearn/_k_neighbors_classifier.py", line 111, in _extract_training_data coreml_sample.vector.append(feature) TypeError: <1x337 sparse matrix of type '<class 'numpy.float64'>' with 16 stored elements in Compressed Sparse has type csr_matrix, but expected one of: int, long, float

This error can be resolved by modifying the _fit_X value of the KNN model with todense() and tolist() functions, but native implementations would be useful.

den-run-ai commented 1 year ago

@the-neural-networker sparse matrix not implemented