Math Project  IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
I am working a project right now where I'm using Colley ranking to rank fantasy football teams. I keep running into an error with my code when I get to
c[(t11)][(t11)] = c[(t11)][(t11)] + 1 # Updating diagonal element
"IndexError: only integers, slices (:
), ellipsis (...
), numpy.newaxis (None
) and integer or boolean arrays are valid indices"
Don't have a programming background, this is for a math class and I am using some public code from Github. Thanks! Link to Files:
https://wetransfer.com/downloads/659cfb94be62b8173b2dfa5df29754be20211123020816/506f33
import numpy as np
import pandas as pd
# Reading 'teams' and 'scores' data
teams = pd.read_csv('teams.txt', header = None)
num_of_teams = len(teams.index)
data = pd.read_csv('scores.txt', header = None)
# Initializing Colley Matrix 'c'and vector 'b'
c = np.zeros([num_of_teams, num_of_teams])
b = np.zeros(num_of_teams)
# Iterating through rows and populating Colley matrix values
for index, row in data.iterrows():
t1 = row[2]
t2 = row[5]
c[(t11)][(t11)] = c[(t11)][(t11)] + 1 # Updating diagonal element
c[(t21)][(t21)] = c[(t21)][(t21)] + 1 # Updating diagonal element
c[(t11)][(t21)] = c[(t11)][(t21)]  1 # Updating off  diagonal element
c[(t21)][(t11)] = c[(t21)][(t11)]  1 # Updating off  diagonal element
# Updating vecotr b based on result of each game
if row[4] > row[7]:
b[(t11)] += 1
b[(t21)] = 1
elif row[4] < row[7]:
b[(t11)] = 1
b[(t21)] += 1
# Adding 2 to diagonal elements (total number of games) of Colley matrix
diag = c.diagonal() + 2
np.fill_diagonal(c, diag)
# Dividing by 2 and adding one to vector b
for i, value in enumerate(b):
b[i] = b[i] / 2
b[i] += 1
# Solving N variable linear equation
r = np.linalg.solve(c, b)
# Displaying ranking for top 4 teams
top_teams = r.argsort()[4:][::1]
for i in top_teams:
print (str(r[i]) + " " + str(teams.iloc[i][1]))
1 answer

When you use
iterrows
(in this case) the data is converted tofloat
, and you can not index using afloat
Convert the
float
columns you use to index toint
like sofor index, row in data.iterrows(): t1 = int(row[2]) t2 = int(row[5])
Complete Code
import numpy as np import pandas as pd # Reading 'teams' and 'scores' data teams = pd.read_csv("teams.txt", header=None) num_of_teams = len(teams.index) data = pd.read_csv("scores.txt", header=None) # Initializing Colley Matrix 'c'and vector 'b' c = np.zeros([num_of_teams, num_of_teams]) b = np.zeros(num_of_teams) # Iterating through rows and populating Colley matrix values for index, row in data.iterrows(): t1 = int(row[2]) t2 = int(row[5]) c[(t1  1)][(t1  1)] = c[(t1  1)][(t1  1)] + 1 # Updating diagonal element c[(t2  1)][(t2  1)] = c[(t2  1)][(t2  1)] + 1 # Updating diagonal element c[(t1  1)][(t2  1)] = c[(t1  1)][(t2  1)]  1 # Updating off  diagonal element c[(t2  1)][(t1  1)] = c[(t2  1)][(t1  1)]  1 # Updating off  diagonal element # Updating vecotr b based on result of each game if row[4] > row[7]: b[(t1  1)] += 1 b[(t2  1)] = 1 elif row[4] < row[7]: b[(t1  1)] = 1 b[(t2  1)] += 1 # Adding 2 to diagonal elements (total number of games) of Colley matrix diag = c.diagonal() + 2 np.fill_diagonal(c, diag) # Dividing by 2 and adding one to vector b for i, value in enumerate(b): b[i] = b[i] / 2 b[i] += 1 # Solving N variable linear equation r = np.linalg.solve(c, b) # Displaying ranking for top 4 teams top_teams = r.argsort()[4:][::1] for i in top_teams: print(str(r[i]) + " " + str(teams.iloc[i][1]))
Output
0.6250000000000001 Freakin 0.625 Mahomes is where ma <3 is 0.625 Then He Waddled Away 0.625 Big Benches
Another alternative is to use
itertuples
instead ofiterrows
for row in data.itertuples(): t1 = row._3 t2 = row._6 c[(t1  1)][(t1  1)] = c[(t1  1)][(t1  1)] + 1 # Updating diagonal element c[(t2  1)][(t2  1)] = c[(t2  1)][(t2  1)] + 1 # Updating diagonal element c[(t1  1)][(t2  1)] = c[(t1  1)][(t2  1)]  1 # Updating off  diagonal element c[(t2  1)][(t1  1)] = c[(t2  1)][(t1  1)]  1 # Updating off  diagonal element # Updating vecotr b based on result of each game if row._5 > row._8: b[(t1  1)] += 1 b[(t2  1)] = 1 elif row._5 < row._8: b[(t1  1)] = 1 b[(t2  1)] += 1
Note how the values are now accessed as
row._3
instead ofrow[2]
androw._6
instead ofrow[5]
and so on.itertuples
maintains the types and there is no need to do a conversion toint
. The rest of the code is the same and you get the same output.
do you know?
how many words do you know