# -*- coding:utf-8 -*-
import re
import matplotlib.pyplot as plt
import os
from sklearn.feature_extraction.text import CountVectorizer
from sklearn import cross_validation
import os
from sklearn.datasets import load_iris
from sklearn import tree
import pydotplus
def load_one_flle(filename):
x=[]
with open(filename) as f:
line=f.readline()
line=line.strip('\n')
return line
def load_adfa_training_files(rootdir):
x=[]
y=[]
list = os.listdir(rootdir)
for i in range(0, len(list)):
path = os.path.join(rootdir, list[i])
if os.path.isfile(path):
x.append(load_one_flle(path))
y.append(0)
return x,y
def dirlist(path, allfile):
filelist = os.listdir(path)
for filename in filelist:
filepath = os.path.join(path, filename)
if os.path.isdir(filepath):
dirlist(filepath, allfile)
else:
allfile.append(filepath)
return allfile
def load_adfa_hydra_ftp_files(rootdir):
x=[]
y=[]
allfile=dirlist(rootdir,[])
for file in allfile:
if re.match(r"/home/qin/code/python/web-ml/1book-master/data/ADFA-LD/Attack_Data_Master/Hydra_FTP_\d+/UAD-Hydra-FTP*",file):
x.append(load_one_flle(file))
y.append(1)
return x,y
if __name__ == '__main__':
x1,y1=load_adfa_training_files("/home/qin/code/python/web-ml/1book-master/data/ADFA-LD/Training_Data_Master/")
x2,y2=load_adfa_hydra_ftp_files("/home/qin/code/python/web-ml/1book-master/data/ADFA-LD/Attack_Data_Master/")
#以上分别是提取 普通数据(y=0) 和 攻击文件数据 y=1
x=x1+x2
y=y1+y2
print x
#合并数据集
vectorizer = CountVectorizer(min_df=1)
x=vectorizer.fit_transform(x)
x=x.toarray()
print y
clf = tree.DecisionTreeClassifier()
print cross_validation.cross_val_score(clf,x,y,n_jobs=-1,cv=10)
#以上是使用决策树训练数据 并且用十折验证
clf = clf.fit(x,y)
#一下是导出数据
dot_data = tree.export_graphviz(clf,out_file=None)
graph=pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf("./photo/ftp_tree.pdf")
以下是显示的效果:
网友评论