0

I have a set of values

y_train=["User","Bot","User","Bot",.......]
y_pred=["Bot","User","User","Bot".........]

I want to generate an array which returns 1 if the values of y_train[i] and t_pred[i] dont match.Both y_train and y_pred consist of same no of values

That is array indicator should be:

indicator=[1,1,0,0..........] 

I have tried

 indicator=0
 for i in range(len(y_train)):
      if y_train[i]!=y_pred[i]:
            indicator[i]=1
      else:
            indicator[i]=0

But error being shown is :

'int' object does not support item assignment

How could this be done? Thanks

0

4 Answers 4

3
indicator = map(lambda x,y: int(x != y), y_train, y_pred) 

As commented, if indicator serves as a boolean array, you may remove the int conversion to generate a True & False array:

indicator = map(lambda x,y: x != y, y_train, y_pred) 

or as suggested in the comment:

indicator =  map(operator.ne, y_train, y_pred) 
Sign up to request clarification or add additional context in comments.

4 Comments

int conversion excluded - the best answer.
If OP needs indicator values as True or False we may remove the conversion.
OP may be unaware of that option
operator.ne is the same as lambda x,y: x != y.
2

indicator = [t != p for t, p in zip(y_train, y_pred)]

2 Comments

Or just t==p, which is essentially integer in disguise
You don't need int, it just adds execution overhead. False and True are equivalent to 0 and 1. 1==True returns ... True and 5+True will evaluate to 6. Rule of the thumb - never convert when you don't have to (Python is not C and does not require strict explicit type castings)
1

You have declared indicator as an int. Try this:

indicator=[]
for i in range(len(y_train)):
  if y_train[i]!=y_pred[i]:
        indicator.append(1)
  else:
        indicator.append(0)

Another way to do this is:

indicator = []
for i,j in zip(y_train,y_pred):
    if i==j:
        indicator.append(0)
    else:
        indicator.append(1)

Comments

1

First, this is what you should do:

indicator = [1 if x != y else 0 for x, y in zip(y_train, y_pred)]
# or indicator = [int(x != y) for x, y in zip(y_train, y_pred)]

This is a list comprehension. It uses zip to go over the values of y_train and y_pred in parallel. For every pair of corresponding values in y_train and y_pred, indicator contains 1 if the values are unequal and 0 otherwise.

Now, here's what's wrong with your attempt. First, if you want indicator to be a list, don't make it an int:

#indicator=0
indicator = []

Second, you can't assign to indices past the end of a list. You can append the values instead:

for i in range(len(y_train)):
    if y_train[i]!=y_pred[i]:
#         indicator[i]=1
          indicator.append(1)
    else:
#         indicator[i]=0
          indicator.append(0)

Comments

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.