<jupyter_start><jupyter_text>Binary Classification<jupyter_code># Prepare dataset
diabetes = datasets.load_breast_cancer()
X = diabetes.data
y = diabetes.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Feed SVM classifier
classifier = SVC(C=1.0, kernel='rbf', max_iter=-1)
classifier.fit(X_train, y_train)

y_pred = classifier.predict(X_test)
metrics.accuracy_score(y_test, y_pred)
# plot confusion matrix
confusion_matrix = metrics.confusion_matrix(y_test, y_pred)
ax = plt.subplot(1,2,1)
MetricsVisualization.plot_confusion_matrix(confusion_matrix, list(diabetes.target_names), ax)
# plot confusion table sample
ax = plt.subplot(1,2,1)
MetricsVisualization.plot_confusion_table_sample(ax)<jupyter_output><empty_output><jupyter_text>Multiclass Classification<jupyter_code># Prepare dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Feed SVM classifier
classifier = SVC(C=1.0, kernel='rbf', max_iter=-1)
classifier.fit(X_train, y_train)

y_pred = classifier.predict(X_test)
metrics.accuracy_score(y_test, y_pred)
# plot confusion matrix
confusion_matrix = metrics.confusion_matrix(y_test, y_pred)
ax = plt.subplot(1,2,1)
MetricsVisualization.plot_confusion_matrix(confusion_matrix, list(diabetes.target_names), ax)<jupyter_output><empty_output><jupyter_text>General Functions<jupyter_code>classification_analysis = ClassificationAnalysis()
# Training
y_true_train = np.array([0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0])

y_pred_train = np.array([0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0])

y_score_train = np.array([[0.94681823, 0.05318171], [0.9670934 , 0.03290659], [0.7758419 , 0.2241581 ], [0.81656456, 0.1834354 ], [0.0285829 , 0.97141707], 
                          [0.19505125, 0.8049488 ], [0.33942837, 0.6605716 ], [0.48501864, 0.5149814 ], [0.98071676, 0.01928318], [0.87189126, 0.12810875],
                          [0.7784543 , 0.22154574], [0.00178004, 0.9982199 ], [0.12195235, 0.87804765], [0.04656461, 0.9534354 ], [0.01764188, 0.98235804],
                          [0.8902476 , 0.10975237], [0.07897467, 0.9210253 ], [0.06069772, 0.93930227], [0.4672949 , 0.53270507], [0.8613442 , 0.13865575]])

train_loss = [0.87875825, 0.6694519, 0.5308192, 0.4359323, 0.39127058, 0.38543105, 0.3794922, 0.37403542, 0.36887342, 0.36388054, 0.3590037, 0.35423213, 
                0.3495689, 0.34501785, 0.34058037, 0.3362563, 0.33204418, 0.3279421]

classification_analysis.add_train_epoch_metircs(MetricsClaculation(y_true_train, y_pred_train, y_score_train, train_loss[-3], number_of_classes = 2, classes_labels = ['ants', 'bees']))

y_pred_train[0] = 1
y_score_train[0] = [0.05318171, 0.94681823]
classification_analysis.add_train_epoch_metircs(MetricsClaculation(y_true_train, y_pred_train, y_score_train, train_loss[-2], number_of_classes = 2, classes_labels = ['ants', 'bees']))

y_pred_train[1] = 1
y_score_train[1] = [0.05318171, 0.94681823]
classification_analysis.add_train_epoch_metircs(MetricsClaculation(y_true_train, y_pred_train, y_score_train, train_loss[-1], number_of_classes = 2, classes_labels = ['ants', 'bees']))
# Validation
y_true_validation = np.array([0, 0, 0, 0, 0, 1, 0, 1, 1, 0])

y_pred_validation = np.array([0, 0, 0, 0, 0, 1, 0, 1, 1, 0])

y_score_validation = np.array([[0.98006827, 0.01993176], [0.9542736 , 0.04572643], [0.6717632 , 0.3282368 ], [0.94712925, 0.05287072], [0.9910054 , 0.00899466],
                    [0.07913006, 0.92087   ], [0.868331  , 0.13166901], [0.27835694, 0.72164303], [0.04550473, 0.95449525], [0.5254467 , 0.4745533 ]])

validation_loss = [0.6948844, 0.5384914, 0.44820136, 0.3862345, 0.3823765, 0.37943494, 0.375823, 0.3716417, 0.3673169, 0.36305183]

