Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
4.7 kB
4
Indexable
Never
knn_roc_auc_score_1 = 0
knn_roc_auc_score_5 = 0
knn_roc_auc_score_10 = 0
cluster_roc_auc_score_1 = 0
cluster_roc_auc_score_5 = 0
cluster_roc_auc_score_10 = 0
minkowski_distance_roc_auc_score_1 = 0
minkowski_distance_roc_auc_score_2 = 0
minkowski_distance_roc_auc_score_inf = 0
cosine_distance_roc_auc_score = 0
mahalanobis_distance_roc_auc_score = 0
lof_roc_auc_score = 0


for i in tqdm.tqdm(range(10)):
    train_data = orig_train_data[orig_train_label==i]
    test_data, test_label = resample(orig_test_data,orig_test_label,target_label=i,outlier_ratio=0.1)
    
    train_label = orig_train_label[orig_train_label==i]
    for idx, val in enumerate(train_label):
        train_label[idx] = 0 if val == i else 1
    for idx, val in enumerate(test_label):
        test_label[idx] = 0 if val == i else 1
        
    # KNN
    k=[1, 5, 10]
    knn_distance, prediction = KNearestNeighborAD(n=0.1, train_data=train_data, test_data=test_data, k=k)


    # Cluster-based
    k=[1, 5, 10] 
    k_nearest_cluster_distance, prediction = KMeansClusterAD(k=k, n=0.1, train_data=train_data, test_data=test_data)
        
    # Distance-based
    k=[5]
    r=[1, 2, np.inf]
    k_nearest_minkowski_distance, prediction = MinkowskiDistanceAD(n=0.1, test_data=test_data)
        
    # Density-based
    local_outlier_factor, prediction = LocalOutlierFactorAD(n=0.1, test_data=test_data)
    if i == 0:
        tsne = TSNE(n_components=2, init='pca')
        test_data_tsne = tsne.fit_transform(test_data)
            
        plt.figure(figsize=(8, 4))
        plt.subplot(1, 2, 1)
        plt.scatter(test_data_tsne[:,0],test_data_tsne[:,1], s=5, c=local_outlier_factor)
        plt.colorbar(label='')
        plt.title('predicted LOF for normal digit=0')

        plt.subplot(1, 2, 2)
        colors = ['indigo' if label == 0 else 'orange' for label in test_label]
        plt.scatter(test_data_tsne[:,0],test_data_tsne[:,1], s=5, c=colors)
        plt.scatter([], [], color='indigo', label='normal')
        plt.scatter([], [], color='orange', label='anomaly')
        plt.legend()
        plt.title('ground truth label LOF for normal digit=0')

        plt.tight_layout()
        plt.show()

    knn_roc_auc_score_1 += roc_auc_score(test_label, knn_distance[:,0])
    knn_roc_auc_score_5 += roc_auc_score(test_label, knn_distance[:,1])
    knn_roc_auc_score_10 += roc_auc_score(test_label, knn_distance[:,2])
    cluster_roc_auc_score_1 += roc_auc_score(test_label, k_nearest_cluster_distance[:,0])
    cluster_roc_auc_score_5 += roc_auc_score(test_label, k_nearest_cluster_distance[:,1])
    cluster_roc_auc_score_10 += roc_auc_score(test_label, k_nearest_cluster_distance[:,2])
    minkowski_distance_roc_auc_score_1 += roc_auc_score(test_label, k_nearest_minkowski_distance[0, :, :])
    minkowski_distance_roc_auc_score_2 += roc_auc_score(test_label, k_nearest_minkowski_distance[1, :, :])
    minkowski_distance_roc_auc_score_inf += roc_auc_score(test_label, k_nearest_minkowski_distance[2, :, :])
    cosine_distance_roc_auc_score += roc_auc_score(test_label, k_nearest_cos_distance)
    mahalanobis_distance_roc_auc_score += roc_auc_score(test_label, k_nearest_mahalanobis_distance)
    lof_roc_auc_score += roc_auc_score(test_label, local_outlier_factor)


print("########## AVERAGE ROC-AUC SOCRE ##########")
print("\nKNN-based AD : ")
print(f"    Average ROC-AUC socre of KNN-based AD with k=1  is {knn_roc_auc_score_1 / 10}")
print(f"    Average ROC-AUC socre of KNN-based AD with k=5  is {knn_roc_auc_score_5 / 10}")
print(f"    Average ROC-AUC socre of KNN-based AD with k=10 is {knn_roc_auc_score_10 / 10}")
print("\nCluster-based AD : ")
print(f"    Average ROC-AUC socre of Cluster-based AD with k=1  is {cluster_roc_auc_score_1 / 10}")
print(f"    Average ROC-AUC socre of Cluster-based AD with k=5  is {cluster_roc_auc_score_5 / 10}")
print(f"    Average ROC-AUC socre of Cluster-based AD with k=10 is {cluster_roc_auc_score_10 / 10}")
print("\nDistance-based AD : ")
print(f"    Average ROC-AUC socre of MinkowskiDistance-based AD with k=5 and r=1   is {minkowski_distance_roc_auc_score_1 / 10}")
print(f"    Average ROC-AUC socre of MinkowskiDistance-based AD with k=5 and r=2   is {minkowski_distance_roc_auc_score_2 / 10}")
print(f"    Average ROC-AUC socre of MinkowskiDistance-based AD with k=5 and r=inf is {minkowski_distance_roc_auc_score_inf / 10}")
print(f"    Average ROC-AUC socre of CosineDistance-based AD with k=5 is {cosine_distance_roc_auc_score / 10}")
print(f"    Average ROC-AUC socre of MahalanobisDistance-based AD with k=5 is {mahalanobis_distance_roc_auc_score / 10}")
print("\nDensity-based AD : ")
print(f"    Average ROC-AUC socre of LOF-based AD with k=5 is {lof_roc_auc_score / 10}")
Leave a Comment