main.py works but I think it can be improved. But I don't know how...

This commit is contained in:
Sandro Zimmermann 2025-11-26 23:55:34 +01:00
parent deb2fb80ae
commit de62eb1e43

38
main.py
View File

@ -15,7 +15,6 @@ FEATURES = ["points", "x", "y"]
def load_dataframe():
try:
colum_list = FEATURES
#df = pd.read_csv("data/shots_dev.csv", usecols = colum_list).dropna()
df = pd.read_csv("data/shots.csv", usecols = colum_list).dropna()
return df
except FileNotFoundError as error:
@ -34,8 +33,16 @@ def calc_f1_score(y_true, y_pred):
tn = np.sum(np.multiply([i==False for i in y_pred], [not(j) for j in y_true]))
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))
if tp != 0 and fp != 0:
precision = calc_precision(tp, fp)
else:
precision = 0
if tp != 0 and fn != 0:
recall = calc_recall(tp, fn)
else:
recall = 0
if precision != 0 and recall != 0:
f1 = (2 * precision * recall) / (precision + recall)
@ -49,11 +56,20 @@ def calc_precision(tp, fp):
def calc_recall(tp, fn):
return tp / (tp + fn)
def get_score_from_cli():
try:
culmen_depth = float(input("x: "))
culmen_length = float(input("y: "))
return np.array([culmen_depth, culmen_length]).reshape(1, -1)
except ValueError:
print("Invalid input. Please enter numeric values.")
return None
def main():
df = load_dataframe()
#print(df.head())
'''print(df.head())
'''sns.countplot(x = df["points"])
sns.countplot(x = df["points"])
plt.show()
sns.heatmap(df.corr(), annot=True, cmap='coolwarm')
@ -65,6 +81,7 @@ def main():
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)
@ -86,10 +103,21 @@ def main():
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}')
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}')
print(f'Sklearn F1 score of {name} is {f1_sklearn}\n')
score = get_score_from_cli()
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__":