# 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. Python字符串 u"string",r"string"的写法含义

    1.字符串前加 u = unicode编码 例:u"我是含有中文字符组成的字符串." 作用:后面字符串以 Unicode 格式 进行编码,一般用在中文字符串前面,防止因为源码储存格 ...

  2. 微信硬件平台(八) 1 esp8266从自己的服务器获取token

    从txt获取token #include <ESP8266WiFi.h> #define host_token "www.dongvdong.top" #define ...

  3. 仿照wtform自定义Form组件

    仿照wtforms自定义Form组件 1.wtforms 点击查看源码分析及使用方法 2.自定义Form组件 #!usr/bin/env python # -*- coding:utf-8 -*- f ...

  4. AI SSD目标检测算法

    Single Shot multibox Detector,简称SSD,是一种目标检测算法. Single Shot意味着SSD属于one stage方法,multibox表示多框预测. CNN 多尺 ...

  5. Python学习总结 13 Scrapy

    当前环境是 Win8 64位的,使用的Python 3.5 版本. 一 安装Scrapy 1,安装 lxml pip install lxml -i https://pypi.douban.com/s ...

  6. kafka模型理解

    1.消息发送至一个topic,而这个topic可以由多个partition组成,每条消息在partition中的位置称为offset 2.消息存在有效期,如果设置为2天,则消息2天后会被删除 3.每个 ...

  7. 两个select之间的元素互相移动并保持顺序

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  8. eclipse不能运行Struts2项目

    刚接触Struts2项目,本想写个HelloWorld上手,谁知道光eclipse配置tomcat就鼓捣一晚上,查阅各种资料. 项目刚开始报错: "java.lang.ClassNotFou ...

  9. HDU - 1698 线段树区间修改,区间查询

    这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上laze标记,然后维护即可. 我自己做的时候太傻逼了...把区间修改写错了,对给定区间进行修改的时候,mid取的是节点的左右 ...

  10. MyEclipse和eclipse的区别

    对于新手来说,MyEclipse和eclipse来说的区别可能就是MyEclipse比eclipse多了my,MyEclipse主要为JavaEE开发,而Eclipse主要为Java开发..那么MyE ...