MCPcopy Create free account
hub / github.com/bobolike123/NetworkTrafficAnalysis / divideDataSet

Class divideDataSet

tools/copyRandomSample.py:35–70  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

33
34
35class divideDataSet:
36 # EXTRACT_RATIO = 0.2 每个家族抽取样本的比例 ,0.2表示按照8:2划分训练集和验证集
37
38 def __init__(self, origin_path, goal_path, EXTRACT_RATIO=0.2):
39 self.origin_path = origin_path
40 self.goal_path = goal_path
41 self.EXTRACT_RATIO = EXTRACT_RATIO
42
43 def checkFileisExist(self):
44 try:
45 createBlankFile(self.origin_path, self.goal_path).createNewFiles()
46 Flag = True
47 except:
48 print('something wrong in checking File is exist')
49 Flag = False
50 return Flag
51
52 def copyFile(self):
53 list = readFilenameList(self.origin_path)
54 # print(list)
55 flag = self.checkFileisExist()
56 if flag == False: return 'moveFile Failed'
57 for malFamily in list: # 对train文件夹下的每个子文件夹,随机取出EXTRACT_RATIO比例的文件放入validation文件夹中
58 file_abs_path = self.origin_path + "\\" + malFamily
59 pathDir = readFilenameList(file_abs_path)
60 # filenumber = len(pathDir)
61 # picknumber = int(filenumber * self.EXTRACT_RATIO)
62 picknumber = dict_num[malFamily]
63 sample = random.sample(pathDir, picknumber)
64 # print(sample)
65 for name in sample:
66 # shutil.move(self.origin_path + '\\' + malFamily + '\\' + name,
67 # self.goal_path + '\\' + malFamily + '\\' + name)
68 shutil.copy(self.origin_path + '\\' + malFamily + '\\' + name,
69 self.goal_path + '\\' + malFamily + '\\' + name)
70 return 'moveFile Succeed!'
71
72if __name__ == '__main__':
73 divideDataSet(origin_path=r'K:\数据库\ISCX-IDS-2012\2_extractFlowFeature',goal_path=r'K:\数据库\ISCX-IDS-2012\3_sampleDataset').copyFile()

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected