def do_POST(self):
	hash = open("/home/capk/FYP/server/myserver/Data/hashes.txt","w")
        permfile = open(filepermission,"w")
	content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
        post_data = self.rfile.read(content_length) # <--- Gets the data itself

	post_data = post_data.replace('=','\n',1)
        post_data = post_data.replace('%0A','\n')
        post_data = post_data.replace('%2F','/')
        post_data = post_data.replace('%3D','=')
	post_data = post_data.replace('%2B','+')
        post_data = post_data.replace('&','\n')
        logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
                str(self.path), str(self.headers), post_data.decode('utf-8'))
	if "permissions" in post_data:
		hashes,permissions = post_data.split("permissions")
		hashes = hashes.replace("hashes=",'')
		hash.write(hashes)
		packageName = hashes.split("\n")
		permfile.write(packageName[0])
		permfile.write("\n")
		permissions = permissions.replace("=",'')
		permissions = permissions.replace("%2C",',')
		permfile.write(permissions)
		permfile.close()
		generate()
		svm()
	else:
		hash.write(post_data)
	hash.close()
	db_request()
        self._set_response()
        self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))
    def __init__(self, kernel = 'poly', C = 1, degree = 1, sigma = 1, threshold = 1e-5):

        self. sv0 = svm(kernel,C=C,degree=degree, sigma=sigma, threshold = threshold)
        self. sv1 = svm(kernel,C=C,degree=degree, sigma=sigma, threshold = threshold)
        self. sv2 = svm(kernel,C=C,degree=degree, sigma=sigma, threshold = threshold)
        self. sv3 = svm(kernel,C=C,degree=degree, sigma=sigma, threshold = threshold)
        self. sv4 = svm(kernel,C=C,degree=degree, sigma=sigma, threshold = threshold)
        self. sv5 = svm(kernel,C=C,degree=degree, sigma=sigma, threshold = threshold)
        self. sv6 = svm(kernel,C=C,degree=degree, sigma=sigma, threshold = threshold)
        self. sv7 = svm(kernel,C=C,degree=degree, sigma=sigma, threshold = threshold)
        self. sv8 = svm(kernel,C=C,degree=degree, sigma=sigma, threshold = threshold)
        self. sv9 = svm(kernel,C=C,degree=degree, sigma=sigma, threshold = threshold)
Example #3
0
def cross_validation_svm(files, epochs, c, bias=1, r=0.01):
    accuracy = []

    for file in files:
        train_file = files[file][0]
        test_file = files[file][1]

        training_set = train_file['d']
        training_set = limit_features(training_set, [36, 24, 22, 42, 402, 52, 32, 29, 20, 51])
        training_lbl = train_file['l']
        test_set = test_file['d']
        test_set = limit_features(test_set, [36, 24, 22, 42, 402, 52, 32, 29, 20, 51])
        test_lbl = test_file['l']

        weigth_svm = svm(training_set, training_lbl, epochs, c, bias, r)
        acc = test_svm(test_set, test_lbl, weigth_svm)
        accuracy.append(acc["correct"] / (acc["correct"] + acc["wrong"]))

    return sum(accuracy) / len(accuracy)
def run_svms(example_sets, num_sets, largest_index):

	# each 'i' is test
	for i in range(len(example_sets)):
		# each 'j' is training
		training = []
		for j in range(len(example_sets)):
			if i == j:
				continue
			training.extend(example_sets[j])

		initial_weights = np.array([0.00001 for k in range(largest_index)] + [1])

		learn_rates = [10, 1, .1, .01, .001, .0001]
		tradeoffs = [10, 1, .1, .01, .001, .0001]

		# train with 4/5
		# this weight_vals_list has a position for each learning rate
		print('fold:\t{}'.format(str(i)))
		for lr in learn_rates:
			for to in tradeoffs:

				w = svm(training, initial_weights, to, lr, 40)
				test_svm(example_sets[i], w)
