def kfold(inputs,outputs,config): n_sp = 10 kfolder = KFold(n_splits=n_sp, shuffle=True) fit_acum = 0 acc_acum = 0 time_average_acum = 0 generations_acum = 0 fitn = [] accu = [] tim = [] gen = [] genomes = [] outputs = np.asarray(outputs) inputs = np.asarray(inputs) folds = 0 for train_index, test_index in kfolder.split(inputs): print('--------------------------------------------------------------') print(" KFOLD: " + str(folds)) #print(" TRAIN: ", train_index, " TEST: ", test_index) in_train, in_test = inputs[train_index], inputs[test_index] out_train, out_test = outputs[train_index], outputs[test_index] in_train = in_train.tolist() in_test = in_test.tolist() out_train = out_train.tolist() out_test = out_test.tolist() test_set = list(zip(in_test,out_test)) result = run(in_train, out_train,config) time_average, generations = result['evol'] n_test = len(in_test) fit = kfold_fit(result['net'], test_set, n_test) acc = kfold_acc(result['net'], test_set, n_test) fit_acum += fit acc_acum += acc time_average_acum += time_average generations_acum += generations fitn.append(fit) accu.append(acc) tim.append(time_average) gen.append(generations) genomes.append(result['genome']) print(" FOLD Fit: ", fit) print(" FOLD Acc: ", acc) print(" FOLD Time: ", time_average) folds += 1 final_fit = fit_acum / folds final_acc = acc_acum / folds final_time = time_average_acum / folds final_generations = generations_acum / folds print('--------------------------------------------------------------') print(" Fitness list: ", str(fitn)) print(" Accuracy list: ", str(accu)) print(" Average Time list: ", str(tim)) print(" Generations list: ", str(gen)) print(" Fitness: ", final_fit) print(" Accuracy: ", final_acc) print(" Average Time: ", final_time) print(" Generations: ", final_generations) return {'f_fit': final_fit, 'f_acc': final_acc, 'f_time': final_time, 'f_generations': final_generations, 'fit_list': fitn, 'accu_list': accu, 'time_list' : tim, 'gen_list': gen, 'genomes': genomes }
def kfold(inputs, outputs, config_file): n_sp = 10 kfolder = KFold(n_splits=n_sp, shuffle=True) acc_acum = 0 time_average_acum = 0 generations_acum = 0 outputs = np.asarray(outputs) inputs = np.asarray(inputs) folds = 0 for train_index, test_index in kfolder.split(inputs): print(" NEW KFOLD ") #print(" TRAIN: ", train_index, " TEST: ", test_index) in_train, in_test = inputs[train_index], inputs[test_index] out_train, out_test = outputs[train_index], outputs[test_index] in_train = in_train.tolist() in_test = in_test.tolist() out_train = out_train.tolist() out_test = out_test.tolist() test_set = list(zip(in_test, out_test)) winner, evol = run(in_train, out_train, config_file) time_average, generations = evol n_test = len(in_test) acc = kfold_acc(winner, test_set, n_test) acc_acum += acc time_average_acum += time_average generations_acum += generations print(" FOLD Acc: ", acc) print(" FOLD Time: ", time_average) folds += 1 final_acc = acc_acum / folds final_time = time_average_acum / folds final_generations = generations_acum / folds print(" Accuracy: ", final_acc) print(" Average Time: ", final_time) print(" Generations: ", final_generations) return final_time, final_generations
fitness_acum = 0 acc_acum = 0 diabetes_output = np.asarray(diabetes_output) diabetes_inputs = np.asarray(diabetes_inputs) folds = 0 for train_index, test_index in kfolder.split(diabetes_inputs): print(" NEW KFOLD ") #print(" TRAIN: ", train_index, " TEST: ", test_index) in_train, in_test = diabetes_inputs[train_index], diabetes_inputs[ test_index] out_train, out_test = diabetes_output[train_index], diabetes_output[ test_index] in_train = in_train.tolist() in_test = in_test.tolist() out_train = out_train.tolist() out_test = out_test.tolist() test_set = list(zip(in_test, out_test)) winner = run(in_train, out_train) n_test = len(in_test) fold_fitness = kfold_fitness(winner, test_set, n_test) acc = kfold_acc(winner, test_set, n_test) acc_acum += acc fitness_acum += fold_fitness print(" FOLD FITNESS : ", fold_fitness) print(" FOLD ACC : ", acc) folds += 1 final_fitness = fitness_acum / folds final_acc = acc_acum / folds print(" FITNESS TOTAL : ", final_fitness) print(" ACC TOTAL : ", final_acc)