I need to generate all possible 4x4 binary matrices that have zeros along the main diagonal, are symmetric, and have six entries equal to 1. Some examples:
[[0,0,0,0],
[0,0,1,1],
[0,1,0,1],
[0,1,1,0]],
[[0,1,1,0],
[1,0,1,0],
[1,1,0,0],
[0,0,0,0]],
[[0,1,0,1],
[1,0,0,1],
[0,0,0,0],
[1,1,0,0]]
How could I do that in Python?
This amounts to choosing which three of the six entries
above the diagonal are 1.
From the list of above-the-diagonal positions in a 4 by 4 matrix:
sage: positions = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
use Sage's Subsets to get all subsets of size 3 of those positions.
sage: S = Subsets([(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)], 3)
Then build the corresponding matrices.
sage: [matrix(ZZ, 4, lambda i, j: (i, j) in s or (j, i) in s) for s in S]
[
[0 1 1 1] [0 1 1 0] [0 1 1 0] [0 1 1 0] [0 1 0 1] [0 1 0 1]
[1 0 0 0] [1 0 1 0] [1 0 0 1] [1 0 0 0] [1 0 1 0] [1 0 0 1]
[1 0 0 0] [1 1 0 0] [1 0 0 0] [1 0 0 1] [0 1 0 0] [0 0 0 0]
[1 0 0 0], [0 0 0 0], [0 1 0 0], [0 0 1 0], [1 0 0 0], [1 1 0 0],
[0 1 0 1] [0 1 0 0] [0 1 0 0] [0 1 0 0] [0 0 1 1] [0 0 1 1]
[1 0 0 0] [1 0 1 1] [1 0 1 0] [1 0 0 1] [0 0 1 0] [0 0 0 1]
[0 0 0 1] [0 1 0 0] [0 1 0 1] [0 0 0 1] [1 1 0 0] [1 0 0 0]
[1 0 1 0], [0 1 0 0], [0 0 1 0], [0 1 1 0], [1 0 0 0], [1 1 0 0],
[0 0 1 1] [0 0 1 0] [0 0 1 0] [0 0 1 0] [0 0 0 1] [0 0 0 1]
[0 0 0 0] [0 0 1 1] [0 0 1 0] [0 0 0 1] [0 0 1 1] [0 0 1 0]
[1 0 0 1] [1 1 0 0] [1 1 0 1] [1 0 0 1] [0 1 0 0] [0 1 0 1]
[1 0 1 0], [0 1 0 0], [0 0 1 0], [0 1 1 0], [1 1 0 0], [1 0 1 0],
[0 0 0 1] [0 0 0 0]
[0 0 0 1] [0 0 1 1]
[0 0 0 1] [0 1 0 1]
[1 1 1 0], [0 1 1 0]
]
Note that these are the adjacency matrices for all graphs
with three edges on four labeled vertices.
If you want un-labeled vertices, or equivalently the list
of adjacency matrices of equivalence classes of graphs
with three edges on four vertices, you could use Nauty
to enumerate them. Here is how to do that from Sage:
sage: G = graphs.nauty_geng("4 3:3")
sage: G
<generator object nauty_geng at 0x21c89a0f0>
sage: [g.adjacency_matrix() for g in G]
[
[0 0 0 1] [0 0 1 1] [0 0 1 1]
[0 0 0 1] [0 0 0 1] [0 0 0 0]
[0 0 0 1] [1 0 0 0] [1 0 0 1]
[1 1 1 0], [1 1 0 0], [1 0 1 0]
]
Related
I have the following numpy arrays
[[[0 0 1 0 0]
[1 0 0 0 0]
[0 0 1 0 0]]
[[1 0 0 0 0]
[0 0 1 0 0]
[0 0 0 1 0]]]
am trying to switch rows between them, 1 row 2 rows it doesn't matter am trying to see if it's possible.
The output can be for the 1st row or 2nd row or 2 first rows respectively:
[[[0 0 1 0 0] [[[0 0 1 0 0] [[[1 0 0 0 0]x
[1 0 0 0 0] [0 0 1 0 0]x [0 0 1 0 0]x
[0 0 0 1 0]]x [0 0 1 0 0]] [0 0 1 0 0]]
[[1 0 0 0 0] [[1 0 0 0 0] [[0 0 1 0 0]x
[0 0 1 0 0] [1 0 0 0 0]x [1 0 0 0 0]x
[0 0 1 0 0]]]x [0 0 0 1 0]]] [0 0 0 1 0]]]
Is it possible? If so How?
You can switch values like rows on NumPy arrays with Python variable swap operator:
import numpy as np
m = np.array([[0, 0, 1, 0, 0],
[1, 0 ,0, 0, 0],
[0, 0, 1, 0, 0]])
n = np.array([[1, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0]])
#m[:, 0], n[:, 0] = n[:, 0].copy(), m[:, 0].copy() #Only for columns
m[0], n[0] = n[0].copy(), m[0].copy() #For rows
print(m, n)
Output:
[[1 0 0 0 0]
[1 0 0 0 0]
[0 0 1 0 0]]
[[0 0 1 0 0]
[0 0 1 0 0]
[0 0 0 1 0]]
I have a numpy array which is of the following type:-
[[[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 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 0 0]
[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 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 1 0]]
[[1 1 1 ... 0 0 0]
[1 1 0 ... 0 0 0]
[1 1 1 ... 0 0 0]
...
[0 1 1 ... 1 1 1]
[1 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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
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
[[1 1 1 ... 1 1 0]
[1 1 0 ... 0 1 1]
[0 0 0 ... 1 0 1]
...
[0 0 1 ... 0 0 0]
[0 1 0 ... 0 0 0]
[1 0 0 ... 0 0 0]]
[[0 0 0 ... 1 0 0]
[1 1 1 ... 0 1 1]
[0 0 1 ... 1 0 1]
...
[1 0 0 ... 1 0 1]
[0 1 0 ... 1 0 0]
[0 0 0 ... 1 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 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 ... 1 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]
[1 1 0 ... 0 0 0]
[1 0 0 ... 0 0 0]
...
[1 0 0 ... 0 1 0]
[1 0 0 ... 1 1 0]
[0 1 1 ... 1 1 1]]
[[0 0 0 ... 0 0 0]
[0 1 0 ... 1 1 0]
[1 0 0 ... 0 1 0]
...
[0 0 0 ... 0 0 1]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]]]
I want to extract only 1/4th of the array but the file is too big to be loaded in RAM. I have tried to use mmap but couldn't understand how to do it because it is giving completely different values than that of my numpy array. Even when I loaded the code using the mem map it gives wrong shape and values. I have noticed that on changing dtype the values also change and cannot understand it. My Method for mem map:-
load = np.memmap("dataset/b.npy", mode='r', shape=(14,8,8))
Is it possible to take a 3D array and and turn it into a coordinate system? My array consists of 0s and 1s. If the value is 1 I want to take the xyz coordinate. In the end I want to output all coordinates to a csv file.
import nibabel as nib
coord = []
img = nib.load('test.nii').get_fdata().astype(int)
test.nii array:
[[[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 ... 1 1 1]
[0 1 0 ... 0 0 0]]
[[1 0 0 ... 0 0 0]
[0 0 1 ... 0 0 0]
[0 1 0 ... 0 0 0]
...
[0 1 0 ... 0 0 0]
[0 1 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 0 ... 0 0 0]
[0 1 0 ... 0 1 1]]
...
[[0 0 0 ... 1 0 0]
[0 0 1 ... 0 0 0]
[0 0 1 ... 0 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 ... 0 0 1]
...
[0 1 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]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 1 0]
[0 1 0 ... 0 0 0]]]
That might not necessarily be the best solution, but let's keep it simple (would be great if framework did that for us, but...well):
data = [[[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, 1, 1, 1],
[0, 1, 0, 0, 0, 0]],
[[1, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0]]]
for x in range(len(data)):
for y in range(len(data[x])):
for z in range(len(data[x][y])):
if data[x][y][z] == 1:
print(f"{x} {y} {z}")
yields:
0 2 2
0 2 3
0 2 4
0 4 3
0 4 4
0 4 5
0 5 1
1 0 0
1 1 2
1 2 1
1 3 1
1 4 1
1 5 3
Using np.where() you can get the row, col and depth index of elements that satisfy you condition.
Try this:
row_idx, col_idx, depth_idx = np.where(img==1)
I am trying to implement a neural network using keras.fit_generator() function. I have implemented the generator function that yields x, y where x is the data and y is the ground truth for the result and both are (156, 156, 156) numpy arrays. However, when I try to feed the data I get an error that says "Output of generator should be a tuple (x, y, sample_weight) or (x, y). Found: tf.Tensor".
When I checked what I obtain when I create my dataset using the generator function and iterate through it, and what I am getting is indeed a tf.Tensor. However, I couldn't figure out how to make it return a tuple instead of a tf.Tensor with shape (1, 2, 156, 156, 156). What should I do to get a tuple of (x, y)?
For simplicity, I used the following generator function that is supposed to yield (x, y):
def tuple_fun():
for _ in range(10):
x = np.random.rand(156,156,156)
y = np.random.rand(156,156,156)
yield tuple((x, y))
I generated the dataset with the following piece of code:
def dataset_generator(batch_size):
dataset = tf.data.Dataset.from_generator(lambda: tuple_fun(),
output_types=tf.int8,
output_shapes = (2, 156, 156, 156)).batch(batch_size)
return dataset
Then I tried feeding the data into a neural network via keras.fit_generator() as follows:
test_batch_size = 1
test_dataset = dataset_generator(test_batch_size)
iterator = iter(test_dataset)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv3D(input_shape=(156, 156, 156, 1),
filters=5, padding="same", kernel_size=3,
activation='relu',
kernel_initializer=tf.keras.initializers.TruncatedNormal()))
model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(decay=0.002))
model.fit_generator(iterator, steps_per_epoch=1, epochs=1)
And I got the following error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-97-e1055a8161e6> in <module>
26
27 model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(decay=0.002))
---> 28 model.fit_generator(iterator, steps_per_epoch=1, epochs=1)
~/anaconda3/envs/condaEnv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1513 shuffle=shuffle,
1514 initial_epoch=initial_epoch,
-> 1515 steps_name='steps_per_epoch')
1516
1517 def evaluate_generator(self,
~/anaconda3/envs/condaEnv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_generator.py in model_iteration(model, data, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch, mode, batch_size, steps_name, **kwargs)
211 step = 0
212 while step < target_steps:
--> 213 batch_data = _get_next_batch(generator, mode)
214 if batch_data is None:
215 if is_dataset:
~/anaconda3/envs/condaEnv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_generator.py in _get_next_batch(generator, mode)
363 raise ValueError('Output of generator should be '
364 'a tuple `(x, y, sample_weight)` '
--> 365 'or `(x, y)`. Found: ' + str(generator_output))
366
367 if len(generator_output) < 1 or len(generator_output) > 3:
ValueError: Output of generator should be a tuple `(x, y, sample_weight)` or `(x, y)`. Found: tf.Tensor(
[[[[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
...
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]]
[[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
...
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]]]], shape=(1, 2, 156, 156, 156), dtype=int8)
How can I solve this problem? This was my first question in stackoverflow so I hope I didn't do anything wrong. Thanks in advance for any help!
I want to store this:
for i in range(len(cpi)):
print((cf[:i+1] / cpi[i]).astype(int))
Into a matrix. I tried this:
payment = np.zeros((len(cpi), len(cpi)))
for i in range(len(cpi)):
payment = cf[:i+1] / cpi[i]).astype(int)
But that gives me the wrong matrix that looks like this:
[[0 1 0 ..., 1 0 3]
[2 9 2 ..., 4 0 4]
[4 0 8 ..., 9 6 3]
...,
[0 0 0 ..., 0 0 0]
[1 3 0 ..., 1 1 1]
[0 3 0 ..., 1 0 0]]
When the output show be this:
[[0 1 0 1 1 1 0 5 1 0 2]]
[[0 0 0 0 0 0 0 2 0 0 1]
[1 3 1 0 0 1 1 1 1 0 1]]
[[0 0 0 0 0 0 0 1 0 0 0]
[0 2 0 0 0 0 0 0 0 0 0]
[1 0 2 0 2 1 1 0 2 1 0]]
[[0 1 0 1 1 1 0 5 1 0 2]
[1 6 1 0 0 1 1 1 2 0 2]
Note that is not all the elements just some of them.
The issue is you are assigning new value to payment variable each time you loop, when you actually want to just append to it. For example (not tested):
payment=[]
for i in range(len(cpi)):
payment.append((cf[:i+1] / cpi[i]).astype(int))