def modified_XOR(kernel, degree, C, sdev):
    import SVM
    from SVM import svm
    sv = svm(kernel, degree=degree, C=C)

    m = 100
    X = sdev * np.random.randn(
        m, 2
    )  # na ovaj nacin dobijamo standardnu gausovu raspodelu, sa mean-om nula i standarndnom devijacijom 1 stim sto
    # mozemo da skaliramo standardnu devijaciju pomocu promenljive sdev
    X[m // 2:,
      0] += 1.  # // ovaj operator zaokruzuje na nizu vrednost ..primer 15/4=3. I ovde smo elemente koji se nalaze u drugoj polivini nultog reda
    # uvecali za jedan
    X[m // 4:m // 2, 1] += 1.
    X[3 * m // 4:, 1] += 1.
    # Prethodna modifikacija za X dovela je do toga da imamo parove (0,0) od 0 do 25, (0,1) od 25 do 50 (1,0) od 50 do 75 i (1,1) od 75 do 100
    targets = -np.ones((m, 1))
    targets[:m // 4, 0] = 1.  #od nultog do 25
    targets[3 * m // 4:, 0] = 1.  #od 75 do kraja
    #prethodna modifikacija za targete, nam govori kojoj klasi treba da pripadaju koji parovi

    sv.train_svm(X, targets)  # treniranje mreze

    Y = sdev * np.random.randn(m, 2)
    Y[m // 2:, 0] += 1.
    Y[m // 4:m // 2, 1] += 1.
    Y[3 * m // 4:m, 1] += 1.
    test = -np.ones((m, 1))
    test[:m // 4, 0] = 1.
    test[3 * m // 4:, 0] = 1.

    output = sv.classifier(Y, soft=False)  # klasifikovanje

    err1 = np.where((output == 1.) & (test == -1.))[0]
    err2 = np.where((output == -1.) & (test == 1.))[0]

    print(kernel, C)
    print("Class 1 errors ", len(err1), " from ", len(test[test == 1]))
    print("Class 2 errors ", len(err2), " from ", len(test[test == -1]))
    print(
        "Test accuracy ", 1. - (float(len(err1) + len(err2))) /
        (len(test[test == 1]) + len(test[test == -1])))

    pl.ion()
    pl.figure()
    l1 = np.where(targets == 1)[0]
    l2 = np.where(targets == -1)[0]
    pl.plot(X[sv.sv, 0], X[sv.sv, 1], 'o', markeredgewidth=5)
    pl.plot(X[l1, 0], X[l1, 1], 'ko')
    pl.plot(X[l2, 0], X[l2, 1], 'wo')
    l1 = np.where(test == 1)[0]
    l2 = np.where(test == -1)[0]
    pl.plot(Y[l1, 0], Y[l1, 1], 'ks')
    pl.plot(Y[l2, 0], Y[l2, 1], 'ws')

    step = 0.1
    f0, f1 = np.meshgrid(
        np.arange(np.min(X[:, 0]) - 0.5,
                  np.max(X[:, 0]) + 0.5, step),
        np.arange(np.min(X[:, 1]) - 0.5,
                  np.max(X[:, 1]) + 0.5, step))

    out = sv.classifier(np.c_[np.ravel(f0), np.ravel(f1)], soft=True).T

    out = out.reshape(f0.shape)
    pl.contour(f0, f1, out, 2)

    pl.axis('off')
    pl.show()
Example #6
0
        item = item.split(" ")
        if (len(item) == 5):
            item = item[1:5]
        for x in item:
            final_feature.append(x)

    else:
        final_feature.append(item)
finl2_features = []
for item in final_feature:
    if item is not (''):
        finl2_features.append(item)

lables = [float(i) for i in lables]
features = [float(i[:6]) for i in finl2_features]

feature_np = np.asarray(features, dtype=np.float32)
label_np = np.asarray(lables, dtype=np.float32)
feature_np = feature_np.reshape(-1, 33)

NeuralNet = neuralnet(3)
model, X_test, y_test = fire_neuralnetwork(feature_np, label_np, NeuralNet)

svm_trainer = svm()

accsvm = svm_trainer.train_predict_svm(feature_np, label_np)

accN = NeuralNet.test(model, X_test, y_test)

print("SVM result: ", accsvm, "neural net: ", accN)
Example #7
0
                                            results_perceptron["wrong"])
print("AHU 38: " + str(accuracy))
results_perceptron = test_perceptron(test3_data, test3_y, weight_perceptron)
accuracy = results_perceptron["correct"] / (results_perceptron["correct"] +
                                            results_perceptron["wrong"])
print("AHU 19B: " + str(accuracy))

# +++++++++++++++++++++++++++++++++++
# SVM
# +++++++++++++++++++++++++++++++++++
print("")
print("SVM")
c = 32
epoch = 10

weigth_svm = svm(train_data, train_y, epoch, 1)
print("WEIGHT: " + str(weigth_svm))
results_svm = test_svm(test1_data, test1_y, weight_perceptron)
accuracy_svm = results_svm["correct"] / (results_svm["correct"] +
                                         results_svm["wrong"])
print("AHU 13: " + str(accuracy))
results_svm = test_svm(test2_data, test2_y, weight_perceptron)
accuracy_svm = results_svm["correct"] / (results_svm["correct"] +
                                         results_svm["wrong"])
print("AHU 38: " + str(accuracy))
results_svm = test_svm(test3_data, test3_y, weight_perceptron)
accuracy_svm = results_svm["correct"] / (results_svm["correct"] +
                                         results_svm["wrong"])
print("AHU 19B: " + str(accuracy))

# +++++++++++++++++++++++++++++++++++
Example #8
0
    if (_b == 0):
        print(_fpcap + '无可解析的数据包')
    else:
        print(_fpcap + "解析成功")

if (_b == 0):
    print('路径中无文件或文件中无可分析的数据包')
else:
    c = fx(IP, file_path, mydb, _a, _b)  # 分析数据包
    print(file_path + "分析成功")

    insert_dos(file_path, mydb, _a, _b)  # 可视化所用表
    print(file_path + "流量时序表生成成功")

    insert_port(IP, file_path, mydb, _a, _b)  # 可视化所用表
    print(file_path + "端口遍历表生成成功")

    insert_login(IP, file_path, mydb, _a, _b)  # 可视化所用表
    print(file_path + "远程登录表生成成功")

    d, e = insert_time(IP, file_path, mydb, _a, _b)  # 可视化所用表
    if e != 0:
        print(file_path + "非法时间操作记录表生成成功")
    else:
        print(file_path + "无非法时间操作")

    scores(mydb, file_path, c, d, e)  # 可视化所用表
    print(file_path + "更新分析表评分项成功")

    svm(file_path, mydb, c)  # 这个会导入实现训练好的模型,预测结果,这里导入样本集只是帮助分析结果做归一化
Example #9
0
def my_form_post():
    text1 = int(request.form['user_school'])
    text2 = int(request.form['user_sex'])
    text3 = int(request.form['user_age'])
    text4 = int(request.form['user_addresstype'])
    text5 = int(request.form['user_familysize'])
    text6 = int(request.form['user_cohabitation'])
    text7 = int(request.form['user_mothereducation'])
    text8 = int(request.form['user_fathereducation'])
    text9 = int(request.form['user_motherjob'])
    text10 = int(request.form['user_fatherjob'])
    text11 = int(request.form['user_schoolchoose'])
    text12 = int(request.form['user_guardian'])
    text13 = int(request.form['user_traveltime'])
    text14 = int(request.form['user_studytime'])
    text15 = int(request.form['user_failures'])
    text16 = int(request.form['user_schoolsupport'])
    text17 = int(request.form['user_familysupport'])
    text18 = int(request.form['user_paidclasses'])
    text19 = int(request.form['user_extraactivities'])
    text20 = int(request.form['user_attendednursery'])
    text21 = int(request.form['user_highereducation'])
    text22 = int(request.form['user_internetaccess'])
    text23 = int(request.form['user_romantic'])
    text24 = int(request.form['user_familyrelation'])
    text25 = int(request.form['user_freetime'])
    text26 = int(request.form['user_goingout'])
    text27 = int(request.form['user_workalcohol'])
    text28 = int(request.form['user_weekalcohol'])
    text29 = int(request.form['user_health'])
    text30 = int(request.form['user_attendence'])
    text31 = int(request.form['user_s1'])
    text32 = int(request.form['user_s2'])

    if text31 >= 10:
        g1 = 1
    else:
        g1 = 0

    if text32 >= 10:
        g2 = 1
    else:
        g2 = 0

    x = [[text1, text2, text3, text4, text5, text6, text7, text8, text9, text10, text11, text12, text13, text14, text15, text16,
          text17, text18, text19, text20, text21, text22, text23, text24, text25, text26, text27, text28, text29, text30, g1, g2]]
    a = rf(x)
    b = gnb(x)
    c = svm(x)
    if a == b:
        output = a
    elif a == c:
        output = a
    elif b == c:
        output = b
    
    if output=='bad':
        info="This is just an assessment based on the data you have given. This prediction shows that there is higher probabilty for you to fail in this subject. You should work hard for getting better results"
    else:
        info="This is just an assessment based on the data you have given. This prediction shows that there is higher probabilty for you getting good result in this subject. You should work hard like this, else this prediction can be wrong"

    return render_template('results.html',output=output,info=info)
Example #10
0
import numpy as np
from sklearn import datasets
from SVM import svm

# create testing data
X, y = datasets.make_blobs(n_samples=100,
                           n_features=2,
                           centers=2,
                           cluster_std=1.05,
                           random_state=42)

y = np.where(y == 0, -1, 1)

# Fit data to model
Model = svm()
Model.fit(X, y)

# predict the cluster and compare with actual
print(f"actual:{y[7]}, predicted: {Model.predict(X[7])}")
	do_naive_bayes = 0
	do_compile_trees = 0
	do_bagged_trees = 0
	do_svm_over_trees = 1

	# // largest index
	lfi = max(get_largest_index(training_whole), get_largest_index(test))

	training_examples = [parse(base_cvsplits.format(i), lfi) for i in range(5)]
	examples = parse(training_whole, lfi + 1)
	test_examples = parse(test, lfi + 1)
	initial_weights = np.array([0.00001 for k in range(lfi + 1)] + [1])

	if do_svm:
		run_svms(training_examples, len(training_examples), lfi)
		w = svm(examples, initial_weights, 10, 0.0001, 40)
		test_svm(test_examples, w)

	if do_log_regr:

		# run cv splits
		print("log regr cv splits")
		run_logregressions(training_examples, len(training_examples), lfi)

		# all training examples
		w = log_regression(examples, initial_weights, 10000, 0.0001, 40)

		# test on training
		acc = test_log_regression(examples, w)
		print("log regr train whole acc:\t{}".format(acc))