PRank算法python实现

来源:互联网 发布:送餐员下载什么软件 编辑:程序博客网 时间:2024/06/05 23:44

算法流程

这里写图片描述

python实现
数据集:LETOR4.0

import numpy as nptrain=[]label=[]#读取文件for line in open("train.txt"):     lines=line.split(' ')    label.append([float(lines[0])+1,lines[1]])    data=[]    for i in range(2,48):        data.append(float(lines[i].split(':')[1]))    train.append(data)train = np.matrix(train)label = np.matrix(label)#预测def predict(w_x, b, k):    for i in range(1,k):        if b[i] > w_x:            return isamples, features = train.shapeclassify =3w = np.matrix(np.zeros(features))b = np.zeros(classify+1)b[classify] = np.inf#迭代次数for _ in range(100):    print 'iterator:',_    for i in range(samples):        x = train[i]        w_x = (w * x.T)[0, 0]        p_label = predict(w_x, b, classify+1)        r = np.zeros(classify)        y = np.zeros(classify)        p_true=float(label[i,0])        if p_label != p_true:            for j in range(1,classify):                if p_true <= j:                    y[j] = -1                else:                    y[j] = 1            for j in range(1,classify):                if (w_x - b[j]) * y[j] <= 0:                    r[j] = y[j]                else:                    r[j] = 0            #参数更新(更新率)            #w = w + 0.1*np.sum(r) * x            w = w + np.sum(r) * x            for j in range(1,classify):                b[j] = b[j] - r[j]count=0for i in range(samples):    x = train[i]    w_x = (w * x.T)[0, 0]    p_label = predict(w_x, b, classify+1)    if float(label[i,0])==p_label:        count=count+1       # print(label[i,0], p_label)print  count,samples,count*1.0/samples

评价指标
IR的评价指标-MAP,NDCG和MRR
待更新

0 0
原创粉丝点击