Created
October 25, 2015 07:30
-
-
Save binga/1adc279f28ff6903c8ba to your computer and use it in GitHub Desktop.
Evaluation Metric for Analytics Vidhya DHack (Python)
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
import pandas as pd | |
def diff_to_score(diff): | |
if diff == 0: | |
return 50 | |
elif diff == 5: | |
return 10 | |
elif diff == 10: | |
return 5 | |
elif diff == -5: | |
return -5 | |
elif diff == -10: | |
return -10 | |
def happy_to_scores(x): | |
if x == 'Very Happy': | |
return 15 | |
elif x == 'Pretty Happy': | |
return 10 | |
elif x == 'Not Happy': | |
return 5 | |
def eval_metric(y_true, y_pred): | |
scores = pd.DataFrame({'actual': y_true, 'predicted': y_pred}) | |
scores['actual'] = scores['actual'].apply(lambda x: happy_to_scores(x)) | |
scores['predicted'] = scores['predicted'].apply(lambda x: happy_to_scores(x)) | |
# scores['actual'] = scores['actual'].astype(int) | |
# scores['predicted'] = scores['predicted'].astype(int) | |
scores['diff'] = scores['actual'] - scores['predicted'] | |
scores['scores'] = scores['diff'].map(lambda x: diff_to_score(x)) | |
final_score = sum(scores['scores']) / (float(len(scores)) * 50) | |
return final_score | |
# test | |
# a = ['Pretty Happy', 'Very Happy', 'Not Happy'] | |
# b = ['Very Happy', 'Very Happy', 'Very Happy'] | |
# print eval_metric(a, b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can I have your email ID if possible?