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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53260157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:09:13  来源:igfitidea点击:

single positional indexer is out-of-bounds index error

pythonpandasnumpytqdm

提问by G.alshammari

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)


error

错误

IndexError Traceback (most recent call last) in () 1 ----> 2 Similarities_Functions.Corated_Matrix() 3 Similarities_Functions.Corated_Similarity() 4 5 print('finished!!!')

~/Documents/PhD/IntelliSys19/journal/ColdStart_Experiment/Similarities_Functions.py in Corated_Matrix() 145 for u2 in users: 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, key) 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 the location -> 1830 self._is_valid_integer(key, axis) 1831 1832 return self._get_loc(key, axis=axis)

~/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in _is_valid_integer(self, key, axis) 1711 l = len(ax) 1712 if key >= l or key < -l: -> 1713 raise IndexError("single positional indexer is out-of-bounds") 1714 return True 1715

IndexError: single positional indexer is out-of-bounds

IndexError Traceback(最近一次调用最后一次) in () 1 ----> 2 Similarities_Functions.Corated_Matrix() 3 Similarities_Functions.Corated_Similarity() 4 5 print('finished!!!')

~/Documents/PhD/IntelliSys19/journal/ColdStart_Experiment/Similarities_Functions.py in Corated_Matrix() 145 for u2 in users: 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, key) 1371 1372 may_callable = com._apply_if_callable(key, self.obj) -> 1373 return self._getitem_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 # 验证位置 -> 1830 self._is_valid_integer(key, axis) 1831 1832 return self._get_loc(key,axis=axis)

~/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in _is_valid_integer(self, key, axis) 1711 l = len(ax) 1712 if key >= l or key < -l: - > 1713 引发 IndexError(“单个位置索引器越界”)1714 返回 True 1715

IndexError:单个位置索引器越界

回答by Fishbones78

IndexError: single positional indexer is out-of-bounds

I believe that this is simply saying that one of your iloc statements is looking up something that doesn't exist. If your DataFrame is 5 rows long, iloc[5, 0] would give out-of-bounds. This is because the last row would be iloc[4, 0], as it begins counting from 0.

我相信这只是说您的 iloc 语句之一正在查找不存在的东西。如果您的 DataFrame 有 5 行长,则 iloc[5, 0] 会超出范围。这是因为最后一行是 iloc[4, 0],因为它从 0 开始计数。