# 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. 用于文本分类的多层注意力模型(Hierachical Attention Nerworks)

    论文来源:Hierarchical Attention Networks for Document Classification 1.概述 文本分类时NLP应用中最基本的任务,从之前的机器学习到现在基 ...

  2. 分享一个前后端分离的web项目(vue+spring boot)

    Github地址:https://github.com/smallsnail-wh 前端项目名为wh-web 后端项目名为wh-server 项目展示地址为我的github pages(https:/ ...

  3. Python:Day05 作业

    购物车: product_list = [['iphone6s',5800],['mac book',9800],['coffee',32],['book',80],['bike',1500]] sh ...

  4. RabbitMq 6种使用模式

    RabbitMQ的5种模式与实例 1.1 简单模式Hello World 功能:一个生产者P发送消息到队列Q,一个消费者C接收 生产者实现思路: 创建连接工厂ConnectionFactory,设置服 ...

  5. There is no action xxxFun defined for api controller api/subitem

    在使用abp的框架时,访问某个接口方法出现错误: There is no action xxxFun defined for api controller api/subitem 原因:肯定是访问的接 ...

  6. Vscode更新之后rg.exe占用cpu过高

    文件->首选项->搜索"search.followSymlinks"改为false https://www.zhihu.com/question/67317040

  7. 《React Native 精解与实战》书籍连载「iOS 平台与 React Native 混合开发」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  8. H3C交换机-SNMP配置

    1.1     SNMP基础配置 1.启动/关闭SNMP Agent服务 在系统视图模式下: 启用:snmp-agent 关闭:undo snmp-agent 注:缺省情况下snmp agent是关闭 ...

  9. VMware(威睿)后端开发笔试题总结

    1.   Linux中查看系统的发行版本信息 的命令? cat/etc/issue    和    lsb_release 2.   linux 挂载一个共享文件夹: mount  -t  cifc ...

  10. hibernate操纵数据库常用方法 及 hibernate对象的三种状态

    在dao层使用hibernate语言来与数据库进行访问,hibernate作为面向对象思想开发的dao层框架其理解也需要以面向对象的思想来看待 使用.hibernate不仅支持使用者使用他提供的对象来 ...