产生负样本

import random
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    fa = open('test.txt', 'r')
    line = fa.readline()
    testlist = []
    al=[]
    bl=[]
    timelist = []
    time_dist = dict()
    train_pos=[]
    while line:
        line=line.split('\n')
        a=int(line[0].split(' ')[0])
        b =int(line[0].split(' ')[1])
        c=line[0].split(' ')[3]
        if c in time_dist:
            time_dist[c] += 1
        else:
            timelist.append(c)
            time_dist[c] = 1
        seq=(line[0].split(' ')[0],line[0].split(' ')[1],line[0].split(' ')[3],line[0].split(' ')[2])
        new_line = ' '.join(seq)  # 将字符列表用','拼接成一个新字符串
        train_pos.append(new_line)
        print(new_line)
        al.append(a)
        bl.append(b)
        line = fa.readline()
    fa.close()
    print(max(al),max(bl))
    if max(al)>max(bl):
        max_n=max(al)
    else:
        max_n=max(bl)

    print(max_n)

    # ftrain=open('train_edge.txt', 'w')
    # for edge in train_pos:
    #     ftrain.write(edge)
    #     ftrain.write('\n')  #把label和time顺序调过来了
    # ftrain.close()

    trainlist = []
    for i in timelist:
        for j in range (int(time_dist[i])):
            right=random.randint(1,max_n)
            left = random.randint(1, max_n)
            pos1=[str(left),str(right),i,str(1)]
            pos2 = [str(right), str(left), i, str(1)]
            postr1=' '.join(pos1)
            postr2 = ' '.join(pos2)
            if postr1 in train_pos or postr2 in train_pos:
                right = right+1
            neg=[str(left),str(right),i,str(0)]
            trainlist.append(' '.join(neg))
            print(' '.join(neg))
    train_pos.extend(trainlist)
    ftrain = open('test_edge.txt', 'w')
    for edge in train_pos:
        ftrain.write(edge)
        ftrain.write('\n')  # 把label和time顺序调过来了
    ftrain.close()

你可能感兴趣的:(python)