From 2becf5823bdedf3da0e31310c7a0a5852e9414c9 Mon Sep 17 00:00:00 2001 From: Sandro Zimmermann Date: Sat, 29 Nov 2025 17:40:32 +0100 Subject: [PATCH] restructioring --- __pycache__/arguments.cpython-313.pyc | Bin 3232 -> 3482 bytes main.py | 166 +++++++++++------- absolut.py => py/absolut.py | 0 arguments.py => py/arguments.py | 17 +- py/cartesian.py | 0 py/functions.py | 0 .../generate_synthetic_shots.py | 2 +- vector.py => py/vector.py | 0 8 files changed, 115 insertions(+), 70 deletions(-) rename absolut.py => py/absolut.py (100%) rename arguments.py => py/arguments.py (76%) create mode 100644 py/cartesian.py create mode 100644 py/functions.py rename generate_synthetic_shots.py => py/generate_synthetic_shots.py (96%) rename vector.py => py/vector.py (100%) diff --git a/__pycache__/arguments.cpython-313.pyc b/__pycache__/arguments.cpython-313.pyc index 323885f023aaaab14ce6a596dcf02cc2d32f90e7..c4fd6a2517ceff9cb14fad2dca9d6dcda2cf2e95 100644 GIT binary patch delta 792 zcmZ8fO=}ZT6rDGjuY62W+uA0XCK+v#nwo~h57ba7rHQ@SVNh{!YKMC728d+ z5+W^KTtsl?(zp?Y`*g$GfA2k!W}UJYdQj(46{*B@I?YR-=#B(KO+(qAI(9()cjA-opK zLv}wQ2xZe|N0)NmCZ@Z}CHUyMGAVZ?$Ff&>5`#QNkS3TyaHxZ6n()Un9T~@SLug0N zZUx(;;Vp05NPpGB?bA`XE|pjd8q&S`KpVLON96UmqpH9sG03wB8IhmEts_6L*z1-< zRi?>DDWp4AZFzOgww9k(YL7bOgYk365(aV|6``S^z4&$z<|TvW;J0Lmw?&v!qq^7| ztbimw0~2yK5~m_^h9iYWb~c(DJN0ILPm9B@`USGeY>*SX<#FBd0=!TP>Y`vuqlE zX%%S>qYziePWGb@(__!T8*gqnk9TJnp*P89a}(aHZD{#U9d(LPg;FSB!85l=d|CeJNS$D;Jo+|<01;+FzI zWywq+#ZUlZGXwEwDWV?qlQ~$lI#v)aqh9aHGXIOPar9md~Kn&m)?yu~uyucx`S&)sJQQ#J9Sz=CU>PqG!4Ul@% z$y)4&jB=A>*t;fYuqcDfQG=MH&0)m{b`c-UoXH2dZcGm36qhjuifFPFiGdV?RhxiV zrXa#>ato&oqx$57oQaH9ll@t2Kqjh#WOODsa_Ncc0+~oItLm(}z#%zVknQ~BncRXf zgF#-0XhJsl7`FkV!Q}7UF|wd=2HFpD9Yh(}5g`BjY4T1k;E|FCSyN;HA}m0J4T!J@ z5l$e&b@DnMHEocZHil0uEUZ$Wco ") + print("Usage: python3 main.py ") sys.exit(1) -args = Arguments(sys.argv[1]) -args.set_mode(sys.argv[2]) - -try: - args.set_information(sys.argv[3]) - args.set_graph(sys.argv[4]) -except IndexError: - args.set_information(False) - args.set_graph(False) - FEATURES = ["points", "x", "y"] # create dataframe from csv and drop any row with null values @@ -50,12 +40,10 @@ def calc_f1_score(y_true, y_pred): fp = np.sum(np.multiply([i==True for i in y_pred], [not(j) for j in y_true])) fn = np.sum(np.multiply([i==False for i in y_pred], y_true)) - '''print(tp) - print(fp) - precision = calc_precision(tp, fp) - recall = calc_recall(tp, fn)''' - + recall = calc_recall(tp, fn) + + ''' if tp != 0 and fp != 0: precision = calc_precision(tp, fp) else: @@ -65,7 +53,8 @@ def calc_f1_score(y_true, y_pred): recall = calc_recall(tp, fn) else: recall = 0 - + ''' + if precision != 0 and recall != 0: f1 = (2 * precision * recall) / (precision + recall) else: @@ -78,69 +67,122 @@ def calc_precision(tp, fp): def calc_recall(tp, fn): return tp / (tp + fn) -def get_score_from_cli(): +def get_score_from_cli(mode): try: x = float(input("x: ")) y = float(input("y: ")) + if mode == "v": + abs_v = np.sqrt(x**2 + y**2) + return np.array([abs_v]).reshape(1, -1) return np.array([x, y]).reshape(1, -1) except ValueError: print("Invalid input. Please enter numeric values.") return None def main(): - df = load_dataframe(args.get_file_path()) - print(df.describe()) - #print(df.head()) - #print(df.head().info()) - sns.countplot(x = df["points"]) - plt.show() - sns.heatmap(df.corr(), annot=True, cmap='coolwarm') - plt.show() + repeat = True + args = Arguments(sys.argv[1], "v", False, False) + args.set_mode("v") + args.set_information(False) + args.set_graph(False) - sns.scatterplot(x=df['x'], y=df['y'], hue=df['points']) - plt.show() - - - features = ["x", "y"] - X = df[features] - - y = pd.get_dummies(df['points']) - - X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0) - - random_forest = RandomForestClassifier(n_estimators=700, random_state=0) - decision_tree = DecisionTreeClassifier(random_state=0) - k_neighbors = KNeighborsClassifier(n_neighbors=5) - - models = { - "Random Forest Classifier": random_forest, - "Decision Tree Classifier": decision_tree, - "K-Neighbors": k_neighbors + settings = { + "repeat": True, + "file": args.get_file_path(), + "mode": args.get_mode(), + "information": args.get_information(), + "graph": args.get_graph() } - for name, model in models.items(): - model.fit(X_train.values, y_train.values) + while repeat: + print("Currently selected setting:") + print(f"File: {settings["file"]}") + print(f"Mode: {settings["mode"]}") + print(f"Display information: {settings["information"]}") + print(f"Display graphs: {settings["graph"]}") - for name, model in models.items(): - pred = model.predict(X_test.values) + prompt = input("Change settings [y / exit]: ") - my_f1_macro_score = calc_f1_macro(y_test, pd.DataFrame(pred)) - print(f'My F1 score of {name} is {my_f1_macro_score}\n') + if prompt == "y": + args.set_file_path(input("Change file : ")) + args.set_mode(input("Change mode [v, a, c]: ")) + args.set_information(bool(input("Display information [True / False]: "))) + args.set_graph(bool(input("Display graphs [True / False]: "))) + elif prompt == "exit": + quit() + + # load dataframe with argument [1] + df = load_dataframe(args.get_file_path()) - f1_sklearn = f1_score(y_test.values, pred, average='macro') - print(f'Sklearn F1 score of {name} is {f1_sklearn}\n') + # print dataframe information if argument [3] is true + if args.get_information(): + print(df.describe()) + print(df.head()) + print(df.head().info()) - score = get_score_from_cli() + # display graphs if argument [4] is true + if args.get_graph(): - label_encoder = LabelEncoder() - df["points"] = label_encoder.fit_transform(df["points"]) + sns.countplot(x = df["points"]) + plt.show() - for name, model in models.items(): - pred = model.predict(score) - points_number = pd.DataFrame(pred).idxmax(axis=1) - points = label_encoder.inverse_transform(points_number)[0] - print(f"{name}: {points} Punkte") + sns.heatmap(df.corr(), annot=True, cmap='coolwarm') + plt.show() + + sns.scatterplot(x=df['x'], y=df['y'], hue=df['points']) + plt.show() + + # use verctor length of (x,y) as feature + if args.get_mode() == "v": + df["radius"] = np.sqrt(df["x"]**2 + df["y"]**2) + X = df[["radius"]] + # use absoult values of (x,y) as feature + elif args.get_mode() == "a": + df_abs = df.copy().abs() + features = ["x", "y"] + X = df[features] + # use unaltered values of (x,y) as feature + elif args.get_mode() == "c": + features = ["x", "y"] + X = df[features] + + y = pd.get_dummies(df['points']) + + X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0) + + random_forest = RandomForestClassifier(n_estimators=700, random_state=0) + decision_tree = DecisionTreeClassifier(random_state=0) + k_neighbors = KNeighborsClassifier(n_neighbors=5) + + models = { + "Random Forest Classifier": random_forest, + "Decision Tree Classifier": decision_tree, + "K-Neighbors": k_neighbors + } + + for name, model in models.items(): + model.fit(X_train.values, y_train.values) + + for name, model in models.items(): + pred = model.predict(X_test.values) + + my_f1_macro_score = calc_f1_macro(y_test, pd.DataFrame(pred)) + print(f'My F1 score of {name} is {my_f1_macro_score}\n') + + f1_sklearn = f1_score(y_test.values, pred, average='macro') + print(f'Sklearn F1 score of {name} is {f1_sklearn}\n') + + score = get_score_from_cli(args.get_mode) + + label_encoder = LabelEncoder() + df["points"] = label_encoder.fit_transform(df["points"]) + + for name, model in models.items(): + pred = model.predict(score) + points_number = pd.DataFrame(pred).idxmax(axis=1) + points = label_encoder.inverse_transform(points_number)[0] + print(f"{name}: {points} Punkte") if __name__ == "__main__": diff --git a/absolut.py b/py/absolut.py similarity index 100% rename from absolut.py rename to py/absolut.py diff --git a/arguments.py b/py/arguments.py similarity index 76% rename from arguments.py rename to py/arguments.py index ce15969..63a7808 100644 --- a/arguments.py +++ b/py/arguments.py @@ -15,17 +15,20 @@ class Graph(Enum): class Arguments: - def __init__(self, file_path): + def __init__(self, file_path, mode, information, graph): self.file_path = file_path - self.mode = None - self.information = None - self.graph = None + self.mode = mode + self.information = information + self.graph = graph def get_file_path(self): return self.file_path + def set_file_path(self, value): + self.file_path = value + def get_mode(self): - return self.mode + return self.mode.value def set_mode(self, value): try: @@ -34,7 +37,7 @@ class Arguments: raise ValueError(f"Invalid mode '{value}'. Allowed values: {[m.value for m in Mode]}") def get_information(self): - return self.information + return self.information.value def set_information(self, value): try: @@ -43,7 +46,7 @@ class Arguments: raise ValueError(f"Invalid information '{value}'. Allowed values: {[m.value for m in Information]}") def get_graph(self): - return self.graph + return self.graph.value def set_graph(self, value): try: diff --git a/py/cartesian.py b/py/cartesian.py new file mode 100644 index 0000000..e69de29 diff --git a/py/functions.py b/py/functions.py new file mode 100644 index 0000000..e69de29 diff --git a/generate_synthetic_shots.py b/py/generate_synthetic_shots.py similarity index 96% rename from generate_synthetic_shots.py rename to py/generate_synthetic_shots.py index f93e76e..2811ef7 100644 --- a/generate_synthetic_shots.py +++ b/py/generate_synthetic_shots.py @@ -68,7 +68,7 @@ for i in xy: elif A > A1: dataset.append([0, i[0], i[1]]) -with open('data/synthetic_shots.csv', 'w', newline='') as csvfile: +with open('../data/synthetic_shots.csv', 'w', newline='') as csvfile: fieldnames = ['points', 'x', 'y'] writer = csv.writer(csvfile) writer.writerow(fieldnames) diff --git a/vector.py b/py/vector.py similarity index 100% rename from vector.py rename to py/vector.py