classification_analysis.add_val_epoch_metircs(MetricsClaculation(y_true_validation, y_pred_validation, y_score_validation, validation_loss[-1], number_of_classes = 2, classes_labels = ['ants', 'bees']))

y_pred_validation[0] = 1
y_score_validation[0] = [0.05318171, 0.94681823]
classification_analysis.add_val_epoch_metircs(MetricsClaculation(y_true_validation, y_pred_validation, y_score_validation, validation_loss[-1], number_of_classes = 2, classes_labels = ['ants', 'bees']))

y_pred_validation[1] = 1
y_score_validation[1] = [0.05318171, 0.94681823]
classification_analysis.add_val_epoch_metircs(MetricsClaculation(y_true_validation, y_pred_validation, y_score_validation, validation_loss[-1], number_of_classes = 2, classes_labels = ['ants', 'bees']))

MetricsVisualization.show_all(classification_analysis)
# Steps Timing
steps_timing = {'backward': [0.000946, 0.000526, 0.000481, 0.002628, 0.001772, 0.001096, 0.000719, 0.001165, 0.000606, 0.000647],
                'd_transfer': [0.001719, 0.001868, 0.001635, 0.001331, 0.002706, 0.002587, 0.001641, 0.001542, 0.001381, 0.001378],
                'forward': [0.022014, 0.020824, 0.020791, 0.020875, 0.022043, 0.02185, 0.023757, 0.020773, 0.0208, 0.02162], 
                'loss': [0.000431, 0.000205, 0.000193, 0.000806, 0.000211, 0.00016, 0.000253, 0.000233, 0.000235, 0.00034],
                'optimizer': [0.001157, 0.000828, 0.0008, 0.000857, 0.000862,0.000772, 0.000815, 0.001249, 0.000899, 0.003832],
                'prediction': [0.000668, 0.00019, 0.00017, 0.000214, 0.000158, 0.000143, 0.000193, 0.000154, 0.000219, 0.000211],
                'zero_grads': [0.000809, 0.000537, 0.000471, 0.000489, 0.000429, 0.000446, 0.000298, 0.000461, 0.000426, 0.000449]}

MetricsVisualization.steps_timing_visualization(steps_timing)
# Line plot
acc_scores = {'train_acc': [0.568, 0.606, 0.738, 0.77, 0.795, 0.801, 0.804, 0.801, 0.801, 0.801],
              'val_acc': [0.65, 0.725, 0.838, 0.85, 0.85, 0.862, 0.85, 0.85, 0.85, 0.85]}

MetricsVisualization.line_plot(acc_scores['train_acc'], acc_scores['val_acc'], 'Accuracy')
# show overall metrics
overall_metrics = {'Accuracy': 0.862, 'Cohen_Kappa': 0.726, 'F1_score': 0.862, 'Precision': 0.867, 'Recall': 0.864, 'Specificity': 0.864}

MetricsVisualization.show_overall_metrics(overall_metrics)
# show classes metrics
classes_metrics = {'F1_score': [0.857, 0.842], 'Precision': [0.8  , 0.914], 'Recall': [0.923, 0.78 ], 'Specificity': [0.78 , 0.923]}

MetricsVisualization.show_classes_metrics(classes_metrics, list(diabetes.target_names))
# show overall metrics
fpr = [0., 0., 0., 0.051, 0.051, 0.077, 0.077, 0.103, 0.103, 0.205, 0.205, 0.282, 0.282, 0.308, 0.308, 0.333, 0.333, 0.385, 0.385, 0.897, 0.897, 1.]
roc_auc = 0.922
tpr = [0., 0.024, 0.61 , 0.61 , 0.732, 0.732, 0.805, 0.805, 0.829, 0.829, 0.878, 0.878, 0.902, 0.902, 0.927, 0.927, 0.951, 0.951, 0.976, 0.976, 1., 1.]

MetricsVisualization.show_roc_curve(2, fpr, tpr, roc_auc)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16403132850598492, 'fraction_numerical': 0.24959361607802572, 'mean_word_length': 4.8094420600858365, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '153523', 'license': 'MIT', 'n_tokens_mistral': 3958, 'n_tokens_neox': 2857, 'n_words': 521, 'path': 'example/example.ipynb', 'repo_name': 'Samer92/classification-analysis', 'size': '6767'}