生成全1特征矩阵

import torch
import random
import numpy as  np
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    fa = open('anot.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)
    ones = np.ones((max_n, 1000))
    number=np.arange(1, max_n+1)
    number = number[:, np.newaxis]
    fea=np.append(number, ones, axis=1)
    print(fea.shape)
    # 后缀改为 .txt 一样
    filename = 'feature.txt'
    # 写文件
    np.savetxt(filename, fea, fmt='%d', delimiter=' ')


生成全1特征矩阵_第1张图片

你可能感兴趣的:(矩阵,numpy,python)