提交 d86fb1d1 编写于 作者: D dzhwinter

"precommit format with github style"

上级 82eb0fe4
...@@ -23,7 +23,6 @@ http://research.microsoft.com/en-us/um/beijing/projects/letor/LETOR4.0/Data/MQ20 ...@@ -23,7 +23,6 @@ http://research.microsoft.com/en-us/um/beijing/projects/letor/LETOR4.0/Data/MQ20
""" """
import os import os
import random import random
import functools import functools
...@@ -31,7 +30,6 @@ import rarfile ...@@ -31,7 +30,6 @@ import rarfile
from common import download from common import download
import numpy as np import numpy as np
# URL = "http://research.microsoft.com/en-us/um/beijing/projects/letor/LETOR4.0/Data/MQ2007.rar" # URL = "http://research.microsoft.com/en-us/um/beijing/projects/letor/LETOR4.0/Data/MQ2007.rar"
URL = "http://www.bigdatalab.ac.cn/benchmark/upload/download_source/7b6dbbe2-842c-11e4-a536-bcaec51b9163_MQ2007.rar" URL = "http://www.bigdatalab.ac.cn/benchmark/upload/download_source/7b6dbbe2-842c-11e4-a536-bcaec51b9163_MQ2007.rar"
MD5 = "7be1640ae95c6408dab0ae7207bdc706" MD5 = "7be1640ae95c6408dab0ae7207bdc706"
...@@ -63,8 +61,12 @@ class Query(object): ...@@ -63,8 +61,12 @@ class Query(object):
description : string description : string
comment section in query doc pair data comment section in query doc pair data
""" """
def __init__(self, query_id=-1, relevance_score=-1,
feature_vector=None, description=""): def __init__(self,
query_id=-1,
relevance_score=-1,
feature_vector=None,
description=""):
self.query_id = query_id self.query_id = query_id
self.relevance_score = relevance_score self.relevance_score = relevance_score
if feature_vector is None: if feature_vector is None:
...@@ -74,7 +76,8 @@ class Query(object): ...@@ -74,7 +76,8 @@ class Query(object):
self.description = description self.description = description
def __str__(self): def __str__(self):
string = "%s %s %s" %(str(self.relevance_score), str(self.query_id), " ".join(str(f) for f in self.feature_vector)) string = "%s %s %s" % (str(self.relevance_score), str(self.query_id),
" ".join(str(f) for f in self.feature_vector))
return string return string
# @classmethod # @classmethod
...@@ -84,9 +87,10 @@ class Query(object): ...@@ -84,9 +87,10 @@ class Query(object):
""" """
comment_position = text.find('#') comment_position = text.find('#')
line = text[:comment_position].strip() line = text[:comment_position].strip()
self.description = text[comment_position+1:].strip() self.description = text[comment_position + 1:].strip()
parts = line.split() parts = line.split()
assert(len(parts) == 48), "expect 48 space split parts, get %d" %(len(parts)) assert (len(parts) == 48), "expect 48 space split parts, get %d" % (
len(parts))
# format : 0 qid:10 1:0.000272 2:0.000000 .... # format : 0 qid:10 1:0.000272 2:0.000000 ....
self.relevance_score = int(parts[0]) self.relevance_score = int(parts[0])
self.query_id = int(parts[1].split(':')[1]) self.query_id = int(parts[1].split(':')[1])
...@@ -95,10 +99,12 @@ class Query(object): ...@@ -95,10 +99,12 @@ class Query(object):
self.feature_vector.append(float(pair[1])) self.feature_vector.append(float(pair[1]))
return self return self
class QueryList(object): class QueryList(object):
""" """
group query into list, every item in list is a Query group query into list, every item in list is a Query
""" """
def __init__(self, querylist=None): def __init__(self, querylist=None):
self.query_id = -1 self.query_id = -1
if querylist is None: if querylist is None:
...@@ -122,7 +128,7 @@ class QueryList(object): ...@@ -122,7 +128,7 @@ class QueryList(object):
def _correct_ranking_(self): def _correct_ranking_(self):
if self.querylist is None: if self.querylist is None:
return return
self.querylist.sort(key=lambda x:x.relevance_score, reverse=True) self.querylist.sort(key=lambda x: x.relevance_score, reverse=True)
def _add_query(self, query): def _add_query(self, query):
if self.query_id == -1: if self.query_id == -1:
...@@ -133,7 +139,6 @@ class QueryList(object): ...@@ -133,7 +139,6 @@ class QueryList(object):
self.querylist.append(query) self.querylist.append(query)
def gen_pair(querylist, partial_order="full"): def gen_pair(querylist, partial_order="full"):
""" """
gen pair for pair-wise learning to rank algorithm gen pair for pair-wise learning to rank algorithm
...@@ -157,23 +162,29 @@ def gen_pair(querylist, partial_order="full"): ...@@ -157,23 +162,29 @@ def gen_pair(querylist, partial_order="full"):
for i, query_left in enumerate(querylist): for i, query_left in enumerate(querylist):
for j, query_right in enumerate(querylist): for j, query_right in enumerate(querylist):
if query_left.relevance_score > query_right.relevance_score: if query_left.relevance_score > query_right.relevance_score:
yield 1, np.array(query_left.feature_vector), np.array(query_right.feature_vector) yield 1, np.array(query_left.feature_vector), np.array(
query_right.feature_vector)
else: else:
yield 1, np.array(query_left.feature_vector), np.array(query_right.feature_vector) yield 1, np.array(query_left.feature_vector), np.array(
query_right.feature_vector)
elif partial_order == "neighbour": elif partial_order == "neighbour":
# C(n) # C(n)
k = 0 k = 0
while k < len(querylist)-1: while k < len(querylist) - 1:
query_left = querylist[k] query_left = querylist[k]
query_right = querylist[k+1] query_right = querylist[k + 1]
if query_left.relevance_score > query_right.relevance_score: if query_left.relevance_score > query_right.relevance_score:
yield 1, np.array(query_left.feature_vector), np.array(query_right.feature_vector) yield 1, np.array(query_left.feature_vector), np.array(
query_right.feature_vector)
else: else:
yield 1, np.array(query_left.feature_vector), np.array(query_right.feature_vector) yield 1, np.array(query_left.feature_vector), np.array(
query_right.feature_vector)
k += 1 k += 1
else: else:
raise ValueError("unsupport parameter of partial_order, Only can be neighbour or full") raise ValueError(
"unsupport parameter of partial_order, Only can be neighbour or full"
)
def gen_list(querylist): def gen_list(querylist):
...@@ -190,19 +201,17 @@ def gen_list(querylist): ...@@ -190,19 +201,17 @@ def gen_list(querylist):
""" """
if not isinstance(querylist, QueryList): if not isinstance(querylist, QueryList):
querylist = QueryList(querylist) querylist = QueryList(querylist)
querylist._correct_ranking_() # querylist._correct_ranking_()
relevance_score_list = [query.relevance_score for query in querylist] relevance_score_list = [query.relevance_score for query in querylist]
feature_vector_list = [query.feature_vector for query in querylist] feature_vector_list = [query.feature_vector for query in querylist]
# yield np.array(relevance_score_list).T, np.array(feature_vector_list) yield np.array(relevance_score_list).T, np.array(feature_vector_list)
for i in range(len(querylist)):
yield relevance_score_list[i], np.array(feature_vector_list[i])
def load_from_text(filepath, shuffle=True, fill_missing=-1): def load_from_text(filepath, shuffle=True, fill_missing=-1):
""" """
parse data file into querys parse data file into querys
""" """
prev_query_id = -1; prev_query_id = -1
querylists = [] querylists = []
querylist = None querylist = None
fn = __initialize_meta_info__() fn = __initialize_meta_info__()
...@@ -235,23 +244,27 @@ def __reader__(filepath, format="pairwise", shuffle=True, fill_missing=-1): ...@@ -235,23 +244,27 @@ def __reader__(filepath, format="pairwise", shuffle=True, fill_missing=-1):
label query_left, query_right # format = "pairwise" label query_left, query_right # format = "pairwise"
label querylist # format = "listwise" label querylist # format = "listwise"
""" """
querylists = load_from_text(filepath, shuffle=shuffle, fill_missing=fill_missing) querylists = load_from_text(
filepath, shuffle=shuffle, fill_missing=fill_missing)
for querylist in querylists: for querylist in querylists:
if format == "pairwise": if format == "pairwise":
for pair in gen_pair(querylist): for pair in gen_pair(querylist):
yield pair yield pair
elif format == "listwise": elif format == "listwise":
# yield next(gen_list(querylist)) yield next(gen_list(querylist))
for instance in gen_list(querylist):
yield instance
train = functools.partial(__reader__,filepath="MQ2007/MQ2007/Fold1/train.txt") train = functools.partial(__reader__, filepath="MQ2007/MQ2007/Fold1/train.txt")
test = functools.partial(__reader__, filepath="MQ2007/MQ2007/Fold1/test.txt") test = functools.partial(__reader__, filepath="MQ2007/MQ2007/Fold1/test.txt")
def fetch(): def fetch():
return download(URL, "MQ2007", MD5) return download(URL, "MQ2007", MD5)
if __name__ == "__main__": if __name__ == "__main__":
fetch() fetch()
for i, (score,
samples) in enumerate(train(
format="listwise", shuffle=False)):
np.savetxt("query_%d" % (i), score, fmt="%.2f")
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册