restructioring
This commit is contained in:
parent
c1a72b706d
commit
2becf5823b
Binary file not shown.
84
main.py
84
main.py
@ -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,6 +53,7 @@ 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)
|
||||
@ -78,20 +67,63 @@ 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():
|
||||
|
||||
repeat = True
|
||||
args = Arguments(sys.argv[1], "v", False, False)
|
||||
args.set_mode("v")
|
||||
args.set_information(False)
|
||||
args.set_graph(False)
|
||||
|
||||
settings = {
|
||||
"repeat": True,
|
||||
"file": args.get_file_path(),
|
||||
"mode": args.get_mode(),
|
||||
"information": args.get_information(),
|
||||
"graph": args.get_graph()
|
||||
}
|
||||
|
||||
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"]}")
|
||||
|
||||
prompt = input("Change settings [y / exit]: ")
|
||||
|
||||
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())
|
||||
|
||||
# print dataframe information if argument [3] is true
|
||||
if args.get_information():
|
||||
print(df.describe())
|
||||
#print(df.head())
|
||||
#print(df.head().info())
|
||||
print(df.head())
|
||||
print(df.head().info())
|
||||
|
||||
# display graphs if argument [4] is true
|
||||
if args.get_graph():
|
||||
|
||||
sns.countplot(x = df["points"])
|
||||
plt.show()
|
||||
|
||||
@ -101,7 +133,17 @@ def main():
|
||||
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]
|
||||
|
||||
@ -131,7 +173,7 @@ def main():
|
||||
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()
|
||||
score = get_score_from_cli(args.get_mode)
|
||||
|
||||
label_encoder = LabelEncoder()
|
||||
df["points"] = label_encoder.fit_transform(df["points"])
|
||||
|
||||
@ -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
0
py/cartesian.py
Normal file
0
py/functions.py
Normal file
0
py/functions.py
Normal 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)
|
||||
Loading…
x
Reference in New Issue
Block a user