# Author: Baozi
#-*- codeing:utf-8 -*-
import _pickle as pickle
from sklearn import ensemble
import random
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, classification_report, \
confusion_matrix
import numpy as np ##########
########## # TRAINING_PICKLE = 'motog-old-65-withnoise-statistical.p' # 1a
TRAINING_PICKLE = 'trunc-dataset1a-noisefree-statistical.p' # 1a
# TESTING_PICKLE = 'motog-new-65-withnoise-statistical.p' # 2
TESTING_PICKLE = 'trunc-dataset2-noisefree-statistical.p' # print('Loading pickles...')
trainingflowlist = pickle.load(open(TRAINING_PICKLE, 'rb'), encoding='iso-8859-1')
testingflowlist = pickle.load(open(TESTING_PICKLE, 'rb'), encoding='iso-8859-1')
print('Done...')
print('') print('Training with ' + TRAINING_PICKLE + ': ' + str(len(trainingflowlist)))
print('Testing with ' + TESTING_PICKLE + ': ' + str(len(testingflowlist)))
print('') for THR in range(10): p = []
r = []
f = []
a = []
c = [] for i in range(5):
print(i)
########## PREPARE STUFF
trainingexamples = []
classifier = ensemble.RandomForestClassifier()
classifier2 = ensemble.RandomForestClassifier() ########## GET FLOWS
for package, time, flow in trainingflowlist:
trainingexamples.append((flow, package))
# print('') ########## SHUFFLE DATA to ensure classes are "evenly" distributed
random.shuffle(trainingexamples) ########## TRAINING PART 1
X1_train = []
y1_train = []
#####################################################
for flow, package in trainingexamples[:int(float(len(trainingexamples)) / 2)]:
X1_train.append(flow)
y1_train.append(package) # print('Fitting classifier...')
classifier.fit(X1_train, y1_train)
# print('Classifier fitted!')
# print('' ########## TRAINING PART 2 (REINFORCEMENT)
X2_train = []
y2_train = []
tmpx_train = []
tmpy_train = [] count = 0
count1 = 0
count2 = 0 ###############################################
for flow, package in trainingexamples[int(float(len(trainingexamples)) / 2):]:
# flow = np.array(flow).reshape(1,-1)
# tmp.append(flow)
tmpx_train.append(flow)
tmpy_train.append(package) predictions = classifier.predict(tmpx_train)
#print(type(predictions))#<class 'numpy.ndarray'>
#print(predictions[0])#com.myfitnesspal.android-auto.csv
for flow, package in trainingexamples[int(float(len(trainingexamples)) / 2):]:
X2_train.append(flow)
prediction = predictions[count] if (prediction == package):
y2_train.append(package)
count1 += 1
else:
y2_train.append('ambiguous')
count2 += 1
count += 1
print("Step Finished!!!!!!!!!!!")
# print(count1)
# print(count2) # print('Fitting 2nd classifier...')
classifier2.fit(X2_train, y2_train)
# print('2nd classifier fitted!'
# print('' ########## TESTING threshold = float(THR) / 10 X_test = []
y_test = []
tmpx_test = []
tmpy_test = []
count = 0
totalflows = 0
consideredflows = 0 for package, time, flow in testingflowlist:
tmpx_test.append(flow)
tmpy_test.append(package) predictionss = classifier2.predict(tmpx_test)#此时的分类器可以预测带有ambiguous标签的样本
prediction_proba = classifier2.predict_proba(tmpx_test)#此时的分类器可以预测带有ambiguous标签的样本
#print(type(prediction_proba))#<class 'numpy.ndarray'>
print(prediction_proba[0]) for package, time, flow in testingflowlist:
prediction = predictionss[count]
if (prediction != 'ambiguous'):
prediction_probability = max(prediction_proba[0])
totalflows += 1 if (prediction_probability >= threshold):
consideredflows += 1 X_test.append(flow)
y_test.append(package)
count += 1 y_pred = classifier2.predict(X_test) p.append(precision_score(y_test, y_pred, average="macro") * 100)
r.append(recall_score(y_test, y_pred, average="macro") * 100)
f.append(f1_score(y_test, y_pred, average="macro") * 100)
a.append(accuracy_score(y_test, y_pred) * 100)
c.append(float(consideredflows) * 100 / totalflows) print('Threshold: ' + str(threshold))
print(np.mean(p))
print(np.mean(r))
print(np.mean(f))
print(np.mean(a))
print(np.mean(c))
print('')

