arash askari
arash askari

Reputation: 19

only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices problem

when running this code I have an error

IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

python code

import random

def getsys():
    row = ''
    for i in range(0 , 8):
        randintt = str(random.randint(0 , 4))
        row += randintt
    return row


def getx():
    x = []
    for i in range(0,14):
        mysys = getsys()
        x.append(mysys)

    return x 

y = getx()
print (y)

import initialsys
import numpy as np

R = np.array([[0.90 , 0.93,0.91 , 0.95],
               [0.95 , 0.94, 0.93, 0],
               [0.85 , 0.90 , 0.87 , 0.92],
               [0.83 , 0.87 , 0.85 , 0 ],
               [0.94 , 0.93 , 0.95 , 0],
               [0.99 , 0.98 , 0.97 , 0.96],
               [0.91 , 0.92 , 0.94 , 0],
               [0.81 , 0.90 , 0.91 , 0],
               [0.97 , 0.99 , 0.96 , 0.91],
               [0.83 , 0.85 , 0.90 , 0],
               [0.94 , 0.95 , 0.96 , 0],
               [0.79 , 0.82 , 0.85 , 0.90],
               [0.98 , 0.99 , 0.97 , 0],
               [0.85 , 0.92 , 0.95 , 0.99]
              ])

def expression(r ,possition , char ):
    exp = 1-r[possition , char]


x = initialsys.getx()
possition = 1
Total = 1
char = ""
for row in x :
    for char in row :
        if char!= 0 :
            exp = expression(R , possition , char)
            Total = Total*exp
    Total = 1-Total
    possition = possition + 1

Upvotes: 0

Views: 12582

Answers (2)

AeboGtx
AeboGtx

Reputation: 56

For people who are getting this error while doing machine learning coding using numpy. When you are trying to print out the prediction classes[d["Y_prediction_test"][0,index]] you will be getting the same error. Please note that d["Y_prediction_test"][0,index] return a float like 1.0. so you should convert it to int

plt.show()
val = d["Y_prediction_test"][0,index]
val = int(val)
print(classes[val])

I have looked for this answer. But was not able to find it. It took some time to solve this, I hope this will help you. I'm self-learning ML/AI. So if we are in the same team, let's connect and help each. Thanks.

Upvotes: 4

Fredrik
Fredrik

Reputation: 451

You have a number of problems in your code but to fix your current error you need to index the array with integers as said in the error and not a char, you could do int(char) and then return the result.

def expression(r, possition , char):
   return 1-r[possition, int(char)]

Upvotes: 0

Related Questions