def _extract_training_data(model, spec):
    """Extract the training data from the scikit model and add it to the CoreML spec"""

    if _is_algorithm_brute(model):
        X = model._fit_X
        if _is_valid_sparse_format(X):
            X = _unpack_sparse(X)

        for sample in X:
            coreml_sample = (
                spec.kNearestNeighborsClassifier.nearestNeighborsIndex.floatSamples.add()
            )
            for feature in sample:
                coreml_sample.vector.append(feature)

    elif _is_algorithm_kd_tree(model):
        # sklearn guarantees that tree data is not stored in a sparse format
        npdata = np.asarray(model._tree.data)
        for sample in npdata:
            coreml_sample = (
                spec.kNearestNeighborsClassifier.nearestNeighborsIndex.floatSamples.add()
            )
            for feature in sample:
                coreml_sample.vector.append(feature)

    for label in model._y:
        spec.kNearestNeighborsClassifier.int64ClassLabels.vector.append(label)
Extract the training data from the scikit model and add it to the CoreML spec
coremltools/converters/sklearn/_k_neighbors_classifier.py