N
N
notxleb2021-06-24 19:20:49
Python
notxleb, 2021-06-24 19:20:49

How to make a matrix from boolean vectors in numpy from a matrix with vectors from int?

Suppose there is such a matrix indexes = [, ]. How to get matrix [, [0,0,1,1,1]]] without using for preferably.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2021-06-24
@dimonchik2013

google something like astype(bool)

D
dmshar, 2021-06-24
@dmshar

With all the clarifications, the most concise that I could come up with:

import numpy as np
import itertools
ind=np.array([, ])
M=4

lt=np.zeros((len(ind),1,M+1),dtype=int)
for a in range(len(ind)):
    lt[a,0,list(itertools.chain(*ind[a]))]+=1

print(lt)

Result:
[
 ]

If not for the strange size of the original array, which I wrote about above:
ind.shape
Out[15]: (2, 1, 3)

everything would be much easier.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question