Python function to expand image (NumPy array) - python

Say I have a greyscale image that is 3x3 and is represented by the numpy array below.
I want to increase the size and resolution of the image, similar to a resizing function in a normal picture editing software, but I don't want it to change any of the values of the pixels, just to expand them.
Is there a Python function that does the following conversion?
[0,0,0]
[0,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,1,1,0,0]
[0,0,0,0,0,0]
[0,0,0,0,0,0]

You could use np.repeat along both axes of the 3x3 img array:
>>> img.repeat(2, axis=0).repeat(2, axis=1)
array([[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, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]])
Another way is to calculate the Kronecker product of img and an array of the appropriate shape filled with ones:
>>> np.kron(img, np.ones((2,2)))
array([[ 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., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.]])
Here's a link to the documentation for np.kron:
So in the example above, each value x in img is multiplied by a 2x2 array of ones to create a 2x2 array of x values. These new 2x2 arrays make up the returned array.
This multiplication might be slower than simply repeating, however.

Related

just fill specific entrys of numpy array -> generating sparse matrix

I want to code a really big matrix with the following structure:
a = np.array([[1, 1, 1, 0, 0 ,0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 1]])
Dimension of this array is (3,9) so basically the 1's depend on the dimension.
In my first row the first 3 entrys should be 1's, in my second row the entrys 3-5 should be 1's,
and so on...
How do I code this?
You can use the kronecker tensor product:
a = np.kron(np.eye(3),np.ones((1,3)))
# array([[1., 1., 1., 0., 0., 0., 0., 0., 0.],
# [0., 0., 0., 1., 1., 1., 0., 0., 0.],
# [0., 0., 0., 0., 0., 0., 1., 1., 1.]])

How to generate identity tensor with python?

I know about np.eye which generates identity matrix. I mean the identity matrix as
In linear algebra, the identity matrix, or sometimes ambiguously called a unit matrix, of size n is the n × n square matrix with ones on the main diagonal and zeros elsewhere.
And I know that we can create it in Numpy with np.identity(3).
But, I would like to know how can I have an identity Tensor in python.
I would like to use identity tensor in tensors multiplication. Like below:
where G = Er ×1 U1 ×2 U2 ...×M UM is a transformation tensor, and Er ∈
R r×r×...×r is an identity tensor (the diagonal elements are 1, and all other entries are 0). I need to have the code for generating the identity tensor.
Thank you in advance.
Something like this?
def nd_id(n, d):
out = np.zeros( (n,) * d )
out[ tuple([np.arange(n)] * d) ] = 1
return out
Testing
nd_id(3,3)
Out[]:
array([[[ 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., 1.]]])
Instead of np.identity use tf.eye:
tf.eye(2)
# [[1., 0.],
# [0., 1.]]
It can be done with a function that returns one if all indices are equal, but it must be vectorized in order to be used in np.fromfunction
np.fromfunction(np.vectorize(lambda i,j,k: int(i==j==k)), (3,3,3))
Output:
array([[[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, 1]]])

Create binary tensor from vector in tensorflow

