yobx.sklearn.neighbors.kneighbors#

yobx.sklearn.neighbors.kneighbors.sklearn_knn_classifier(g: GraphBuilderExtendedProtocol, sts: Dict, outputs: List[str], estimator: KNeighborsClassifier, X: str, name: str = 'knn_clf') str | Tuple[str, str][source]#

Converts a sklearn.neighbors.KNeighborsClassifier into ONNX.

The converter supports all metrics implemented by _compute_pairwise_distances(). The effective metric (and any extra parameters such as the Minkowski exponent p) are read from estimator.effective_metric_ and estimator.effective_metric_params_.

Supported metrics: "sqeuclidean", "euclidean", "cosine", "manhattan" (aliases: "cityblock", "l1"), "chebyshev", "minkowski". The "euclidean" and "sqeuclidean" metrics use com.microsoft.CDist when that domain is registered; all other metrics use the standard-ONNX path.

Full graph structure (standard-ONNX path):

X (N, F)
  │
  └─── pairwise distances ────────────────────────────────────► dists (N, M)
                                                                       │
                                                +----------------------+
                                                │
                        TopK(k, axis=1, largest=0) ──► indices (N, k)
                                                │
        Gather(training_labels_encoded) ──────► neighbor_labels (N, k)
                                                │
Reshape(-1) → OneHot(n_classes) → Reshape(N, k, n_classes) ──►
                                                │
                            ReduceSum(axis=1) ──► votes (N, n_classes)
                                                │
                ArgMax(axis=1) → Gather(classes_) ──► labels (N,)
                                                │
            Div(votes, ReduceSum(votes, axis=1)) ──► probabilities (N, n_classes)
Parameters:
  • g – graph builder

  • sts – shapes defined by scikit-learn

  • outputs – desired output names; outputs[0] receives the predicted labels and outputs[1] (if present) receives the class probabilities

  • estimator – a fitted KNeighborsClassifier

  • X – input tensor name

  • name – prefix names for the added nodes

Returns:

predicted label tensor (and optionally probability tensor as second output)

yobx.sklearn.neighbors.kneighbors.sklearn_knn_regressor(g: GraphBuilderExtendedProtocol, sts: Dict, outputs: List[str], estimator: KNeighborsRegressor, X: str, name: str = 'knn_reg') str[source]#

Converts a sklearn.neighbors.KNeighborsRegressor into ONNX.

The converter supports all metrics implemented by _compute_pairwise_distances(). The effective metric (and any extra parameters such as the Minkowski exponent p) are read from estimator.effective_metric_ and estimator.effective_metric_params_.

Supported metrics: "sqeuclidean", "euclidean", "cosine", "manhattan" (aliases: "cityblock", "l1"), "chebyshev", "minkowski". The "euclidean" and "sqeuclidean" metrics use com.microsoft.CDist when that domain is registered; all other metrics use the standard-ONNX path.

Parameters:
  • g – graph builder

  • sts – shapes defined by scikit-learn

  • outputs – desired output names

  • estimator – a fitted KNeighborsRegressor

  • X – input tensor name

  • name – prefix names for the added nodes

Returns:

predicted value tensor