def experiment_anomaly_detection(train, test, comb, num_train, anom_prob, labels): phi = calc_feature_vecs(comb.X) print phi.size # bayes classifier (DIMS, N) = phi.size w_bayes = co.matrix(1.0, (DIMS, 1)) pred = w_bayes.trans()*phi[:,num_train:] (fpr, tpr, thres) = metric.roc_curve(labels[num_train:], pred.trans()) bayes_auc = metric.auc(fpr, tpr) # train one-class svm kern = Kernel.get_kernel(phi[:,0:num_train], phi[:,0:num_train]) ocsvm = OCSVM(kern, C=1.0/(num_train*anom_prob)) ocsvm.train_dual() kern = Kernel.get_kernel(phi, phi) (oc_as, foo) = ocsvm.apply_dual(kern[num_train:,ocsvm.get_support_dual()]) (fpr, tpr, thres) = metric.roc_curve(labels[num_train:], oc_as) base_auc = metric.auc(fpr, tpr) if (base_auc<0.5): base_auc = 1.0-base_auc # train structured anomaly detection #sad = StructuredOCSVM(train, C=1.0/(num_train*anom_prob)) sad = StructuredOCSVM(train, C=1.0/(num_train*0.5)) (lsol, lats, thres) = sad.train_dc(max_iter=50) (pred_vals, pred_lats) = sad.apply(test) (fpr, tpr, thres) = metric.roc_curve(labels[num_train:], pred_vals) auc = metric.auc(fpr, tpr) if (auc<0.5): auc = 1.0-auc return (auc, base_auc, bayes_auc)
def experiment_anomaly_segmentation(train, test, comb, num_train, anom_prob, labels): # transductive train/pred for structured anomaly detection sad = StructuredOCSVM(comb, C=1.0/(num_train*0.5)) (lsol, lats, thres) = sad.train_dc(max_iter=40) (cont, cont_exm) = test.evaluate(lats[num_train:]) # train structured svm ssvm = SSVM(train) (sol, slacks) = ssvm.train() (vals, preds) = ssvm.apply(test) (base_cont, base_cont_exm) = test.evaluate(preds) return (cont, base_cont)
def experiment_anomaly_segmentation(train, test, comb, num_train, anom_prob, labels): # transductive train/pred for structured anomaly detection sad = StructuredOCSVM(comb, C=1.0 / (num_train * 0.5)) (lsol, lats, thres) = sad.train_dc(max_iter=40) (cont, cont_exm) = test.evaluate(lats[num_train:]) # train structured svm ssvm = SSVM(train) (sol, slacks) = ssvm.train() (vals, preds) = ssvm.apply(test) (base_cont, base_cont_exm) = test.evaluate(preds) return (cont, base_cont)
def experiment_anomaly_detection(train, test, comb, num_train, anom_prob, labels): # train one-class svm phi = calc_feature_vecs(comb.X) kern = Kernel.get_kernel(phi[:,0:num_train], phi[:,0:num_train]) ocsvm = OCSVM(kern, C=1.0/(num_train*anom_prob)) ocsvm.train_dual() kern = Kernel.get_kernel(phi, phi) (oc_as, foo) = ocsvm.apply_dual(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 = StructuredOCSVM(train, C=1.0/(num_train*anom_prob)) (lsol, lats, thres) = sad.train_dc(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)
def experiment_anomaly_detection(train, test, comb, num_train, anom_prob, labels): # train one-class svm phi = calc_feature_vecs(comb.X) kern = Kernel.get_kernel(phi[:, 0:num_train], phi[:, 0:num_train]) ocsvm = OCSVM(kern, C=1.0 / (num_train * anom_prob)) ocsvm.train_dual() kern = Kernel.get_kernel(phi, phi) (oc_as, foo) = ocsvm.apply_dual(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 = StructuredOCSVM(train, C=1.0 / (num_train * anom_prob)) (lsol, lats, thres) = sad.train_dc(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(len(X)): lens = len(X[i][0,:]) cnt += lens tst_mean += co.matrix(1.0, (1, lens))*X[i].trans() print tst_mean/float(cnt) return X if __name__ == '__main__': EXMS_TRAIN = 80 EXMS_TEST = 100 EXMS_COMB = EXMS_TRAIN+EXMS_TEST (train, test, comb, label, phi) = get_model(EXMS_COMB, EXMS_TRAIN) lsvm = StructuredOCSVM(train, C=1.0/(train.samples*0.5)) (lsol, lats, thres) = lsvm.train_dc(max_iter=100) (err, err_exm) = train.evaluate(lats) print err plt.figure() scores = [] for i in range(train.samples): LENS = len(train.y[i]) (anom_score, scores_exm) = train.get_scores(lsol, i, lats[i]) scores.append(anom_score) plt.plot(range(LENS),scores_exm.trans() + i*8,'-g') plt.plot(range(LENS),train.y[i].trans() + i*8,'-b') plt.plot(range(LENS),lats[i].trans() + i*8,'-r')
Dtrain1 = ToyData.get_gaussian(100,dims=2,means=[1.0,1.0],vars=[1.0,0.3]) Dtrain2 = ToyData.get_gaussian(10,dims=2,means=[-2.0,1.0],vars=[0.3,1.3]) Dtrain3 = ToyData.get_gaussian(10,dims=2,means=[3.0,-3.0],vars=[0.3,0.3]) Dtrain4 = ToyData.get_gaussian(5,dims=2,means=[6.0,-3.0],vars=[0.2,0.1]) Dtrain = co.matrix([[Dtrain1], [Dtrain2], [Dtrain3], [Dtrain4]]) Dtrain = co.matrix([[Dtrain.trans()],[co.matrix(1.0,(125,1))]]).trans() Dy = co.matrix([co.matrix([0]*100), co.matrix([1]*10), co.matrix([2]*10), co.matrix([3]*5)]) # generate structured object sobj = SOMultiClass(Dtrain, y=Dy , classes=NUM_CLASSES) # unsupervised methods lsvdd = LatentSVDD(sobj,1.0/(125.0*1.0)) spca = StructuredPCA(sobj) socsvm = StructuredOCSVM(sobj,1.0/(125.0*1.0)) # supervised methods ssvm = SSVM(sobj) # 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) Dtest = np.append(Dtest,np.reshape([1.0]*(sx*sy),(1,sx*sy)),axis=0) print(Dtest.shape)
Dtrain = co.matrix([[Dtrain1], [Dtrain2], [Dtrain3], [Dtrain4]]) Dtrain = co.matrix([[Dtrain.trans()], [co.matrix(1.0, (125, 1))]]).trans() Dy = co.matrix([ co.matrix([0] * 100), co.matrix([1] * 10), co.matrix([2] * 10), co.matrix([3] * 5) ]) # generate structured object sobj = SOMultiClass(Dtrain, y=Dy, classes=NUM_CLASSES) # unsupervised methods lsvdd = LatentSVDD(sobj, 1.0 / (125.0 * 1.0)) spca = StructuredPCA(sobj) socsvm = StructuredOCSVM(sobj, 1.0 / (125.0 * 1.0)) # supervised methods ssvm = SSVM(sobj) # 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) Dtest = np.append(Dtest, np.reshape([1.0] * (sx * sy), (1, sx * sy)), axis=0)