How to dilate a numpy array - python

I have a numpy array of points with shape (1000,3)
where axis 1 takes on values [x,y,1]
The points are at discrete values on a grid so an example array looks like:
array=([1,2,1],[4,5,1],[2,3,1],...,[xN,yN,1])
I would like to dilate this 2d array, and by this I mean, for each [x,y,1] coordinate in the array, if [x±1,y±1,1] is not in the array append it to the array.
currently I'm doing this with the following code:
np.append(array, [array[:,0],array[:,1]+1,1])
np.append(array, [array[:,0]+1,array[:,1],1])
np.append(array, [array[:,0]+1,array[:,1]+1,1])
np.append(array, [array[:,0]-1,array[:,1],1])
np.append(array, [array[:,0],array[:,1]-1,1])
np.append(array, [array[:,0]-1,array[:,1]-1,1])
np.append(array, [array[:,0]+1,array[:,1]-1,1])
np.append(array, [array[:,0]-1,array[:,1]+1,1])
then I am using np.unique(array) to reduce down to unqiue elements. This method works, but it is too slow to run on large arrays with more than 100000 points, and it doesn't feel like a smooth solution. There must be a way to do this without duplicating so many points then having to find all unique instances. Is there a different (read:faster) way to do what I am doing?

2000 x 4000 x 200 is just doable with a lookup table. At just below a million coordinates I get a speedup by a factor of ~5 compared to the np.unique approach.
lookup table: 2.18715, np.unique: 11.40247
Code:
import numpy as np
from numpy.lib.stride_tricks import as_strided
from time import time
coords = np.unique(np.random.randint(0, 2000*4000*200, (1000000,)))
coords = np.c_[coords // (4000*200), (coords // 200) % 4000, coords % 200]
t = [time()]
ws = np.empty((2002, 4002, 202), dtype=np.uint8)
ws = as_strided(ws, (2000, 4000, 200, 3, 3, 3), 2 * ws.strides)
ws[tuple(coords.T)] = np.arange(27).reshape(3, 3, 3)
unq = ws[tuple(coords.T)] == np.arange(27).reshape(3, 3, 3)
result = (coords[:, None, None, None, :] + np.moveaxis(np.indices((3, 3, 3)) - 1, 0, -1))[unq]
del ws
t.append(time())
result2 = np.unique((coords[:, None, None, None, :] + np.moveaxis(np.indices((3, 3, 3)) - 1, 0, -1)).reshape(-1, 3), axis = 0)
t.append(time())
print('lookup table: {:8.5f}, np.unique: {:8.5f}'.format(*np.diff(t)))

Related

Subtract 2D array from each pixel of a 3D image and get a 4D array

I have a 2D array of shape (10, 3) and an image represented as a 3D array of shape (480, 640, 3). I'd like to perform a difference between each pixel and each element of the 2D array, to get a final result of shape (10, 480, 640, 3).
For now, my code looks like this:
arr_2d = np.random.rand(10, 3)
arr_3d = np.random.rand(480, 640, 3)
res = np.ones_like(arr_3d)
res = np.tile(res, (10, 1, 1, 1))
for i in range(10):
res[i] = arr_3d - arr_2d[i]
My question is if there's a way to do this without the for loop, only using numpy operations.
You can try broadcasting with np.array like this
arr_2d = arr_2d.reshape(-1,1,1,3)
arr_3d = arr_3d.reshape((-1,*arr_3d.shape))
res = arr_3d - arr_2d
This should give the same result as your original code

How to stack numpy array along an axis

I have two numpy arrays, one with shape let's say (10, 5, 200), and another one with the shape (1, 200), how can I stack them so I get as a result an array of dimensions (10, 6, 200)? Basically by stacking it to each 2-d array iterating along the first dimension
a = np.random.random((10, 5, 200))
b = np.zeros((1, 200))
I'v tried with hstack and vstack but I get an error in incorrect number of axis
Let's say:
a = np.random.random((10, 5, 200))
b = np.zeros((1, 200))
Let's look at the volume (number of elements) of each array:
The volume of a is 10*5*200 = 10000.
The volume of an array with (10,6,200) is 10*5*200=1200.
That is you want to create an array that has 2000 more elements.
However, the volume of b is 1*200 = 200.
This means a and b can't be stacked.
As hpaulj mentioned in the comments, one way is to define an numpy array and then fill it:
result = np.empty((a.shape[0], a.shape[1] + b.shape[0], a.shape[2]))
result[:, :a.shape[1], :] = a
result[:, a.shape[1]:, :] = b

How to join two 3D numpy arrays so that np.arr(1,m,n) + np.arr(1,m,n) = np.arr(2,m,n)

I have several 3-dimensional numpy arrays that I want to join together to feed them as a training set for my LSTM neural network. They are mostly of shape (1,m,n)
I want to join them so that, for e.g. np.arr(1,50,20) + np.arr(1,50,20) = np.arr(2,50,20) and np.arr(1,50,20) + np.arr(3,50,20) = np.arr(4,50,20)
Which of the stack functions of numpy would suit my problem? Or is there another way to solve it more efficiently?
Use numpy concatenate with the first axis.
import numpy as np
rng = np.random.default_rng()
a = rng.integers(0, 10, (1, 3, 20))
b = rng.integers(-10, -1, (2, 3, 20))
c = np.concatenate((a, b), axis=0)
print(c.shape)
(3, 3, 20)
Use np.vstack
x = np.array([[[2,3,5],[4,5,1]]])
y = np.array([[[1,5,8],[8,0,9]]])
x.shape
(1,2,3)
np.vstack((x,y)).shape
(2,2,3)

Creating multiple random numpy arrays with different range of values

I got a question so I was trying to create a 3D array containing multiple 2D array with different range of values, for example I can do this:
import numpy as np
np.random.seed(1)
arr = np.random.randint(1, 10, size = (2,2)) #Random 2D array with range of values (1, 10)
arr2 = np.random.randint(11, 20, size = (2,2)) #Random 2D array with range of values (11, 20)
...
and then create the 3D array by this
newarr = np.array([arr, arr2, ...])
I try doing this:
import numpy as np
np.random.seed(1)
n = 3
aux = []
for i in range (n):
if i == 0:
aux.append(rng4.randint(1, 10, size = (2, 2)))
elif i == 1:
aux.append(rng4.randint(11, 20, size = (2, 2)))
elif i == 2:
aux.append(rng4.randint(21, 30, size = (2, 2)))
newarr = np.array(aux)
The output is what I want but in either case if I want another range of values I need to "add" manually a new elif to give another range, is there a way I can do this? Thank you!
It is a trivial loop programming exercise:
newarr = np.empty(shape=(2, 2, n))
for i in range (n):
newarr[:,:,i] = rng4.randint(i * 10 + 1, i * 10 + 10,
size = (2, 2))

Insert N matrices with 3 dimensions to a new variable

I need to insert 3-dimensional matrices into a new variable.
I'm trying to do that by:
Creating a 4-dimensional matrix and by promoting the fourth dimension saving the three dimensions respectively.
Sample code:
from python_speech_features import mfcc
import numpy as np
X = np.zeros((0,0,0,0),float) #4-dimensional - (0, 0, 0, 0)
ii = 0
for ii in range 1000:
data, fs = sf.read(curfile[ii])
sig = mfcc(data, fs, winstep=winstep,winlen=winlen,nfft=1024) #size - (49, 13)
sig = sig[:, :, np.newaxis] #add third-dimensional - (49, 13, 1)
X[:,:,:,ii] = sig
Error:
IndexError: index 0 is out of bounds for axis 3 with size 0
Someone can help me with that problem?
You are not creating array in right way. You cannot insert value in axis which have zero length at least specify some length for axis
X = np.zeros((10, 10, 10,1000), float)
print(X.shape)
# (10, 10, 10, 1000)
Now you can set value in whatever axis you want by simply,
X[:, :, :, 2] = 1
# this will simply set value of 3rd axis's 3rd element to 1
Either use np.stack (i think it is the best way of doing it) or create the initial array in its final size:
np.zeros((49,13,1,1000), float)
In your case

Categories