0
 for (i,j) in zip(Y_test,Y_pred_test):   
        if np.logical_and((Y_test[i]==1),(Y_pred_test[j]==1)):
            TP += 1                           
        elif np.logical_and((Y_test[i]==1),(Y_pred_test[j] == 0)):
            FN += 1                          
        elif np.logical_and((Y_test[i]==0),(Y_pred_test[j]==1)):
            FP += 1                           
        elif np.logical_and((Y_test[i]==0),(Y_pred_test[j]==0)):
            TN += 1 

Python - NumPy Question:

I need help. My code keeps coming up with an error on this particular section. The error states "IndexError: index 1 is out of bounds for axis 0 with size 1"
I'm currently writing up how to Calculate TP, FP, TN, FN, Accuracy, Precision, Recall, and F-1 score.

Y_test data contains:

[[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 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 1 1 1 1 1 1 1 1 1 1
  1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]]

Y_pred_test data contains:

[[0. 0. 0. 0. 0. 1. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 1.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 0. 1. 1. 0. 0. 0. 0. 0. 1. 0. 0.
  1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 1. 1. 1. 1. 1. 1. 1. 0. 1. 1.
  1. 0. 1. 1. 1. 1. 1. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
  1. 1. 1. 1. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
  1. 1. 1. 1. 1.]]
   

1 Answer 1

0

As Y_test and Y_pred_test are 2d arrays both i and j in the for loop are 1d arrays. Y_test[i] uses the values in i as the indices to access the rows in Y_test. Row zero exists but row 1 doesn't causing the error message.

import numpy as np
Y_test = np.array( [[ 0, 0, 0, 1, 1, 1, 1 ]] ) # Easy to see test data
Y_pred_test = np.array( [[ 0., 1., 1., 0., 0., 1. ]] ) 

for (i,j) in zip(Y_test,Y_pred_test):
     print(i,'\n', j) 

# [0 0 0 1 1 1 1]  # i in the loop 
# [0. 1. 1. 0. 0. 1.]   # j in the loop
Sign up to request clarification or add additional context in comments.

1 Comment

thank you! I was able to fix my code with your guidance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.