Related
I am trying to train agents in a multi-agent grid world using q-learning. I am using the existing environment 'collection-game': https://reposhub.com/python/deep-learning/ArnaudFickinger-gym-multigrid.html#articleHeader2. My problem is the observation/state-parameter that the environment algorithm returns. I want to use this parameter to choose the best policy given the state the agent is currently in. An example of this is shown below. Here the state parameter returns an integer that can be used to index the q-table.
Example of using the state to pick policy
The environment that I am using however returns an array of arrays that shows the observations for all agents in the environment:
[array([[[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 6, 0, 0, 0, 0, 0],
[10, 3, 0, 0, 3, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0]],
[[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 6, 0, 0, 0, 0, 0],
[10, 2, 0, 0, 2, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0]],
[[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0]],
[[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[10, 1, 0, 0, 0, 1]],
[[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0]],
[[ 2, 5, 0, 0, 0, 0],
[ 2, 5, 0, 0, 0, 0],
[ 2, 5, 0, 0, 0, 0],
[ 2,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0],
[ 0, 0, 0, 0, 0, 0],
[ 2, 5, 0, 0, 0, 0],
[ 6, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0]],
[[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 2, 5, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 6, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0]]], dtype=uint8)]
My question is: how to interpret this variable, and how to use it to pick the best policy for each agent.
This question already has answers here:
JSON dumps custom formatting
(3 answers)
Closed 1 year ago.
I'm currently making a level editor for a game I made using Pygame. I save the world data in a json file, which contains the different blocks for each column in every row. Trough my level editor, I update the json file and it works, but the indent is messed up so it's unreadable for humans. The data for every level consists out of one list containing many other lists and the line is split after every list. Thanks for helping me!
Before dumping (the way I want it to look after updating)
{
"level_1_data": [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 1],
[1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 2, 2, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 7, 0, 5, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 1],
[1, 7, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0,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
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
0,
0,
2,
2,
2,
2,
0,
0,
0,
0,
0,
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
2,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
],
[
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1
]
]
}
This is the code I have so far:
with open("./world_data.json", "r") as f:
data = json.load(f)
new_data = data[world.level_name] #Loading level_1_data or level_2_data
new_data[button[1][3][1]][button[1][3][0]] = 0 #Finding the right index in the 2 layered list
data[world.level_name] = new_data
data.update(data)
with open("./world_data.json", "w") as f:
json.dump(data, f, indent=4)
Try using pprint, it will get you alot closer to what you want.
import pprint
with open("world_data.json", "w") as f:
pprint.pprint(data, stream = f)
I need to solve a 2 by 2 array with 4 unknown
A B
C D
I know all horizontal sum A+B=11, C+D=7
I know all vertical sum A+C=10, B+D=8
I know all diagonal sum A+D=15, B+C=3
I then use Python to solve for A,B,C,D
import numpy as np
A = [[1, 1, 1, 1],
[1, 0, 0, 1],
[1, 0, 1, 0],
[0, 0, 1, 1]]
a = [18, 15, 10, 7]
answera = np.linalg.solve(A, a)
print(answera)
And the answer is [9. 2. 1. 6.] which is correct
Now I need to solve 4 by 4 array with 16 unknown
A B C D
E F G H
I J K L
M N O P
I know horizontal sum A+B+C+D=10, E+F+G+H=26, I+J+K+L=42, M+N+O+P=58
I know vertical sum A+E+I+M=28, B+F+J+N=32, C+G+K+O=36, D+H+L+P=40
I know diagonal sum M=13, I+N=23, E+J+O=30, A+F+K+P=34, B+G+L=21, C+H=11, D=4
The other diagonal sum A=1, B+E=7, C+F+I=18, D+G+J+M=34, H+K+N=33, L+O=27, P=16
Which mean I know the value of the 4 corners.
I tried the following code but did not work
C = [[1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
c = [10, 26, 42, 58, 7, 21, 39, 33, 27, 11, 23, 35, 30, 23, 32, 136]
answerc = np.linalg.solve(C, c)
print(answerc)
The correct answer should be [1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.] but I got error message
Traceback (most recent call last):
answerc = np.linalg.solve(C, c)
r = gufunc(a, b, signature=signature, extobj=extobj)
raise LinAlgError("Singular matrix")
numpy.linalg.LinAlgError: Singular matrix
Am I in the right direction? I will need to solve 5X5 with 25 unknown, 6X6 with 36 unknown and so on. Is there an easier way?
-----------------------------------------------------------------------------
Following Mr. Rory Daulton solution, I can solve the above 1 to 16 4X4 array without problem, but when I use it in another array with negative number, it doesn't give answer as expected;
The negative 4X4 array as follow
-20 -10 -5 0
-10 -20 -10 -5
-5 0 -10 -20
-10 -20 -10 -5
My python code as follow
import numpy as np
G = [[1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # horizontal rows
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0], # vertical columns
[0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], # forward diagonals
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # back diagonals
[0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
]
g = [-35, -45, -35, -45, # horizontal rows
-45, -50, -35, -30, # vertical columns
-10, -25, -20, -55, -40, -10, 0, # forward diagonals
-20, -20, -30, -20, -35, -30, -5, # back diagonals
]
answerg = np.linalg.lstsq(G, g, rcond=None)
print(answerg[0])
The output is not exactly the original array
[-2.00000000e+01 -1.31250000e+01 -1.87500000e+00 8.88178420e-15
-6.87500000e+00 -2.00000000e+01 -1.00000000e+01 -8.12500000e+00
-8.12500000e+00 2.13162821e-14 -1.00000000e+01 -1.68750000e+01
-1.00000000e+01 -1.68750000e+01 -1.31250000e+01 -5.00000000e+00]
What should I try? Thank you in advance.
SHORT ANSWER: There are infinitely many solutions to your problem. So this takes a more complex analysis of the equations.
LONG ANSWER: You have multiple problems with your code.
First, you make it easy to make mistakes, since the lines of your matrix do not correspond to the data that you present. Worse, you have no comments to explain things. This mis-match will probably cause errors. You have 22 pieces of data in your sums, so use them. You tried to combine some of the sums and ignore others (the four corners) but you did not do it properly and you ended up with a singular matrix.
Next, you use linalg.solve. In your problem you have more data items (22) than unknowns (16), so solve is inappropriate. The numpy documentation for solve states
a must be square and of full-rank, i.e., all rows (or, equivalently,
columns) must be linearly independent; if either is not true, use
lstsq for the least-squares best “solution” of the system/equation.
The matrix resulting from your data is not square, therefore the rows are not linearly independent, so you should use lstsq rather than solve. The lstsq routine gives more information than you need for your problem, so just print the first item in the resulting list.
Combining those ideas and adding a few comments gives this code:
import numpy as np
C = [[1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # horizontal rows
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0], # vertical columns
[0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], # forward diagonals
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # back diagonals
[0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
]
c = [10, 26, 42, 58, # horizontal rows
28, 32, 36, 40, # vertical columns
13, 23, 30, 34, 21, 11, 4, # forward diagonals
1, 7, 18, 34, 33, 27, 16, # back diagonals
]
answerc = np.linalg.lstsq(C, c, rcond=None)
print(answerc[0])
The printout is what you want:
[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.]
However, to be honest, there is no guarantee that this is an answer--just that it is a "closest" answer. Also, if it is an answer, there may be other answers. And, in fact, further analysis shows that there are other answers that satisfy all your conditions.
The sympy module can generate a row reduced echelon form of the matrix, which can be used to do more in-depth analysis of all the answers. However, the constants are then to be part of the matrix, rather than used as a separate array. Here is code for sympy to attempt to solve your problem:
import sympy
C = [[1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10], # horizontal rows
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 26],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 42],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 58],
[1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 28], # vertical columns
[0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 32],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 36],
[0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 40],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 13], # for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 0, 6],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1, 0, -7],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, -6],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 10],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 11],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 27],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 13],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 29],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 16],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]), (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15))
If you know how to analyze this you will see that there are infinitely many answers to your problem. If you set the number in the 15th cell to 15+x then the above matrix shows that the answer to all your restrictions is
1 2+x 3-x 4
5-x 6 7 8+x
9+x 10 11 12-x
13 14-x 15+x 16
The solve function of numpy only works if there is just one solution, so even if you had adjusted your matrix differently it would not have worked for you.
ANSWER TO YOUR UPDATE:
It seems that you missed the point of my answer. Your 4x4 problem has infinitely many answers, so there is no procedure that can choose the particular answer that you have in mind. The np.linalg.lstsq routine can find one of the answers to your problem but probably will not find your desired answer. You should consider it to be a coincidence that using that routine in your first problem gave your desired answer--that will probably not work in other problems.
It is a little hard to interpret the given answer to your new problem, since the scientific notation is hard to read. But all those matrix values are exact, and here they are as rational numbers in a particular format that should be obvious:
-20 -10-(3+1/8) - 5+(3+1/8) 0
-10+(3+1/8) -20 -10 - 5-(3+1/8)
- 5-(3+1/8) 0 -10 -20+(3+1/8)
-10 -20+(3+1/8) -10-(3+1/8) - 5
You see that the numpy's answer is the one that you expected, with the value 3+1/8 added to or subtracted from half the array values. This makes x=3+1/8 in the general answer that I gave you for your first problem.
This is as good as you can expect. Numpy gave you a correct answer--it has no idea how to choose the answer that you had in your head from the infinitely many correct answers to your problem. The only way to get just one answer is to change your problem--perhaps state the value in the first row and second column, or the sum of the first and third values in any one of the rows, or something similar.
Let's import numpy first,
import numpy as np
For example, I have a matrix A as,
A = np.identity(10)
I have two other matrices as,
B = np.random.sample((4, 4))
C = np.random.sample((6, 6))
In addition, I have two lists of indices as,
idx_1 = [1, 2, 4, 7]
idx_2 = [0, 3, 5, 6, 8, 9]
Now I want to replace idx_1 rows and columns of A by B and idx_2 rows and columns of A by C. The final A matrix will be a block diagonal matrix.
What is the efficient way to achieve this?
I tried as follows but I did not change A, I don't know why, but I did not get any error as well.
A[idx_1][:,idx_1] = B
In [99]: A = np.identity(10).astype(int)
In [100]: A
Out[100]:
array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]])
In [101]: idx_1 = [1, 2, 4, 7]
I can select a set of diagonal values with:
In [102]: A[idx_1, idx_1]
Out[102]: array([1, 1, 1, 1])
In [103]: A[idx_1, idx_1] = [10,20,30,40]
In [104]: A
Out[104]:
array([[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 10, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 20, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 30, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 40, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]])
But it looks like you want to replace a (4,4) block of values. We have to construct a pair on indices that together broadcast to the shape, that is (4,1) array with a (1,4) array.
In [105]: np.ix_(idx_1, idx_1)
Out[105]:
(array([[1],
[2],
[4],
[7]]), array([[1, 2, 4, 7]]))
In [106]: A[np.ix_(idx_1, idx_1)]
Out[106]:
array([[10, 0, 0, 0],
[ 0, 20, 0, 0],
[ 0, 0, 30, 0],
[ 0, 0, 0, 40]])
In [107]: A[np.ix_(idx_1, idx_1)] += 1
In [108]: A
Out[108]:
array([[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 11, 1, 0, 1, 0, 0, 1, 0, 0],
[ 0, 1, 21, 0, 1, 0, 0, 1, 0, 0],
[ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[ 0, 1, 1, 0, 31, 0, 0, 1, 0, 0],
[ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[ 0, 1, 1, 0, 1, 0, 0, 41, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]])
The equivalent indexing with nested lists is:
In [109]: A[[[1],[2],[4],[7]],[[1,2,4,7]]]
Out[109]:
array([[11, 1, 1, 1],
[ 1, 21, 1, 1],
[ 1, 1, 31, 1],
[ 1, 1, 1, 41]])
Regarding your indexing attempt:
In [110]: A[idx_1] # A[idx_1,:]
Out[110]:
array([[ 0, 11, 1, 0, 1, 0, 0, 1, 0, 0],
[ 0, 1, 21, 0, 1, 0, 0, 1, 0, 0],
[ 0, 1, 1, 0, 31, 0, 0, 1, 0, 0],
[ 0, 1, 1, 0, 1, 0, 0, 41, 0, 0]])
In [111]: A[idx_1][:,idx_1]
Out[111]:
array([[11, 1, 1, 1],
[ 1, 21, 1, 1],
[ 1, 1, 31, 1],
[ 1, 1, 1, 41]])
In[111] is evaluated in 2 steps; first rows are selected, and then columns.
In
A[idx_1][:,idx_1] = B
the values of B will replace columns in Out[110]. But that's a copy of values from A, not a view. So a good grasp of the difference between view and copy, and between basic and advanced indexing is important when working with numpy.
I have a pixel-array like the array below and from that I want to distinguish the two "groups" of 1s. The plan is to do this in a large set of similar pixel-arrays so I need to find a way to do this efficient.
Maybe I can add all the 1-positions to a separate array and do some search to find the ones connected, but it should be some better way. Is there any algorithms for finding connected components like this?
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
Considering that the groups should never touch each other, you can use scipy.ndimage.measurements.label to label the groups:
In [1]: import numpy as np
In [2]: from scipy.ndimage.measurements import label
In [3]: array = np.array(...) # your example
In [4]: structure = np.ones((3, 3), dtype=np.int) # this defines the connection filter
In [5]: structure # in this case we allow any kind of connection
Out[5]:
array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
In [6]: labeled, ncomponents = label(array, structure)
In [7]: labeled
Out[7]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)
In [7]: ncomponents
Out[7]: 2
Although I haven't read the particular implementation, SciPy tends to use highly efficient algorithms implemented in C, hence the performance should be relatively high. You can then extract the indices for each group using NumPy:
In [8]: indices = np.indices(array.shape).T[:,:,[1, 0]]
In [9]: indices[labeled == 1]
Out[9]:
array([[ 1, 6],
[ 1, 7],
[ 2, 6],
[ 2, 7],
[ 2, 8],
[ 2, 9],
[ 2, 10],
[ 2, 11],
[ 2, 12],
[ 2, 13],
[ 3, 11],
[ 3, 12],
[ 3, 13]])
In [10]: indices[labeled == 2]
Out[10]:
array([[ 5, 1],
[ 6, 1],
[ 7, 1],
[ 7, 2],
[ 8, 1],
[ 8, 2],
[ 9, 2],
[10, 2],
[10, 3],
[11, 2],
[11, 3],
[12, 3],
[13, 3]])