restructioring

This commit is contained in:
Sandro Zimmermann 2025-11-29 17:40:32 +01:00
parent c1a72b706d
commit 2becf5823b
8 changed files with 115 additions and 70 deletions

166
main.py
View File

@ -1,5 +1,4 @@
import sys
from arguments import Arguments
import pandas as pd
import numpy as np
import seaborn as sns
@ -10,21 +9,12 @@ from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import LabelEncoder
from py.arguments import Arguments
if not sys.argv[1:]:
print("Usage: python3 main.py <path to csv> <mode vector [v] (default) or absolut [a] or cartesian [c]> <optional information [true]> <optional graphs [true]>")
print("Usage: python3 main.py <path to csv>")
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 <path to 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__":

View File

@ -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:

0
py/cartesian.py Normal file
View File

0
py/functions.py Normal file
View File

View File

@ -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)