pin_configuration = [[1, 1], [2, 2], [3, 3], [4, 4], [...]]
bool_list = [[False] * 68] * 68
for m in range(0, 68):
for b in range(0, len(pin_configuration[m][1:])):
position = pin_configuration[m][b]
bool_list[m][position] = True
Now I have a output like this:
bool_list = [[True, False, False,False,...], [True, False, False,
False,...], [True, False, False, False,...], [...]
But i want this:
bool_list = [[True, False, False,False,...], [False, True, False,
False,...], [False, False, True, False,...], [...]
I just want to set True in bool_list there where the value pin_configuration[x][1] is
How I can do this ?
You initialize bool_list in wrong way - instead of making 68 separate lists (rows), you make 68 references to the same list.
bool_list = [[False] * 68 for i in range(68)]
will do what you want.
Here's a way to get the desired list. Just set n back to 68 once you're sure it works fine.
You need to create n distinct lists first, and you can then iterate over the pin_configuration coordinates to set the desired elements to True.
Note that the first element in Python lists has index 0, not 1:
pin_configuration = [[1, 1], [2, 2], [3, 3], [4, 4]]
n = 5
bool_list = [[False] * n for i in range(n)]
for i,j in pin_configuration:
bool_list[i-1][j-1] = True
print(bool_list)
#[[True, False, False, False, False], [False, True, False, False, False], [False, False, True, False, False], [False, False, False, True, False], [False, False, False, False, False]]
Related
I am trying to create an array of boolean masks from an array of breakpoint-pairs. So the result should be boolean masks of length n with true values in between the two breakpoints. I could solve the problem iteratively by writing a for loop but I want to find out the vectorized numpy equivalent for it.
mask = np.array([[False, False, False, False, False],
[False, False, False, False, False]])
breakpoints = np.array([[1, 3],
[2, 4]])
for i, bp in enumerate(breakpoints):
mask[i, bp[0]:bp[1]] = True
Output:
array([[False, True, True, False, False],
[False, False, True, True, False]])
Optimally, I would like to solve this with indexing and array operations in numpy but I can't get my head around the correct way of doing it.
I hope this example is clear and thank you for any help!
You can use the following trick:
>>> breakpoints = np.array([[1, 3],
... [2, 4]])
>>> output_width = 5
>>> idx = np.arange(output_width)
>>> (breakpoints[:,[0]] <= idx) & (idx < breakpoints[:,[1]])
array([[False, True, True, False, False],
[False, False, True, True, False]])
I have an array like this:
arrayElements = [[1, 4, 6],[2, 4, 6],[3, 5, 6],...,[2, 5, 6]]
I need to know, for example, the indices where an arrayElements is equal to 1.
Right now, I am doing:
rows, columns = np.where(arrayElements == 1)
This works, but I am doing this in a loop that loops through all possible element values, in my case, it's 1-500,000+. This is taking 30-40 minutes to run depending how big my array is. Can anyone suggest a better way of going about this? (Additional information is that I don't care about the column that the value is in, just the row, not sure if that's useful.)
Edit: I need to know the value of every element separately. That is, I need the values of rows for each value that elements contains.
So you are generating thousands of arrays like this:
In [271]: [(i,np.where(arr==i)[0]) for i in range(1,7)]
Out[271]:
[(1, array([0])),
(2, array([1, 3])),
(3, array([2])),
(4, array([0, 1])),
(5, array([2, 3])),
(6, array([0, 1, 2, 3]))]
I could do the == test for all values at once with a bit of broadcasting:
In [281]: arr==np.arange(1,7)[:,None,None]
Out[281]:
array([[[ True, False, False],
[False, False, False],
[False, False, False],
[False, False, False]],
[[False, False, False],
[ True, False, False],
[False, False, False],
[ True, False, False]],
[[False, False, False],
[False, False, False],
[ True, False, False],
[False, False, False]],
[[False, True, False],
[False, True, False],
[False, False, False],
[False, False, False]],
[[False, False, False],
[False, False, False],
[False, True, False],
[False, True, False]],
[[False, False, True],
[False, False, True],
[False, False, True],
[False, False, True]]])
and since you only care about rows, apply an any:
In [282]: (arr==np.arange(1,7)[:,None,None]).any(axis=2)
Out[282]:
array([[ True, False, False, False],
[False, True, False, True],
[False, False, True, False],
[ True, True, False, False],
[False, False, True, True],
[ True, True, True, True]])
The where on this is the same values as in Out[271], but grouped differently:
In [283]: np.where((arr==np.arange(1,7)[:,None,None]).any(axis=2))
Out[283]:
(array([0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5]),
array([0, 1, 3, 2, 0, 1, 2, 3, 0, 1, 2, 3]))
It can be split up with:
In [284]: from collections import defaultdict
In [285]: dd = defaultdict(list)
In [287]: for i,j in zip(*Out[283]): dd[i].append(j)
In [288]: dd
Out[288]:
defaultdict(list,
{0: [0], 1: [1, 3], 2: [2], 3: [0, 1], 4: [2, 3], 5: [0, 1, 2, 3]})
This 2nd approach may be faster for some arrays, though it may not scale well to your full problem.
By using np.isin (see documentation), you can test for multiple element values.
For example:
import numpy as np
a = np.array([1,2,3,4])
check_for = np.array([1,2])
locs = np.isin(a, check_for)
# [True, True, False, False]
np.where(locs)
#[0, 1]
Note: This assumes that you do not need to know the indices for every element value separately.
In the case that you need to track every element value separately, use a default dictionary and iterate through the matrix.
from collections import defaultdict
tracker = defaultdict(set)
for (row, column), value in np.ndenumerate(arrayElements):
tracker[value].add(row)
You could try looping over the values and indices using numpy.ndenumerate and using Counter, defaultdict, or dict where the keys are the values in the array.
I have the following numpy array:
a = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
I can use extended slicing to select e.g. columns:
>>> a[:,0::2]
array([[1, 3],
[4, 6],
[7, 9]])
>>> a[:,1::2]
array([[2],
[5],
[8]])
But I want to produce the following:
array([[True, False, True],
[True, False, True],
[True, False, True]])
array([[False, True, False],
[False, True, False],
[False, True, False]])
import numpy as np
bools = np.array([[False, False, False],
[False, False, False],
[False, False, False]])
bools[:, 0::2] = True
print(bools)
Output:
[[ True False True]
[ True False True]
[ True False True]]
np.array([[True if y%2==0 else False for y,z in enumerate(x)] for x in bools])
np.array([[False if y%2==0 else True for y,z in enumerate(x)] for x in bools])
Explanation:
By using list comprehension, variable 'x' iterates through each row of a. The inner list comprehension iterates through each of this('x' from outer comprehension) list elements. It can be observed that in your output, True & False values depend on index of the elements rather than the element values. Hence by using enumerate(), we get the index of each element in 'y' & value in 'z'. And using conditions on 'y', we decide on replacing with True or False
for example
a = np.array(([0, 0], [0, 1], [1, 0], [1, 1]))
c = np.array(a == [0, 1])
in this way I just get
`array([[ True, False],
[ True, True],
[False, False],
[False, True]])`
but I want to get
array([False, True, False, False])
of course I can ravel c and use a if(c[i]==1)&(c[i+1]==1) to give "True",
c = c.ravel()
cshape = list(c.shape)
del cshape[-1]
d = []
for i in range(0, len(c), 2):
if (c[i]==1)&(c[i + 1]==1):
d.append(True)
else:
d.append(False)
d = np.array(d).reshape(cshape)
but for a large system, it could be a cost of resources.
is there any simple method to do it?
In [1]: a = np.array(([0, 0], [0, 1], [1, 0], [1, 1]))
In [2]: a==np.array([0,1])
Out[2]:
array([[ True, False],
[ True, True],
[False, False],
[False, True]])
Just check whether all elements of a row are True:
In [3]: _.all(axis=1)
Out[3]: array([False, True, False, False])
I have a bi-dimensional np.array like
x = np.array([[1,2], [4,5], [4,6], [5,4], [4,5]])
now I want the indices where x is equal to [4,5] (-> [1, 4]). The operator == works in a different way:
x == [4,5]
array([[False, False],
[ True, True],
[ True, False],
[False, False],
[ True, True]], dtype=bool)
but I want something like [False, True, False, False, True]. Is it ok to do an and?
Usually the array is very big and I have to do it a lot of times, so I need a very fast way.
this should be the numpy-way:
x = np.array([[1,2], [4,5], [4,6], [5,4], [4,5]])
(x == [4,5]).all(1)
#out: array([False, True, False, False, True], dtype=bool)
No prior experience with numpy, but this works for a standard array:
x = [[1, 2], [4, 5], [4, 6], [5, 4], [4, 5]]
indices = [i for i, v in enumerate(x) if v == [4, 5]]
# gives [1, 4]
matches = [v == [4, 5] for v in x]
# gives [False, True, False, False, True]