Spaces:
Configuration error
Configuration error
Update result.py
Browse files
result.py
CHANGED
|
@@ -1,12 +1,74 @@
|
|
| 1 |
import argparse
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def main():
|
| 4 |
-
parser = argparse.ArgumentParser()
|
| 5 |
-
parser.add_argument('--results-path', type=str, required=True, help='Path to results file')
|
| 6 |
-
args = parser.parse_args()
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import argparse
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
from sklearn.metrics import f1_score, precision_score, recall_score, classification_report, confusion_matrix
|
| 5 |
+
import json
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
def evaluate_results():
|
| 9 |
+
dataset_path = "data/processed_train_dataset.csv"
|
| 10 |
+
model_path = "model/best_model.pkl"
|
| 11 |
+
|
| 12 |
+
# Load dataset and model
|
| 13 |
+
df = pd.read_csv(dataset_path)
|
| 14 |
+
model = joblib.load(model_path)
|
| 15 |
|
| 16 |
+
features = [
|
| 17 |
+
'product_category_1',
|
| 18 |
+
'product_category_2',
|
| 19 |
+
'user_depth',
|
| 20 |
+
'age_level',
|
| 21 |
+
'city_development_index',
|
| 22 |
+
'var_1',
|
| 23 |
+
'gender'
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
# Prepare the data
|
| 27 |
+
X = df[features]
|
| 28 |
+
y = df['is_click']
|
| 29 |
+
X = pd.get_dummies(X, columns=['gender'], drop_first=True)
|
| 30 |
+
|
| 31 |
+
# Train-test split
|
| 32 |
+
_, X_test, _, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
|
| 33 |
+
|
| 34 |
+
# Predict
|
| 35 |
+
y_pred = model.predict(X_test)
|
| 36 |
+
|
| 37 |
+
# Calculate metrics
|
| 38 |
+
f1 = f1_score(y_test, y_pred)
|
| 39 |
+
precision = precision_score(y_test, y_pred, zero_division=True)
|
| 40 |
+
recall = recall_score(y_test, y_pred)
|
| 41 |
+
|
| 42 |
+
# Save results
|
| 43 |
+
metrics = {
|
| 44 |
+
'f1_score': f1,
|
| 45 |
+
'precision': precision,
|
| 46 |
+
'recall': recall
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
with open('results/metrics.json', 'w') as f:
|
| 50 |
+
json.dump(metrics, f, indent=4)
|
| 51 |
+
|
| 52 |
+
# Classification report and confusion matrix
|
| 53 |
+
report = classification_report(y_test, y_pred)
|
| 54 |
+
matrix = confusion_matrix(y_test, y_pred)
|
| 55 |
+
|
| 56 |
+
with open('results/classification_report.txt', 'w') as f:
|
| 57 |
+
f.write(report)
|
| 58 |
+
|
| 59 |
+
with open('results/confusion_matrix.txt', 'w') as f:
|
| 60 |
+
f.write(str(matrix))
|
| 61 |
+
|
| 62 |
+
print("Results saved in the 'results' folder.")
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# def main():
|
| 66 |
+
# parser = argparse.ArgumentParser()
|
| 67 |
+
# parser.add_argument('--results-path', type=str, required=True, help='Path to results file')
|
| 68 |
+
# args = parser.parse_args()
|
| 69 |
+
|
| 70 |
+
# print(f"Analyzing results from {args.results_path}")
|
| 71 |
+
# # Add analysis logic here
|
| 72 |
+
|
| 73 |
+
# if __name__ == '__main__':
|
| 74 |
+
# main()
|