def test_hmad(phi, kern, train, test, num_train, anom_prob, labels, zero_shot=False): auc = 0.5 # train structured anomaly detection sad = LatentOCSVM(train, C=1.0 / (num_train * anom_prob)) (lsol, lats, thres) = sad.train_dc(max_iter=60, zero_shot=zero_shot) (pred_vals, pred_lats) = sad.apply(test) (fpr, tpr, thres) = metric.roc_curve(labels[num_train:], pred_vals) auc = metric.auc(fpr, tpr) return auc
def experiment_anomaly_detection(train, test, comb, num_train, anom_prob, labels): # train one-class svm phi = calc_feature_vecs(comb.X) kern = get_kernel(phi[:,0:num_train], phi[:,0:num_train]) ocsvm = OcSvmDualQP(kern, anom_prob) ocsvm.fit() kern = get_kernel(phi, phi) oc_as = ocsvm.apply(kern[num_train:,ocsvm.get_support_dual()]) fpr, tpr, thres = metric.roc_curve(labels[num_train:], oc_as) base_auc = metric.auc(fpr, tpr) # train structured anomaly detection sad = LatentOCSVM(train, anom_prob) sad.fit(max_iter=40) pred_vals, pred_lats = sad.apply(test) fpr, tpr, thres = metric.roc_curve(labels[num_train:], pred_vals) auc = metric.auc(fpr, tpr) return auc, base_auc
for i in range(NUM_CLASSES): mean = (np.random.rand(2) - 0.5) * 12. Dtrain[i * NUM_DATA:(i + 1) * NUM_DATA, :2] = np.random.multivariate_normal( mean, 1. * np.random.rand() * np.eye(2), size=NUM_DATA) Dy[i * NUM_DATA:(i + 1) * NUM_DATA] = i # generate structured object sobj = SOMultiClass(Dtrain.T, y=Dy, classes=NUM_CLASSES) # unsupervised methods lsvdd = LatentSVDD(sobj, 0.9) lsvdd.fit() spca = LatentPCA(sobj) spca.fit() socsvm = LatentOCSVM(sobj, .2) socsvm.fit() # supervised methods ssvm = SSVM(sobj) ssvm.train() # generate test data grid delta = 0.2 x = np.arange(-8.0, 8.0, delta) y = np.arange(-8.0, 8.0, delta) X, Y = np.meshgrid(x, y) (sx, sy) = X.shape Xf = np.reshape(X, (1, sx * sy)) Yf = np.reshape(Y, (1, sx * sy)) Dtest = np.append(Xf, Yf, axis=0)
Dy = np.zeros(NUM_CLASSES*NUM_DATA, dtype=np.int) Dtrain = np.ones((NUM_CLASSES*NUM_DATA, 3)) for i in range(NUM_CLASSES): mean = (np.random.rand(2)-0.5)*12. Dtrain[i*NUM_DATA:(i+1)*NUM_DATA, :2] = np.random.multivariate_normal(mean, 1.*np.random.rand()*np.eye(2), size=NUM_DATA) Dy[i*NUM_DATA:(i+1)*NUM_DATA] = i # generate structured object sobj = SOMultiClass(Dtrain.T, y=Dy , classes=NUM_CLASSES) # unsupervised methods lsvdd = LatentSVDD(sobj, 0.9) lsvdd.fit() spca = LatentPCA(sobj) spca.fit() socsvm = LatentOCSVM(sobj, .2) socsvm.fit() # supervised methods ssvm = SSVM(sobj) ssvm.train() # generate test data grid delta = 0.2 x = np.arange(-8.0, 8.0, delta) y = np.arange(-8.0, 8.0, delta) X, Y = np.meshgrid(x, y) (sx,sy) = X.shape Xf = np.reshape(X,(1,sx*sy)) Yf = np.reshape(Y,(1,sx*sy)) Dtest = np.append(Xf, Yf, axis=0)