Home > OS >  Array Multiplication "IndexError: index 7 is out of bounds for axis 0 with size 7" Python
Array Multiplication "IndexError: index 7 is out of bounds for axis 0 with size 7" Python

Time:10-27

def mult(train, weight):
  mult = np.zeros((len(train), len(weight[0])))
  for i in range(len(train)):
    for j in range(len(weight[0])):
      for k in range(len(weight)):
        mult[i][j]  = train[i][k] * weight[k][j]
  return mult

So I'm trying to multiply two arrays by size train = (987, 7) and weight = (28, 4). But when i run it, i get an error: "IndexError: index 7 is out of bounds for axis 0 with size 7".

CodePudding user response:

  1. k is out of range, change to for k in range(train[0]):
  2. Since you are using NumPy, transform train and weight to np.ndarray. eg: mult[i, j], train[i, k], weight[k, j]
  3. Using matrix multiply is a compact method
    multi = np.matmul(train, weight[0:7, :])
  • Related