Bachstelze commited on
Commit
8a4a582
·
1 Parent(s): 50a170f

first evaluation implementation

Browse files
Files changed (1) hide show
  1. test/test_model.py +59 -1
test/test_model.py CHANGED
@@ -1 +1,59 @@
1
- # test code for the automatic GitHub action
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+
3
+ import pandas as pd
4
+ from sklearn.linear_model import LinearRegression
5
+ from sklearn.metrics import mean_absolute_error, r2_score
6
+
7
+ train_path = "../Datasets_all/A2_dataset_80.csv"
8
+ test_path = "../Datasets_all/A2_dataset_20.csv"
9
+
10
+ # validating the linear regression model based on
11
+ # https://medium.com/@_SSP/validating-machine-learning-regression-models-a-comprehensive-guide-b94fd94e339c
12
+
13
+
14
+ def load_and_evaluate_model(model_path):
15
+ # Load the pickled model
16
+ with open(model_path, "rb") as f:
17
+ model = pickle.load(f)
18
+
19
+ # check the model type
20
+ assert isinstance(model, LinearRegression)
21
+
22
+ # Load data
23
+ train_df = pd.read_csv(train_path)
24
+ test_df = pd.read_csv(test_path)
25
+
26
+ # Define target and features
27
+ target_col = "AimoScore"
28
+ unwanted_cols = ["EstimatedScore"]
29
+ features_cols = [
30
+ col
31
+ for col in train_df.columns
32
+ if col not in unwanted_cols and col != target_col
33
+ ]
34
+
35
+ X_test = test_df[features_cols]
36
+ y_test = test_df[target_col]
37
+
38
+ # Predict on test set
39
+ y_pred = model.predict(X_test)
40
+
41
+ # Evaluate
42
+ mae = mean_absolute_error(y_test, y_pred)
43
+ r2 = r2_score(y_test, y_pred)
44
+
45
+ print(f"Mean Absolute Error on test set: {mae:.4f}")
46
+ print(f"R^2 score on test set: {r2:.4f}")
47
+
48
+ # assert the threeshold values
49
+ assert mae < 0.15, "Mean Absolute Error is too high"
50
+ assert r2 > 0.5, "R^2 score is too low"
51
+
52
+ # Save predictions to CSV
53
+ test_df["Predicted_AimoScore"] = y_pred
54
+ test_df.to_csv("predicted_test.csv", index=False)
55
+
56
+
57
+ if __name__ == "__main__":
58
+ model_path = "linear_regression_model.pkl"
59
+ load_and_evaluate_model(model_path)