DSIP / result.py
KaiquanMah's picture
Update result.py
bdd73b1 verified
raw
history blame
1.94 kB
import argparse
import pandas as pd
import joblib
from sklearn.metrics import f1_score, precision_score, recall_score, classification_report, confusion_matrix
import json
def evaluate_results():
dataset_path = "data/train.csv"
model_path = "model/best_model.pkl"
# Load dataset and model
df = pd.read_csv(dataset_path)
model = joblib.load(model_path)
features = [
'product_category_1',
'product_category_2',
'user_depth',
'age_level',
'city_development_index',
'var_1',
'gender'
]
# Prepare the data
X = df[features]
y = df['is_click']
X = pd.get_dummies(X, columns=['gender'], drop_first=True)
# Train-test split
_, X_test, _, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Predict
y_pred = model.predict(X_test)
# Calculate metrics
f1 = f1_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, zero_division=True)
recall = recall_score(y_test, y_pred)
# Save results
metrics = {
'f1_score': f1,
'precision': precision,
'recall': recall
}
with open('results/metrics.json', 'w') as f:
json.dump(metrics, f, indent=4)
# Classification report and confusion matrix
report = classification_report(y_test, y_pred)
matrix = confusion_matrix(y_test, y_pred)
with open('results/classification_report.txt', 'w') as f:
f.write(report)
with open('results/confusion_matrix.txt', 'w') as f:
f.write(str(matrix))
print("Results saved in the 'results' folder.")
# def main():
# parser = argparse.ArgumentParser()
# parser.add_argument('--results-path', type=str, required=True, help='Path to results file')
# args = parser.parse_args()
# print(f"Analyzing results from {args.results_path}")
# # Add analysis logic here
# if __name__ == '__main__':
# main()