I start with a 1D numpy array x (or tensorflow tensor) with N integer entries. Every entry is smaller or equal to N.
I now want to create a tensor Y of shape (N,N) (i.e. an NxN matrix) where Y[i,j]=0 if x[i]!=x[j] and Y[i,j]=1 if x[i]==x[j].
Example with numpy:
import numpy as np
x=np.array([1,2,1,2,3,4,2])
Y=np.zeros((x.shape[0],x.shape[0]))
for i in range(x.shape[0]):
for j in range(x.shape[0]):
if x[i]==x[j]:
Y[i,j]=1
Output
array([[ 1., 0., 1., 0., 0., 0., 0.],
[ 0., 1., 0., 1., 0., 0., 1.],
[ 1., 0., 1., 0., 0., 0., 0.],
[ 0., 1., 0., 1., 0., 0., 1.],
[ 0., 0., 0., 0., 1., 0., 0.],
[ 0., 0., 0., 0., 0., 1., 0.],
[ 0., 1., 0., 1., 0., 0., 1.]])
How do I create the same function efficiently in pure tensorflow code?
And: What if I have an extra batch dimension, so that the input x has shape (B,N) and I expect as an ouput Y with shape (B,N,N). The batches are all independent of each other.
Reshape x to two different shapes, (B, 1, N) and (B, N, 1) so they can be properly broadcasted, then compare these two tensors, the result would be what you need with 1 being True and 0 being False:
import tensorflow as tf
import numpy as np
x=np.array([1,2,1,2,3,4,2])
t = tf.constant(x)
r = tf.cast(
tf.equal(
tf.reshape(t, (-1, 1, t.shape[-1].value)),
tf.reshape(t, (-1, t.shape[-1].value, 1))
), tf.int8)
sess = tf.Session()
sess.run(r)
#array([[[1, 0, 1, 0, 0, 0, 0],
# [0, 1, 0, 1, 0, 0, 1],
# [1, 0, 1, 0, 0, 0, 0],
# [0, 1, 0, 1, 0, 0, 1],
# [0, 0, 0, 0, 1, 0, 0],
# [0, 0, 0, 0, 0, 1, 0],
# [0, 1, 0, 1, 0, 0, 1]]], dtype=int8)
import tensorflow as tf
x = tf.constant([1,2,1,2,3,4,2])
x = tf.expand_dims(x, axis=0)
x = tf.tile(x, [x.shape[1], 1])
x_ = tf.transpose(x)
Y = tf.where(tf.equal(x,x_), tf.ones_like(x), tf.zeros_like(x))
There you have your vector x. You expand dims to have a matrix [1, x.shape]. Then you repeat it to have a copy of the same vector along the lines. x[i] == x[j] is therefore equivalent to x == x_ where x_ is the transposed of your matrix x.
tf.where is a conditional tensor. You give the condition (x == x_), and for each element, if it is true, it will take the first value (tf.ones_like) and if it is false it will take the second value (tf.zeros_like). Those *_like(x) functions are generating a tensor full of 0 or 1 with the same shape than x.

how to efficiently insert element for a n dimension array

There is a simple example.
I have a 2d array
a=np.arange(4).reshape(2,2)+1
array([[1, 2],
[3, 4]])
and I wanna insert 0 (or any other value) in the beginning and end of the array, then it becomes
array([[ 0., 0., 0., 0.],
[ 0., 1., 2., 0.],
[ 0., 3., 4., 0.],
[ 0., 0., 0., 0.]])
I'm trying np.insert or np.concatenate, but I failed for >2 dimension. What is the fastest way to handle this kind of problem?
Use numpy.pad:
>>> np.pad(a, 1, 'constant')
array([[0, 0, 0, 0],
[0, 1, 2, 0],
[0, 3, 4, 0],
[0, 0, 0, 0]])

transform a numpy array of dimensions nx1 into a numpy array of dimensions nx10 [duplicate]

This question already has answers here:
Convert array of indices to one-hot encoded array in NumPy
(22 answers)
Closed 7 years ago.
I have a numpy array, A of size nx1 where each value is a number between 0 and 9.
I would like to create a new array, B of size nx10 such that in B[i] we store a numpy array that contains zeros and a 1 in position A[i].
For example:
A array
[[9]
[2]
[4]
[1]
[8]]
B array
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0]]
Is there an elegant way of doing this with numpy?
Create a new empty array using numpy.zeros, its size is going to be (arr.size, arr.max()), now fill the items on those positions using multi-dimensional indexing:
>>> arr = np.array([[9], [2], [4], [1], [8]])
>>> arr_ = np.zeros((arr.size, arr.max()))
>>> arr_[np.arange(arr.size), arr[:,0]-1] = 1
>>> arr_
array([[ 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[ 0., 1., 0., 0., 0., 0., 0., 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., 1., 0.]])
Something like this would do it:
A = [[9],[2],[4],[1],[8]]
B = [[1 if i == j[0] else 0 for i in range(10)] for j in A]
This is a list-based approach; you can simply use np.asarray on B to get the numpy matrix, or create a 10x10 matrix of zeros in numpy and fill 1s in the positions dictated by the A array.
The latter generalises to the case where A's elements might have more than one item.
You could do something like this, assuming an NP Nx1 array of X:
max_val = max(X)
length_arr = len(X)
new_arr = np.zeros((max_val,length_arr))
This will create the array of the right size that you want.
for i in range(len(X)):
new_arr[i][X[i]-1]=1
Should then assign the correct values in place?
Works fine in my test case.

Categories