Skip to content

Commit

Permalink
chore: remove knn from classifier notebook
Browse files Browse the repository at this point in the history
A new notebook will be dedicated for knn

closes zama-ai/concrete-ml-internal#4037
  • Loading branch information
kcelia authored Oct 3, 2023
1 parent f46a112 commit 8336b47
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 77 deletions.
51 changes: 5 additions & 46 deletions docs/advanced_examples/ClassifierComparison.ipynb

Large diffs are not rendered by default.

42 changes: 11 additions & 31 deletions docs/advanced_examples/utils/classifier_comparison_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

warnings.simplefilter(action="ignore", category=FutureWarning)
import time
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -21,18 +20,15 @@
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

from concrete.ml.common.utils import get_model_name
from concrete.ml.sklearn import DecisionTreeClassifier

ALWAYS_USE_SIM = False

# pylint: disable=too-many-locals,too-many-statements,too-many-branches,invalid-name
def make_classifier_comparison(
title, classifiers, decision_level, verbose=False, show_score=False, save_plot=False
):
def make_classifier_comparison(title, classifiers, decision_level, verbose=False, save_plot=False):

h = 0.04 # Step size in the mesh
n_samples = 25 if get_model_name(classifiers[0][0]) == "KNeighborsClassifier" else 200
n_samples = 200

X, y = make_classification(
n_samples=n_samples,
Expand Down Expand Up @@ -123,16 +119,8 @@ def make_classifier_comparison(
sklearn_y_pred = sklearn_model.predict(X_test)

# Compile the Concrete ML model
if get_model_name(classifier) == "KNeighborsClassifier":
n_compile_samples = 20
else:
n_compile_samples = X_train.shape[0]

if verbose:
print(f"Inputset has: {n_compile_samples} samples used for the compilationg.\n")

time_begin = time.time()
circuit = concrete_model.compile(X_train[:n_compile_samples])
circuit = concrete_model.compile(X_train)

if verbose:
print(f"Compilation time: {(time.time() - time_begin):.4f} seconds\n")
Expand Down Expand Up @@ -190,14 +178,6 @@ def make_classifier_comparison(
np.c_[xx.ravel(), yy.ravel()],
fhe="simulate",
)
# `predict_proba` not implemented yet for Concrete KNN
# FIXME: https://github.com/zama-ai/concrete-ml-internal/issues/3962
elif get_model_name(classifier) == "KNeighborsClassifier":
sklearn_Z = sklearn_model.predict(np.c_[xx.ravel(), yy.ravel()].astype(np.float32))
concrete_Z = concrete_model.predict(
np.c_[xx.ravel(), yy.ravel()],
fhe="simulate",
)
else:
sklearn_Z = sklearn_model.predict_proba(
np.c_[xx.ravel(), yy.ravel()].astype(np.float32)
Expand Down Expand Up @@ -249,14 +229,14 @@ def make_classifier_comparison(

if i == 0:
ax.set_title(model_name + f" ({framework})", fontsize=font_size_text)
if show_score:
ax.text(
xx.max() - 0.3,
yy.min() + 0.3,
f"{score*100:0.1f}%",
size=font_size_text,
horizontalalignment="right",
)

ax.text(
xx.max() - 0.3,
yy.min() + 0.3,
f"{score*100:0.1f}%",
size=font_size_text,
horizontalalignment="right",
)

if bitwidth and framework == "Concrete ML":
ax.text(
Expand Down

0 comments on commit 8336b47

Please sign in to comment.