>>> import numpy as np>>> from sklearn.model_selection import KFold >>> X = ["a", "b", "c", "d"]>>> kf = KFold(n_splits=2)>>> for train, test in kf.split(X):... print("%s %s" % (train, test))[2 3] [0 1][0 1] [2 3]
# -*- coding:utf-8 -*-# 得到交叉验证数据集,保存成CSV文件# 输入是一个包含正常恶意标签的完整数据集,在读数据的时候分开保存到datasetBenign,datasetMalicious# 分别对两个数据集进行KFold,最后合并保存 from sklearn.model_selection import KFoldimport csv def writeInFile(benignKFTrain, benignKFTest, maliciousKFTrain, maliciousKFTest, i, datasetBenign, datasetMalicious): newTrainFilePath = "E://hadoopExperimentResult//5KFold//AllDataSetIIR10//dataset//ImbalancedAllTraffic-train-%s.csv" % i newTestFilePath = "E://hadoopExperimentResult//5KFold//AllDataSetIIR10//dataset//IImbalancedAllTraffic-test-%s.csv" % i newTrainFile = open(newTrainFilePath, "wb")# wb 为防止空行 newTestFile = open(newTestFilePath, "wb") writerTrain = csv.writer(newTrainFile) writerTest = csv.writer(newTestFile) for index in benignKFTrain: writerTrain.writerow(datasetBenign[index]) for index in benignKFTest: writerTest.writerow(datasetBenign[index]) for index in maliciousKFTrain: writerTrain.writerow(datasetMalicious[index]) for index in maliciousKFTest: writerTest.writerow(datasetMalicious[index]) newTrainFile.close() newTestFile.close() def getKFoldDataSet(datasetPath): # CSV读取文件 # 开始从文件中读取全部的数据集 datasetFile = file(datasetPath, 'rb') datasetBenign = [] datasetMalicious = [] readerDataset = csv.reader(datasetFile) for line in readerDataset: if len(line) > 1: curLine = [] curLine.append(float(line[0])) curLine.append(float(line[1])) curLine.append(float(line[2])) curLine.append(float(line[3])) curLine.append(float(line[4])) curLine.append(float(line[5])) curLine.append(float(line[6])) curLine.append(line[7]) if line[7] == "benign": datasetBenign.append(curLine) else: datasetMalicious.append(curLine) # 交叉验证分割数据集 K = 5 kf = KFold(n_splits=K) benignKFTrain = []; benignKFTest = [] for train,test in kf.split(datasetBenign): benignKFTrain.append(train) benignKFTest.append(test) maliciousKFTrain=[]; maliciousKFTest=[] for train,test in kf.split(datasetMalicious): maliciousKFTrain.append(train) maliciousKFTest.append(test) for i in range(K): print "======================== "+ str(i)+ " ========================" print benignKFTrain[i], benignKFTest[i] print maliciousKFTrain[i],maliciousKFTest[i] writeInFile(benignKFTrain[i], benignKFTest[i], maliciousKFTrain[i], maliciousKFTest[i], i, datasetBenign, datasetMalicious) datasetFile.close() if __name__ == "__main__": getKFoldDataSet(r"E:/hadoopExperimentResult/5KFold/AllDataSetIIR10/dataset/ImbalancedAllTraffic-10.csv")