-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Applied feeback from Gert, Lisa and Adrienne
- Loading branch information
Showing
11 changed files
with
186 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
.DS_Store | ||
.AppleDouble | ||
.LSOverride | ||
__pycache__/ | ||
.tool-versions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
name: eyra-rank | ||
channels: | ||
- defaults | ||
- conda-forge | ||
dependencies: | ||
- pandas=1.5 | ||
- scikit-learn=1.2 | ||
- joblib=1.1 | ||
- matplotlib=3.7 | ||
- pandas=2.2.1 | ||
- scikit-learn=1.4.1.post1 | ||
- joblib=1.3.2 | ||
- matplotlib=3.8.3 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
""" | ||
This script calls submission.py. Add your method to submission.py to run your | ||
prediction method. | ||
To test your submission use the following command: | ||
python run.py predict | ||
For example: | ||
python run.py predict data/PreFer_fake_data.csv | ||
Optionally, you can use the score function to calculate evaluation scores given | ||
your predictions and the ground truth within the training dataset. | ||
""" | ||
|
||
import sys | ||
import argparse | ||
import pandas as pd | ||
import submission | ||
|
||
parser = argparse.ArgumentParser(description="Score data.") | ||
# Score subcommand | ||
parser.add_argument("prediction_path", help="Path to predicted outcome CSV file.") | ||
# Score subcommand | ||
parser.add_argument("ground_truth_path", help="Path to ground truth outcome CSV file.") | ||
# Score subcommand | ||
parser.add_argument("--output", help="Path to evaluation score output CSV file.") | ||
|
||
args = parser.parse_args() | ||
|
||
|
||
def score(prediction_path, ground_truth_path, output): | ||
"""Score (evaluate) the predictions and write the metrics. | ||
This function takes the path to a CSV file containing predicted outcomes and the | ||
path to a CSV file containing the ground truth outcomes. It calculates the overall | ||
prediction accuracy, and precision, recall, and F1 score for having a child | ||
and writes these scores to a new output CSV file. | ||
This function should not be modified. | ||
""" | ||
|
||
if output is None: | ||
output = sys.stdout | ||
# Load predictions and ground truth into dataframes | ||
predictions_df = pd.read_csv(prediction_path) | ||
ground_truth_df = pd.read_csv(ground_truth_path) | ||
|
||
# Merge predictions and ground truth on the 'id' column | ||
merged_df = pd.merge(predictions_df, ground_truth_df, on="nomem_encr", how="right") | ||
|
||
# Calculate accuracy | ||
accuracy = len(merged_df[merged_df["prediction"] == merged_df["new_child"]]) / len( | ||
merged_df | ||
) | ||
|
||
# Calculate true positives, false positives, and false negatives | ||
true_positives = len( | ||
merged_df[(merged_df["prediction"] == 1) & (merged_df["new_child"] == 1)] | ||
) | ||
false_positives = len( | ||
merged_df[(merged_df["prediction"] == 1) & (merged_df["new_child"] == 0)] | ||
) | ||
false_negatives = len( | ||
merged_df[(merged_df["prediction"] == 0) & (merged_df["new_child"] == 1)] | ||
) | ||
|
||
# Calculate precision, recall, and F1 score | ||
try: | ||
precision = true_positives / (true_positives + false_positives) | ||
except ZeroDivisionError: | ||
precision = 0 | ||
try: | ||
recall = true_positives / (true_positives + false_negatives) | ||
except ZeroDivisionError: | ||
recall = 0 | ||
try: | ||
f1_score = 2 * (precision * recall) / (precision + recall) | ||
except ZeroDivisionError: | ||
f1_score = 0 | ||
# Write metric output to a new CSV file | ||
metrics_df = pd.DataFrame( | ||
{ | ||
"accuracy": [accuracy], | ||
"precision": [precision], | ||
"recall": [recall], | ||
"f1_score": [f1_score], | ||
} | ||
) | ||
metrics_df.to_csv(output, index=False) | ||
|
||
|
||
if __name__ == "__main__": | ||
args = parser.parse_args() | ||
score(args.prediction_path, args.ground_truth_path, args.output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.