单位置索引器是越界索引错误

user_ratings_matrix = training_df.pivot(index='userId', columns='movieId', values='rating')
users = user_ratings_matrix.index.values 

print('Creating corated data frame...')
with open('similarities/UsersCorated.csv', 'w') as result_file:
    print('user1,user2,corated', file=result_file)

    print('Calculating corated between users...')
    for u1 in tqdm(users, total=len(users)):
        for u2 in users:

            movies_u1 = ~np.isnan(user_ratings_matrix.iloc[u1 - 1])
            movies_u2 = ~np.isnan(user_ratings_matrix.iloc[u2 - 1])

            same_movies = np.logical_and(movies_u1, movies_u2)
            num_same_movies = list(same_movies).count(True)
        print(f"{u1},{u2},{num_same_movies}", file=result_file)

错误

IndexError Traceback(最近一次调用last)in()1 ----> 2 Similarities_Functions.Corated_Matrix()3 Similarities_Functions.Corated_Similarity()4 5 print('finished !!!')〜/ Documents / PhD / IntelliSys19 / journal Corated_Matrix()中的/ColdStart_Experiment/Similarities_Functions.py 145用户中的u2:146 ratings1 = np.nan_to_num(np.array(user_ratings_matrix.iloc [u1 - 1] .values)) - > 147 ratings2 = np.nan_to_num(np .array(user_ratings_matrix.iloc [u2 - 1] .values))148 149 sim = pearsonr(ratings1,ratings2)〜/ anaconda3 / lib / python3.6 / site-packages / pandas / core / indexing.py in getitem(self ,键)1371 1372 maybe_callable = com._apply_if_callable(key,self.obj) - > 1373 return self._getitem_axis(maybe_callable,axis = axis)1374 1375 def _is_scalar_access(self,key):〜/ anaconda3 / lib / python3.6 _site-packages/pandas/core/indexing.py in _getitem_axis(self,key,axis)1828 1829#validate location - > 1830 self._is_valid_integer(key,axis)1831 1832 return self._get_loc(key,axis = axis )〜/ anaconda3 / lib / p _is_valid_integer中的ython3.6 / site-packages / pandas / core / indexing.py(self,key,axis)1711 l = len(ax)1712如果key> = l或key <-l: - > 1713引发IndexError(“单个位置索引器超出界限“)1714返回True 1715 IndexError:单个位置索引器超出范围

回答(1)

2 years ago

IndexError: single positional indexer is out-of-bounds

我相信这只是说你的一个iloc语句正在查找不存在的东西 . 如果你的DataFrame是5行长,iloc [5,0]会给出越界 . 这是因为最后一行是iloc [4,0],因为它从0开始计数 .