Numpy check if a matrix can be transformed to another matrix by swaping columns - numpy

Say we have matrix A and B as follow
>>> A
matrix([[0, 0, 0, 1],
[1, 0, 0, 0],
[1, 0, 0, 0]])
>>> B
matrix([[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 1, 0]])
Clearly we can "transform" matrix A to B by column swapping. Is there an efficient algorithm to check whether two (potentially large) matrices can be transformed to each other in this way?

Here is a simple function. For very large matrix, it is possible that (A==B).all() is slower than np.array_equal(A,B).
import numpy as np
A = np.array([[0, 0, 0, 1],
[1, 0, 0, 0],
[1, 0, 0, 0]])
B = np.array([[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 1, 0]])
def isSwaping(a, b):
count = 0
for i, c in enumerate(a.T): # transpose of a
for d in b.T:
if (c == d).all():
count += 1
break
if count == i : # then it is uncessary to continue
return False
return True
print isSwaping(A, B)

Related

How to create a sparse matrix with a given base matrix?

I have the following 2 x 2 matrix
1 0
1 1
I want to expand this matrix with dimensions in powers of 2. For example the matrix with dimension 4 would look like:
1 0 0 0
1 1 0 0
1 0 1 0
1 1 1 1
Essentially, I want to retain the original matrix wherever 1 occurs in the base matrix and fill up zeros where 0 occurs in the base matrix? Is there a fast way to do this in numpy or scipy? I want to be able to expand this to any power of 2, say 512 or 1024.
For relatively small values of the powers of 2 (say up to 10), you can recursively replace every 1 with the inital matrix a using numpy block:
import numpy as np
a = np.array([[1, 0], [1, 1]])
def generate(a, k):
z = np.zeros_like(a)
result = a.copy()
for _ in range(1, k):
result = eval(f"np.block({str(result.tolist()).replace('1', 'a').replace('0', 'z')})")
return result
Example for k=3 (8x8 result matrix) generate(a, 3):
array([[1, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 0, 1, 0, 0, 0],
[1, 1, 0, 0, 1, 1, 0, 0],
[1, 0, 1, 0, 1, 0, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1]])
You can combine tile and repeat.
>>> np.tile(arr, (2, 2))
array([[1, 0, 1, 0],
[1, 1, 1, 1],
[1, 0, 1, 0],
[1, 1, 1, 1]]
>>> np.repeat(np.repeat(arr, 2, axis=1), 2, axis=0)
array([[1, 1, 0, 0],
[1, 1, 0, 0],
[1, 1, 1, 1],
[1, 1, 1, 1]])
Then just multiply:
def tile_mask(a):
tiled = np.tile(a, (2, 2))
mask = np.repeat(
np.repeat(a, 2, axis=1),
2, axis=0
)
return tiled * mask
>>> tile_mask(arr)
array([[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 0, 1, 0],
[1, 1, 1, 1]])
I don't know of a good way to do this for higher powers besides recursion though:
def tile_mask(a, n=2):
if n > 2:
a = tile_mask(a, n-1)
tiled = np.tile(a, (2, 2))
mask = np.repeat(
np.repeat(a, 2, axis=1),
2, axis=0
)
return tiled * mask
>>> tile_mask(arr, 3)
array([[1, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 0, 1, 0, 0, 0],
[1, 1, 0, 0, 1, 1, 0, 0],
[1, 0, 1, 0, 1, 0, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1]])

find numpy rows that are the same

I have a numpy array
How can I find which of them are the same and how many times appear in the matrix?
thanks
dummy example:
A=np.array([[0, 1, 0, 1],[0, 0, 0, 0],[0, 1, 1, 1],[0, 0, 0, 0]])
You can use numpy.unique with axis=0 and return_counts=True:
np.unique(A, axis=0, return_counts=True)
Output:
(array([[0, 0, 0, 0],
[0, 1, 0, 1],
[0, 1, 1, 1]]),
array([2, 1, 1]))

What is the purpose of rotating filters while building convolutions with scipy signal?

I recently came across a bit of python code (shown below) which does 2d convolution with scipy signal.
x = np.array([[1, 1, 1, 0, 0],
[0, 1, 1, 1, 0],
[0, 0, 1, 1, 1],
[0, 0, 1, 1, 0],
[0, 1, 1, 0, 0]],
dtype='float')
w_k = np.array([[1, 0, 1],
[0, 1, 0],
[1, 0, 1],],
dtype='float')
w_k = np.rot90(w_k, 2)
f = signal.convolve2d(x, w_k, 'valid')
Right before the convolve2d operation, the filter was rotated. What is the purpose of that?

What is the order output of conv2d in tensorflow?

I wanted to get the values of output tensor in tensorflow.
the kernel shape of first layer was K[row, col, in_channel, out_channel].
the input image shape is P[batch, row, col, channel]
But I tried to get the first four kernel value, they were K[0, 0, 0, 0], K[0, 1, 0, 0], K[1, 0, 0, 0], K[1, 1, 0, 0].
I got the input values were P[0, 0, 0, 0], P[0, 0, 1, 0], P[0, 1, 0, 0], P[0, 1, 1, 0].
python code is that "F = tf.nn.conv2d(P, K, stride=[1, 1, 1, 1], padding='SAME')"
Console showed output value (F[0, 0, 0, 0]) is not K[0, 0, 0, 0] * P[0, 0, 0, 0] + K[0, 1, 0, 0] * P[0, 0, 1, 0] + K[1, 0, 0, 0] * P[0, 1, 0, 0] + K[1, 1, 0, 0] * P[0, 1, 1, 0]
What is the order of these output feature maps? I had 40 conv_kernel,the first output was not computed by the first conv_kernel
There's something wrong in your input values.
Remember that conv2d wants an input tensor of shape [batch, in_height, in_width, in_channels] and a filter / kernel tensor of shape [filter_height, filter_width, in_channels, out_channels].
In fact, reshaping your data the results are the one expected (please note that conv2d computes the correlation and not the convolution).
import tensorflow as tf
K = tf.get_variable("K", shape=(4,4), initializer=tf.constant_initializer([
[0, 0, 0, 0],
[0, 1, 0, 0],
[1, 0, 0, 0],
[1, 1, 0, 0]
]))
K = tf.reshape(K, (4,4,1,1))
P = tf.get_variable("P", shape=(4,4), initializer=tf.constant_initializer([
[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 1, 0, 0],
[0, 1, 1, 0]
]))
P = tf.reshape(P, (1,4,4,1))
F = tf.nn.conv2d(P, K, strides=[1, 1, 1, 1], padding='VALID')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run(F))
In this example, I'm computing the correlation between the input P (a batch with 1 element of depth 1) and the filter P (4x4 filter, with input depth 1 and output depth 1).

