Skip to content

Instantly share code, notes, and snippets.

@yagays
Created August 15, 2014 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yagays/951f355a0cebe7c63332 to your computer and use it in GitHub Desktop.
Save yagays/951f355a0cebe7c63332 to your computer and use it in GitHub Desktop.
zozo/recontest
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
# File format : GROUPID,GOODSID,VIEWRATE,BUYRATE
# Usage : calculate_score.py target.csv predict.csv
def read_submit_file(submit_file):
viewrate = {}
buyrate = {}
with open(submit_file) as f:
for line in f:
if line != "GROUPID,GOODSID,VIEWRATE,BUYRATE":
l = line.rstrip().split(",")
if l[0] in viewrate.keys():
viewrate[l[0]][l[1]] = l[2]
else:
viewrate[l[0]] = {l[1]:l[2]}
if l[0] in buyrate.keys():
buyrate[l[0]][l[1]] = l[3]
else:
buyrate[l[0]] = {l[1]:l[3]}
return viewrate,buyrate
def calculate_group_error(y,x):
s = {}
for g in map(str,range(1,96)):
group_score = 0.0
for k,v in y[g].items():
if k in x[g].keys():
group_score += abs(float(v) - float(x[g][k]))
else:
group_score += 1.0
s[g] = group_score
return s
if __name__ == "__main__":
y_view, y_buy = read_submit_file(sys.argv[1])
x_view, x_buy = read_submit_file(sys.argv[2])
view_error_dict = calculate_group_error(y_view, x_view)
buy_error_dict = calculate_group_error(y_buy, x_buy)
score = sum(view_error_dict.values()) + sum(buy_error_dict.values())
print "view error: " + str(sum(view_error_dict.values()))
print "buy error: " + str(sum(buy_error_dict.values()))
print "total score: " + str(score)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment