2

I have a nested array with some values. I have another array, where the length of both arrays are equal. I'd like to get an output, where I have a nested array of 1's and 0's, such that it is 1 where the value in the second array was equal to the value in that nested array.

I've taken a look on existing stack overflow questions but have been unable to construct an answer.

masks_list = []
for i in range(len(y_pred)):
    mask = (y_pred[i] == y_test.values[i]) * 1
    masks_list.append(mask)
masks = np.array(masks_list);

Essentially, that's the code I currently have and it works, but I think that it's probably not the most effecient way of doing it.

YPRED:
[[4 0 1 2 3 5 6]
 [0 1 2 3 5 6 4]]

YTEST:
8    1
5    4

Masks:
[[0 0 1 0 0 0 0]
 [0 0 0 0 0 0 1]]
5
  • Could you give some test array and inner test array? If it's work it gonna be a good solution. I think you can done it also with map. Commented Apr 3, 2019 at 6:23
  • Post a sample of you desired I/O? Commented Apr 3, 2019 at 6:31
  • Added some example I/O. Commented Apr 3, 2019 at 7:50
  • So by 'nested array' you mean a 2d array (in your example shape (2,7)). Commented Apr 3, 2019 at 16:28
  • Yes. That's what I mean. Commented Apr 4, 2019 at 7:25

2 Answers 2

1

Another good solution with less line of code.

a = set(y_pred).intersection(y_test)
f = [1 if i in a else 0 for i, j in enumerate(y_pred)]

After that you can check performance like in this answer as follow:

import time
from time import perf_counter as pc

t0=pc()    
a = set(y_pred).intersection(y_test)
f = [1 if i in a else 0 for i, j in enumerate(y_pred)]
t1 = pc() - t0

t0=pc()
for i in range(len(y_pred)):
    mask = (y_pred[i] == y_test[i]) * 1
    masks_list.append(mask)
t2 = pc() - t0

val = t1 - t2

Generally it means if value is positive than the first solution are slower. If you have np.array instead of list you can try do as described in this answer:

type(y_pred)
>> numpy.ndarray
y_pred = y_pred.tolist()
type(y_pred)
>> list
Sign up to request clarification or add additional context in comments.

4 Comments

np.array is unhashable.
Is this comment according to my answer or to @Jaja ?
Your comment, at this line: a = set(y_pred).intersection(y_test)
I understand that you have got np.array instead of list. Could you try stackoverflow.com/a/1966210/4510954
0

Idea(least loop): compare array and nested array:

masks = np.equal(y_pred, y_test.values)

you can look at this too:

np.array_equal(A,B)  # test if same shape, same elements values
np.array_equiv(A,B)  # test if broadcastable shape, same elements values
np.allclose(A,B,...) # test if same shape, elements have close enough values

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.