tensorflow manipulate labels vector into "multiple hot encoder"

is it possible (in a nice way, that is) in tensorflow to achieve the next functionality:
assume we have a dense vector of tags
labels = [0,3,1,2,0]
I need to make a "multiple hot encoder" of it. meaning, for each row I need 1's up to the index of the label minus 1
so the required result will be
[[0, 0, 0],
[1, 1, 1],
[0, 0, 1],
[0, 1, 1],
[0, 0, 0]]
thanks
You could do this using tf.nn.embeddings_lookup as shown here:
embeddings = tf.constant([[0,0,0], [0,0,1], [0,1,1], [1,1,1]])
labels = [0,3,1,2,0]
encode_tensors = tf.nn.embedding_lookup(embeddings,labels)
Output of sess.run(encode_tensors) :
array([[0, 0, 0],
[1, 1, 1],
[0, 0, 1],
[0, 1, 1],
[0, 0, 0]], dtype=int32)
Hope this helps !
for completion:
it's also possible to use:
In [397]: labels = np.array([1, 2, 0, 3, 0])
In [398]: sess.run(tf.sequence_mask(labels, 3, dtype=tf.int8))
Out[398]:
array([[1, 0, 0],
[1, 1, 0],
[0, 0, 0],
[1, 1, 1],
[0, 0, 0]], dtype=int8)
the result matrix will be reversed from what I asked though