Clustering¶
concrete_fhe_toolkit.ml.clustering ¶
Clustering task namespace: models, trainers, and metrics.
sklearn-style entry point for unsupervised grouping of encrypted samples.
FHEKMeans ¶
Bases: FHEModel
Encrypted K-Means Inference Model (cluster assignment).
Assigns encrypted samples to the nearest public centroid. Train with
FHEKMeansTrainer.fit_encrypted or provide centroids directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
centroids
|
list
|
The public list of cluster centroids. |
required |
max_distance
|
int
|
Upper bound on the squared distance from any sample to any centroid (sizes the argmin reduction). |
required |
Example
model = FHEKMeans(centroids=[[0,0], [10,10]], max_distance=200)
model.compile(dummy_inputset, batch_size=1)
Source code in src/concrete_fhe_toolkit/ml/classes.py
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 | |
FHEKMeansTrainer ¶
Bases: FHETrainer
Hybrid encrypted k-means training (fixed iterations).
Each iteration compiles one circuit with the current public centroids: it assigns every encrypted sample to its nearest centroid and returns per-cluster sums and counts. Only those aggregates are decrypted; the centroid update (division) happens clear-side, and the new centroids become the next iteration's public constants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_centroids
|
List[List[int]]
|
Public, data-independent starting centroids. |
required |
min_value
|
int
|
The lower bound for the encrypted inputs, used to dimension the FHE circuit. |
required |
max_value
|
int
|
The upper bound for the encrypted inputs, used to dimension the FHE circuit. |
required |
n_iterations
|
int
|
Fixed number of Lloyd iterations. |
5
|
simulate
|
bool
|
Run circuits in simulation. |
False
|
configuration
|
Optional[Configuration]
|
Optional FHE compiler configuration object. |
None
|
Example
trainer = FHEKMeansTrainer(initial_centroids=[[0, 0]], min_value=0, max_value=1)
Source code in src/concrete_fhe_toolkit/ml/trainers.py
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 | |
fit_encrypted ¶
fit_encrypted(X_train: List[List[int]]) -> Any
Fits the model securely on encrypted training data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X_train
|
List[List[int]]
|
Encrypted training features. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The trained FHEKMeans model. |
Source code in src/concrete_fhe_toolkit/ml/trainers.py
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 | |
nearest_centroid_inference ¶
nearest_centroid_inference(sample: Union[ndarray, List[Any]], centroids: Union[ndarray, List[List[Any]]], labels: Optional[Union[ndarray, List[Any]]] = None, *, max_distance: int = 15) -> Any
Assign an encrypted sample to the nearest public centroid (k-means step).
Returns the centroid index, or the matching label when labels is
given. max_distance must bound the squared distance to any centroid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
Union[ndarray, List[Any]]
|
The encrypted sample. |
required |
centroids
|
Union[ndarray, List[List[Any]]]
|
A list of public centroids. |
required |
labels
|
Optional[Union[ndarray, List[Any]]]
|
Optional labels for each centroid. |
None
|
max_distance
|
int
|
An upper bound on the squared distance. Defaults to 15. |
15
|
Returns:
| Type | Description |
|---|---|
Any
|
The index of the nearest centroid, or its corresponding label. |
Example
from concrete_fhe_toolkit.ml.models import nearest_centroid_inference
# Inside an FHE circuit
# cluster_idx = nearest_centroid_inference(
# enc_sample, public_centroids, max_distance=100
# )
Source code in src/concrete_fhe_toolkit/ml/models.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | |
inertia ¶
inertia(samples: List[List[Any]], centroids: List[List[int]], *, max_distance: int) -> Any
Sum of squared distances from each sample to its nearest centroid.
The standard k-means quality metric (lower is better). max_distance
must bound the squared distance from any sample to any centroid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
samples
|
List[List[Any]]
|
The encrypted samples to evaluate. |
required |
centroids
|
List[List[int]]
|
The centroids to compute distance against. |
required |
max_distance
|
int
|
The maximum squared distance bound. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The sum of squared distances (inertia). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If centroids is empty. |
Example
from concrete_fhe_toolkit.ml.clustering import inertia
# Calculate inertia for two encrypted samples
enc_inertia = inertia([[1, 2], [5, 5]], centroids=[[1, 1], [4, 4]], max_distance=100)
Source code in src/concrete_fhe_toolkit/ml/clustering.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |