【问题标题】:IndexError: index n is out of bounds for axis 1 with size nIndexError:索引 n 超出轴 1 的范围,大小为 n
【发布时间】:2020-11-26 05:42:31
【问题描述】:

我有一个空矩阵M.shape:

(179, 179)

现在我想使用以下循环填充它:

for game in range(len(games)-1):

    df_round = df_games_position[df_games_position['rodada_id'] == games['rodada_id'][game]]
    players_home = df_round[df_round['time_id'] == games['time_id'][game]]
    players_away = df_round[df_round['time_id'] == games['adversario_id'][game]]
    
    count=0
    for j_home in range(len(players_home)):
        count_fora=0
        for j_away in range(len(players_away)):
            score_home = 0
            score_away = 0
            
            points_j_home = players_home['points_num'].iloc[j_home]
            points_j_away = players_away['points_num'].iloc[j_away]
            print ('POINTS HOME',points_j_home)
            print ('POINTS AWAY',points_j_away)
            
            soma =  points_j_home + points_j_away 
            if soma != 0:
                score_home = points_j_home / soma
                score_away = points_j_away / soma
                print ('SCORE HOME', score_home)
                print ('SCORE AWAY',score_away)
            
            j1 = players_home['Rank'].iloc[j_home].astype('int64')
            j2 = players_away['Rank'].iloc[j_away].astype('int64')
            print ('j1',j1)
            print ('j2',j2)
                
            M[j1,j1] = M[j1,j1] + games['goals_home_norm'][game] + score_home
            M[j1,j2] = M[j1,j2] + games['goals_away_norm'][game] + score_away
            M[j2,j1] = M[j2,j1] + games['goals_home_norm'][game] + score_home
            M[j2,j2] = M[j2,j2] + games['goals_away_norm'][game] + score_away
            
            print (M)
            
            count+=1
            print ('COUNT', count)

最后我得到了错误:

M[j1,j2] = M[j1,j2] + games['gols_fora_norm'][game] + score_home
IndexError: index 179 is out of bounds for axis 1 with size 179

我的最后一轮迭代打印:

COUNT 3
SCORE HOME 0.0
SCORE AWAY 0.0
j1 7
j2 162
[[0.         0.         0.         ... 0.         0.         0.        ]
 [0.         0.         0.         ... 0.         0.         0.        ]
 [0.         0.         0.         ... 0.         0.         0.        ]
 ...
 [0.         0.         0.         ... 0.         0.         0.        ]
 [0.         0.         0.         ... 0.         0.         0.        ]
 [0.         0.         0.         ... 0.         0.         8.57263145]]
COUNT 4
SCORE HOME 0.0
SCORE AWAY 0.0
j1 7
j2 179

我错过了什么?

【问题讨论】:

  • "IndexError: index 179 is out of bounds for axis 1 with size 179" 您认为应该在范围内的最小索引是多少?您认为应该在界限内的最大索引是多少?为什么?它是否适用于内置 Python 列表?它在 Javascript 中是这样工作的吗?它在您使用的其他语言中也能这样工作吗?

标签: python pandas matrix index-error


【解决方案1】:

矩阵是numpyarray,而index是从0而不是1开始

np.array([1,2,3,4]).shape
Out[29]: (4,)
np.array([1,2,3,4])[3]
Out[30]: 4

我们可以简单地修复它,创建形状为 (180,180) 的空 M

M = M[1:,1:]

【讨论】:

  • @8-BitBorges 简单修复,在分配 M= M[1:,1:] 后创建形状为 (180,180) 的 M
  • 我明白了。在分配行之后,您能否使用上面的代码编辑您的答案?
  • 我做到了,现在我收到错误IndexError: index 178 is out of bounds for axis 0 with size 178...
  • @8-BitBorges 因为你已经有了空的 M 180,180178 不应该超过 ...
猜你喜欢
  • 2015-08-06
  • 2021-05-16
  • 1970-01-01
  • 1970-01-01
  • 2018-04-10
  • 2018-05-09
  • 2020-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多