コード例 #1
0
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)
コード例 #2
0
def test_ocsvm(phi, kern, train, test, num_train, anom_prob, labels):
    auc = 0.5
    ocsvm = OCSVM(kern[:num_train, :num_train], C=1.0 / (num_train * anom_prob))
    msg = ocsvm.train_dual()
    if not msg == OCSVM.MSG_ERROR:
        (oc_as, foo) = ocsvm.apply_dual(kern[num_train:, ocsvm.get_support_dual()])
        (fpr, tpr, thres) = metric.roc_curve(labels[num_train:], oc_as)
        auc = metric.auc(fpr, tpr)
    return auc
コード例 #3
0
def perf_ocsvm(phi, marker, train, test, anom_prob, ord=1):
    #phi = phi[phi_inds.tolist(),:]
    print('(a) normalize features...')
    phi = normalize_features(phi, ord=ord)
    print('(b) Build kernel...')
    kern = Kernel.get_kernel(phi, phi)
    print('(c) Train OCSVM...')
    ocsvm = OCSVM(kern[train, train],
                  C=1.0 / (float(len(train)) * (1.0 - anom_prob)))
    ocsvm.train_dual()
    print('(d) Apply OCSVM...')
    (oc_as, foo) = ocsvm.apply_dual(kern[test,
                                         train[ocsvm.get_support_dual()]])
    (fpr, tpr, thres) = metric.roc_curve(co.matrix(marker)[test], oc_as)
    auc = metric.auc(fpr, tpr)
    print('(e) Return AUC={0}...'.format(auc))
    return auc
コード例 #4
0
ファイル: test_hmsvm.py プロジェクト: 10sun/tilitools
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)
コード例 #5
0
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)
コード例 #6
0
ファイル: test_ocsvm.py プロジェクト: 10sun/tilitools
	svm = OCSVM(kernel,0.1)
	svm.train_dual()

	delta = 0.07
	x = np.arange(-4.0, 4.0, delta)
	y = np.arange(-4.0, 4.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)
	print(Dtest.shape)
	print('halloooo')
	foo = 3 * delta

	# build test kernel
	kernel = Kernel.get_kernel(co.matrix(Dtest),Dtrain[:,svm.get_support_dual()],ktype,kparam)

	(res,state) = svm.apply_dual(kernel)
	print(res.size)

	Z = np.reshape(res,(sx,sy))
	plt.contourf(X, Y, Z)
	plt.contour(X, Y, Z, [np.array(svm.get_threshold())[0,0]])
	plt.scatter(Dtrain[0,svm.get_support_dual()],Dtrain[1,svm.get_support_dual()],40,c='k')
	plt.scatter(Dtrain[0,:],Dtrain[1,:],10)
	plt.show()

	print('finished')
コード例 #7
0
ファイル: 14_nips_pgm.py プロジェクト: ibalazevic/tilitools
	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')

	 	if i==0:
		 	plt.plot(range(LENS),train.X[i][0,:].trans() + i*8,'-k')


	(fpr, tpr, thres) = metric.roc_curve(label, co.matrix(scores))
	auc = metric.auc(fpr, tpr)
	print auc

	# train one-class svm
	kern = Kernel.get_kernel(phi, phi)
	ocsvm = OCSVM(kern, C=1.0/(EXMS_TRAIN*0.1))
	ocsvm.train_dual()
	(oc_as, foo) = ocsvm.apply_dual(kern[:,ocsvm.get_support_dual()])
	(fpr, tpr, thres) = metric.roc_curve(label[0:EXMS_TRAIN], oc_as)
	base_auc = metric.auc(fpr, tpr)
	print base_auc

	plt.show()

	print('finished')
コード例 #8
0
    x = np.arange(-4.0, 4.0, delta)
    y = np.arange(-4.0, 4.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)
    print(Dtest.shape)
    print('halloooo')
    foo = 3 * delta

    # build test kernel
    kernel = Kernel.get_kernel(co.matrix(Dtest),
                               Dtrain[:,
                                      svm.get_support_dual()], ktype, kparam)

    (res, state) = svm.apply_dual(kernel)
    print(res.size)

    Z = np.reshape(res, (sx, sy))
    plt.contourf(X, Y, Z)
    plt.contour(X, Y, Z, [np.array(svm.get_threshold())[0, 0]])
    plt.scatter(Dtrain[0, svm.get_support_dual()],
                Dtrain[1, svm.get_support_dual()],
                40,
                c='k')
    plt.scatter(Dtrain[0, :], Dtrain[1, :], 10)
    plt.show()

    print('finished')