<jupyter_start><jupyter_text>Recognizing a DigitIn this example, we try to recognise digits of class 9 given trainingexamples from classes 0-8.<jupyter_code>import os
from IPython.display import Image
import numpy as np
from pathlib import Path
from sklearn import metrics

cwd = os.getcwd()
os.chdir(Path(cwd).parents[1])

from lsanomaly import LSAnomaly
import lsanomaly.notebooks.digits as demo

digits = os.path.join(os.getcwd(), "lsanomaly", "notebooks", "digits.png")<jupyter_output><empty_output><jupyter_text>The $8\times 8$ images of digits are loaded from [scikit-learn](https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.html). Any digit $< 9$is defined as the _inlier_ class. Thus, the digit 9 is an _outlier_ or _anomaly_. Notethis is similar to the [SVC example](https://scikit-learn.org/stable/auto_examples/classification/plot_digits_classification.htmlsphx-glr-auto-examples-classification-plot-digits-classification-py).  Anomaly Model<jupyter_code>X_train, X_test, y_train, y_test = demo.data_prep(test_size=0.5)

anomaly_model = LSAnomaly()
anomaly_model.fit(X_train, y_train)
predictions = anomaly_model.predict_proba(X_test)
fpr, tpr, thresholds = metrics.roc_curve(y_test == 9, predictions[:, -1])<jupyter_output><empty_output><jupyter_text>ROC Curve<jupyter_code>demo.plot_roc(fpr, tpr, metrics.auc(fpr, tpr))<jupyter_output><empty_output><jupyter_text>Confusion Matrix<jupyter_code>y_pred = anomaly_model.predict(X_test)

y_pred = [w if np.isreal(w) else 9 for w in y_pred]
demo.plot_confusion_matrix(y_test,
                           y_pred,
                           title='Confusion matrix',
                           normalize=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10621009866511898, 'fraction_numerical': 0.0075449796865931515, 'mean_word_length': 5.201438848920863, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '606519', 'license': 'MIT', 'n_tokens_mistral': 641, 'n_tokens_neox': 602, 'n_words': 124, 'path': 'lsanomaly/notebooks/digits.ipynb', 'repo_name': 'lsanomaly/lsanomaly', 'size': '1723'}