From 1f31f125a1237d3fed269f063950d04f41c9f14d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Batista?= Date: Mon, 3 Oct 2022 13:04:36 +0100 Subject: [PATCH] removing files --- Main_STGP.py | 77 --------------------- Main_STGP_example.py | 44 ------------ Main_STGP_standalone.py | 145 ---------------------------------------- 3 files changed, 266 deletions(-) delete mode 100644 Main_STGP.py delete mode 100644 Main_STGP_example.py delete mode 100644 Main_STGP_standalone.py diff --git a/Main_STGP.py b/Main_STGP.py deleted file mode 100644 index 5cf86da..0000000 --- a/Main_STGP.py +++ /dev/null @@ -1,77 +0,0 @@ -import pandas -from sklearn.model_selection import train_test_split - -from stgp.STGP import STGP -from stgp.Constants import * -import os - - -# -# By using this file, you are agreeing to this product's EULA -# -# This product can be obtained in https://github.com/jespb/Python-STGP -# -# Copyright ©2019 J. E. Batista -# - - -def readDataset(filename, seed = 0): - df = pandas.read_csv(filename) - train, test = train_test_split(df, train_size = TRAIN_FRACTION, random_state = seed) - return train, test - - -def run(dataset, run_id = 0): - if VERBOSE: - print("> Starting run:") - print(" > ID:", run_id) - print(" > Dataset:", dataset) - print() - - train, test = readDataset(DATASETS_DIR+dataset, seed = run_id) - stgp = STGP(train, test) - - accuracy = stgp.getAccuracyOverTime() - rmse = stgp.getRmseOverTime() - size = stgp.getSizeOverTime() - times = stgp.getGenerationTimes() - - return ([accuracy[0],accuracy[1],rmse[0],rmse[1],size,times,str(stgp.getBestIndividual())]) - - - -def callstgp(dataset): - toWrite=[] - for run_id in range(RUNS): - toWrite.append( run(dataset, run_id) ) - - openFile(OUTPUT_DIR+"stgp_"+dataset) - writeToFile("Attribute,Run,") - for i in range(MAX_GENERATION): - writeToFile(str(i)+",") - writeToFile("\n") - - attributes= ["Training-Accuracy","Test-Accuracy","Training-RMSE","Test-RMSE","Size","Generation-Time","Final_Model"] - for ai in range(len(toWrite[0])-1): - for i in range(len(toWrite)): - writeToFile("\n"+attributes[ai]+","+str(i)+",") - for val in toWrite[i][ai]: - writeToFile(str(val)+",") - writeToFile("\n\n") - for i in range(len(toWrite)): - writeToFile("\n"+attributes[-1]+","+str(i)+",") - writeToFile(str(toWrite[i][-1])) - writeToFile("\n\n") - - closeFile() - - - -if __name__ == '__main__': - try: - os.makedirs(OUTPUT_DIR) - except: - pass - - for dataset in DATASETS: - callstgp(dataset) \ No newline at end of file diff --git a/Main_STGP_example.py b/Main_STGP_example.py deleted file mode 100644 index 49664ae..0000000 --- a/Main_STGP_example.py +++ /dev/null @@ -1,44 +0,0 @@ -import pandas - -from stgp.STGP import STGP - -from sklearn.model_selection import train_test_split - -from sklearn.metrics import accuracy_score - -import warnings - -warnings.filterwarnings("ignore", category=FutureWarning, - message="From version 0.21, test_size will always complement", - module="sklearn") - -# -# By using this file, you are agreeing to this product's EULA -# -# This product can be obtained in https://github.com/jespb/Python-STGP -# -# Copyright ©2019-2021 J. E. Batista -# - - - -filename= "heart.csv" - -# Open the dataset -ds = pandas.read_csv("datasets/"+filename) -class_header = ds.columns[-1] - -# Split the dataset -Tr_X, Te_X, Tr_Y, Te_Y = train_test_split(ds.drop(columns=[class_header]), ds[class_header], - train_size=0.7, random_state = 42, stratify = ds[class_header]) - -# Train a model -model = STGP() -model.fit(Tr_X, Tr_Y) - -# Predict test results -pred = model.predict(Te_X) - -# Obtain test accuracy -print( accuracy_score(pred, Te_Y) ) - diff --git a/Main_STGP_standalone.py b/Main_STGP_standalone.py deleted file mode 100644 index 6258428..0000000 --- a/Main_STGP_standalone.py +++ /dev/null @@ -1,145 +0,0 @@ -import pandas - -from stgp.STGP import STGP -from sys import argv -from Arguments import * -import os - -from sklearn.model_selection import train_test_split - -import numpy as np - -import warnings - -warnings.filterwarnings("ignore", category=FutureWarning, - message="From version 0.21, test_size will always complement", - module="sklearn") - - -# -# By using this file, you are agreeing to this product's EULA -# -# This product can be obtained in https://github.com/jespb/Python-STGP -# -# Copyright ©2019-2021 J. E. Batista -# - - - - -def openAndSplitDatasets(which,seed): - if VERBOSE: - print( "> Opening: ", which ) - - # Open dataset - ds = pandas.read_csv(DATASETS_DIR+which) - - # Read header - class_header = ds.columns[-1] - - return train_test_split(ds.drop(columns=[class_header]), ds[class_header], - train_size=TRAIN_FRACTION, random_state=seed, - stratify = ds[class_header]) - - -def run(r,dataset): - if VERBOSE: - print("> Starting run:") - print(" > ID:", r) - print(" > Dataset:", dataset) - print() - - Tr_X, Te_X, Tr_Y, Te_Y = openAndSplitDatasets(dataset,r) - - # Train a model - model = STGP(OPERATORS, MAX_DEPTH, POPULATION_SIZE, MAX_GENERATION, TOURNAMENT_SIZE, - ELITISM_SIZE, LIMIT_DEPTH, THREADS, VERBOSE) - model.fit(Tr_X, Tr_Y, Te_X, Te_Y) - - - # Obtain training results - accuracy = model.getAccuracyOverTime() - rmse = model.getRMSEOverTime() - size = model.getSizeOverTime() - model_str = str(model.getBestIndividual()) - times = model.getGenerationTimes() - - tr_acc = accuracy[0] - te_acc = accuracy[1] - tr_rmse = rmse[0] - te_rmse = rmse[1] - - if VERBOSE: - print("> Ending run:") - print(" > ID:", r) - print(" > Dataset:", dataset) - print(" > Final model:", model_str) - print(" > Training accuracy:", tr_acc[-1]) - print(" > Test accuracy:", te_acc[-1]) - print() - - return (tr_acc,te_acc, - tr_rmse,te_rmse, - size, times, - model_str) - - -def callm3gp(): - try: - os.makedirs(OUTPUT_DIR) - except: - pass - - for dataset in DATASETS: - outputFilename = OUTPUT_DIR+"stgp_"+ dataset - if not os.path.exists(outputFilename): - results = [] - - # Run the algorithm several times - for r in range(RUNS): - results.append(run(r,dataset)) - - # Write output header - file = open(outputFilename , "w") - file.write("Attribute,Run,") - for i in range(MAX_GENERATION): - file.write(str(i)+",") - file.write("\n") - - attributes= ["Training-Accuracy","Test-Accuracy", - "Training-RMSE", "Test-RMSE", - "Size", "Time", - "Final_Model"] - - # Write attributes with value over time - for ai in range(len(attributes)-1): - for i in range(RUNS): - file.write("\n"+attributes[ai]+","+str(i)+",") - file.write( ",".join([str(val) for val in results[i][ai]])) - file.write("\n") - - # Write the final models - for i in range(len(results)): - file.write("\n"+attributes[-1]+","+str(i)+",") - file.write(results[i][-1]) - file.write("\n") - - # Write some parameters - file.write("\n\nParameters") - file.write("\nOperators,"+str(OPERATORS)) - file.write("\nMax Initial Depth,"+str(MAX_DEPTH)) - file.write("\nPopulation Size,"+str(POPULATION_SIZE)) - file.write("\nMax Generation,"+str(MAX_GENERATION)) - file.write("\nTournament Size,"+str(TOURNAMENT_SIZE)) - file.write("\nElitism Size,"+str(ELITISM_SIZE)) - file.write("\nDepth Limit,"+str(LIMIT_DEPTH)) - file.write("\nThreads,"+str(THREADS)) - - - file.close() - else: - print("Filename: " + outputFilename +" already exists.") - - -if __name__ == '__main__': - callm3gp()