一个python库,用于不同程度的排名(一种学习排名的方法)
fairsearchdeltr的Python项目详细描述
python的公平搜索deltr
这是实现DELTR模型以获得公平排名的python库。
安装
要安装fairsearchdeltr
,只需使用pip
(或pipenv
):
pip install fairsearchdeltr
就这样!
在代码中使用它
您需要先从包中导入类:
fromfairsearchdeltrimportDeltr
训练A型
您需要先对模型进行培训,然后才能对文档进行排序。
# import other helper librariesimportpandasaspdfromioimportStringIO# load some train data (this is just a sample - more is better)train_data_raw="""q_id,doc_id,gender,score,judgment 1,1,1,0.962650646167003,1 1,2,0,0.940172822166108,0.98 1,3,0,0.925288002880488,0.96 1,4,1,0.896143226020877,0.94 1,5,0,0.89180775633204,0.92 1,6,0,0.838704766545679,0.9 """train_data=pd.read_csv(StringIO(train_data_raw))# setup the DELTR objectprotected_feature="gender"# column name of the protected attribute (index after query and document id)gamma=1# value of the gamma parameternumber_of_iterations=10000# number of iterations the training should runstandardize=True# let's apply standardization to the features# create the Deltr objectdtr=Deltr(protected_feature,gamma,number_of_iterations,standardize=standardize)# train the modeldtr.train(train_data)>>array([0.02527054,0.07692437])# your run should have approximately same results
使用模型对
现在,您可以使用获得的模型对一些数据进行排序。
# load some test/prediction dataprediction_data_raw="""q_id,doc_id,gender,score 1,7,0,0.9645 1,8,0,0.9524 1,9,0,0.9285 1,10,0,0.8961 1,11,1,0.8911 1,12,1,0.8312 """prediction_data=pd.read_csv(StringIO(prediction_data_raw))# use the model to rank the data dtr.rank(prediction_data)>>doc_idgenderjudgement41110.07484951210.0637700700.0634861800.0612482900.05682831000.050836# the result will be a re-ranked dataframe
库中包含每个函数的足够的代码文档。
更深入地检查模型
您可以使用名为log
的特殊属性来检查模型的训练是如何进行的。
dtr.log>>[<TrainStep[1553844278383,[0.019264690.00976336],[[-0.00125304-0.0014605][-0.00125304-0.0014605][-0.00125304-0.0014605][-0.00125304-0.0014605][-0.00125304-0.0014605][-0.00125304-0.0014605]],5.999620187652397,0.0]>,...]
log
返回来自fairsearchdeltr.models.TrainStep
类的对象列表。类是训练每个步骤中参数的表示。
包含一个timestamp
、omega
、omega_gradient
、loss
、loss_standard
、loss_exposure
。
开发
- 克隆此存储库
git clone https://github.com/fair-search/fairsearchdeltr-python
- 将目录更改为克隆存储库的目录
cd WHERE_ITS_DOWNLOADED/fairsearchdeltr-python
- 使用任何IDE处理代码
测试
只需运行:
python setup.py test
学分
本文介绍了deltr算法:
- 梅克·泽利克、吉娜·特雷莎·迪恩、卡洛斯·卡斯蒂略。”Reducing Disparate Exposure in Ranking: A Learning to Rank Approach。“预印本arxiv:1805.08716(2018)。
这个图书馆是由Ivan Kitanovski在论文的基础上开发的。有关详细信息,请参见license文件。
如有任何问题,请联系Mieke Zehlike
另请参见
您还可以看到DELTR for ElasticSearch 以及DELTR Java library。