2 severnorth SeverNorth 于 2017.09.01 00:52 提问

'float' object cannot be interpreted as an integer

在Python代码里调用了zeros函数,按理说里面的dtype参数本来就应该是float啊。。不知道为什么会一直报错,代码如下:

def _load_representation(self):
'''
load user and item latent features generate by MF for every meta-graph
'''
#if dt in ['yelp-200k', 'amazon-200k', 'amazon-50k', 'amazon-100k', 'amazon-10k', 'amazon-5k', 'cikm-yelp', 'yelp-50k', 'yelp-10k', 'yelp-5k', 'yelp-100k', 'douban']:
fnum = self.N / 2
ufilename = self.data_dir + 'uids.txt'
bfilename = self.data_dir + 'bids.txt'
uids = [int(l.strip()) for l in open(ufilename, 'r').readlines()]
uid2reps = {k:np.zeros(fnum, dtype=np.float64) for k in uids}

    bids = [int(l.strip()) for l in open(bfilename, 'r').readlines()]
    bid2reps = {k:np.zeros(fnum, dtype=np.float64) for k in bids}

    ufiles, vfiles = self._generate_feature_files()

    feature_dir = self.data_dir + 'mf_features/path_count/'
    for find, filename in enumerate(ufiles):
        ufs = np.loadtxt(feature_dir + filename, dtype=np.float64)
        cur = find * self.F
        for uf in ufs:
            uid = int(uf[0])
            f = uf[1:]
            uid2reps[uid][cur:cur+self.F] = f

    for find, filename in enumerate(vfiles):
        bfs = np.loadtxt(feature_dir + filename, dtype=np.float64)
        cur = find * self.F
        for bf in bfs:
            bid = int(bf[0])
            f = bf[1:]
            bid2reps[bid][cur:cur+self.F] = f
    logging.info('load all representations, len(ufiles)=%s, len(vfiles)=%s, ufiles=%s, vfiles=%s', len(ufiles), len(vfiles), '|'.join(ufiles), '|'.join(vfiles))
    return uid2reps, bid2reps

应该是里面这两句有问题:

uids = [int(l.strip()) for l in open(ufilename, 'r').readlines()]
uid2reps = {k:np.zeros(fnum, dtype=np.float64) for k in uids}

前面有导入numpy“import numpy as np”,求大神指点到底是哪里错了。。一直提示“TypeError: 'float' object cannot be interpreted as an integer”。。

Csdn user default icon
上传中...
上传图片
插入图片