Appscanner实验还原code3的更多相关文章

  1. Appscanner实验还原code2

    import _pickle as pickle from sklearn import svm, ensemble import random from sklearn.metrics import ...

  2. Appscanner实验还原code1

    import _pickle as pickle from sklearn import svm, ensemble import random from sklearn.metrics import ...

  3. 11.2.0.4rac service_name参数修改

    环境介绍 )客户环境11. 两节点 rac,集群重启后,集群资源一切正常,应用cs架构,连接数据库报错,提示连接对象不存在 )分析报错原因,连接数据库方式:ip:Port/service_name方式 ...

  4. RAC环境修改参数生效测试

    本篇文档--目的:实验测试在RAC环境下,修改数据库参数与单实例相比,需要注意的地方 --举例说明,在实际生产环境下,以下参数很可能会需要修改 --在安装数据库完成后,很可能没有标准化,初始化文档,没 ...

  5. vsftp -samba-autofs

    摘要: 1.FTP文件传输协议,PAM可插拔认证模块,TFTP简单文件传输协议. 注意:iptables防火墙管理工具默认禁止了FTP传输协议的端口号 2.vsftpd服务程序三种认证模式?三种认证模 ...

  6. 【故障处理】ORA-12162 错误的处理

    [故障处理]ORA-12162: TNS:net service name is incorrectly specified 一.1  场景 今天拿到一个新的环境,可是执行sqlplus / as s ...

  7. SDUT OJ 数据结构实验之二叉树四:(先序中序)还原二叉树

    数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...

  8. SDUT 3343 数据结构实验之二叉树四:还原二叉树

    数据结构实验之二叉树四:还原二叉树 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定一棵 ...

  9. SDUT-3343_数据结构实验之二叉树四:(先序中序)还原二叉树

    数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一棵二叉树的先序遍历 ...

随机推荐

  1. nginx配置tomcat负载均衡,nginx.conf配置文件的配置

  2. UVA11400-Lighting System Design(动态规划基础)

    Problem UVA11400-Lighting System Design Accept: 654  Submit: 4654Time Limit: 3000 mSec Problem Descr ...

  3. No.0

    算法类 1.快速排序算法 2.树的非递归后序排序算法 3.希尔排序 4.冒泡排序 5.链表和链表转向 6.其他   设计模式 1.单例模式 2.工厂模式 3.抽象工厂模式 4.面向对象设计,ooa,o ...

  4. P1515 旅行(简单搜索)

    非常简单的搜索. 思路:先排序,然后,搜索枚举的时候满足A < 两个旅店 < B,然后,搜索就行了. #include<iostream> #include<algori ...

  5. mysql执行顺序

    SELECT语句执行顺序 SELECT语句中子句的执行顺序与SELECT语句中子句的输入顺序是不一样的,所以并不是从SELECT子句开始执行的,而是按照下面的顺序执行: 开始->FROM子句-& ...

  6. PHPsql

    下面员工3的薪水大于其主管的薪水,一条SQL找到薪水比下属低的主管 id username salary pid 1 a 3000 null 2 b 8000 null 3 c 5000 1 4 d ...

  7. 【js】使用javascript 实现静态网页分页效果

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-Type" content ...

  8. 无预挖无ICO-潜力币XDAG最强攻略

    更新下XDAG上交易所了,当时看好这个币才推荐的,那时场外才2毛不到,现在涨到9毛一个了... https://www.coinbat.com/trade/panel/xdag... VBitEX [ ...

  9. Groovy语言学习--语法基础(4)

    语法基础到此就结束了,毕竟其和Java许多地方并无差别.groovy作为脚本,是为了能更好地随业务变化调整规则,我根据自己对规则的理解 通过一张图简单描述一个groovy脚本场景:由于货币膨胀,经济收 ...

  10. 显式与隐式(ExplicitAndImplicit)

    显式与隐式(Explicit And Implicit) 1.概念 1.1 显式 实现的单词Explicit意思是清楚的.明确的.详述的.所以,显式的“显”是指明显且清楚的实现,相对于接口来说,就是明 ...