Related
I want to apply something like this:
a = np.array([1,2,3])
np.broadcast_to(a, (3,3))
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
On each vector in a multi-vector array:
a = np.array([[1,2,3], [4,5,6]])
np.broadcast_to(a, (2,3,3))
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,3) and requested shape (2,3,3)
To get something like this:
array([[[1, 2, 3],
[1, 2, 3],
[1, 2, 3]],
[[4, 5, 6],
[4, 5, 6],
[4, 5, 6]]])
One way is to use list-comprehension and broadcast each of the inner array:
>>> np.array([np.broadcast_to(i, (3,3)) for i in a])
array([[[1, 2, 3],
[1, 2, 3],
[1, 2, 3]],
[[4, 5, 6],
[4, 5, 6],
[4, 5, 6]]])
Or, you can just add an extra dimension to a then call broadcast_to over it:
>>> np.broadcast_to(a[:,None], (2,3,3))
array([[[1, 2, 3],
[1, 2, 3],
[1, 2, 3]],
[[4, 5, 6],
[4, 5, 6],
[4, 5, 6]]])
So I've got a numpy array like this:
a = np.array([[1, 2, 3],
[2, 3, 4],
[4, 5, 6]])
and I want to convert it into an array like this:
[[[1, 1, 1], [2, 2, 2], [3, 3, 3]],
[[2, 2, 2], [3, 3, 3], [4, 4, 4]],
[[4, 4, 4], [5, 5, 5], [6, 6, 6]]]
How would you do it?
Use numpy.repeat on the array with one extra dimension:
np.repeat(a[...,None], 3, axis=2)
Or numpy.tile:
np.tile(a[...,None], (1,1,3))
Output:
array([[[1, 1, 1],
[2, 2, 2],
[3, 3, 3]],
[[2, 2, 2],
[3, 3, 3],
[4, 4, 4]],
[[4, 4, 4],
[5, 5, 5],
[6, 6, 6]]])
I have a two-dimensional NumPy array with shape
(2, 2)
Example array
array([[1, 2],
[3, 4]])
I am trying to have it copy on just the first axis until it reaches the shape:
(5, 2)
Example result array
array([[1, 2],
[1, 2],
[1, 2],
[3, 4],
[3, 4]])
np.repeat does the job but it has to be a multiple as it repeats everything
np.repeat(arr, 3, axis=0)
array([[1, 2],
[1, 2],
[1, 2],
[3, 4],
[3, 4],
[3, 4]])
giving a 6 by 2 array and not a 5 by 2 array
np.repeat(arr, [3, 2], axis=0)
I want to add dimensions to an array, but expand_dims always adds dimension of size 1.
Input:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
What expand_dims does:
[[[1], [2], [3]], [[4], [5], [6]], [[7], [8], [9]]]
What I want:
[[[1, 1], [1, 2], [1, 3]], [[1, 4], [1, 5], [1, 6]], [[1, 7], [1, 8], [1, 9]]]
Basically I want to replace each scalar in the matrix by a vector [1, x] where x is the original scalar.
Here's one way using broadcasting and np.insert() function:
In [32]: a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
In [33]: np.insert(a[:,:,None], 0, 1, 2)
Out[33]:
array([[[1, 1],
[1, 2],
[1, 3]],
[[1, 4],
[1, 5],
[1, 6]],
[[1, 7],
[1, 8],
[1, 9]]])
There are lots of ways of constructing the new array.
You could initial the array with right shape and fill, and copy values:
In [402]: arr = np.arange(1,10).reshape(3,3)
In [403]: arr
Out[403]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [404]: res = np.ones((3,3,2),int)
In [405]: res[:,:,1] = arr
In [406]: res
Out[406]:
array([[[1, 1],
[1, 2],
[1, 3]],
[[1, 4],
[1, 5],
[1, 6]],
[[1, 7],
[1, 8],
[1, 9]]])
You could join the array with a like size array of 1s. concatenate is the basic joining function:
In [407]: np.concatenate((np.ones((3,3,1),int), arr[:,:,None]), axis=2)
Out[407]:
array([[[1, 1],
[1, 2],
[1, 3]],
[[1, 4],
[1, 5],
[1, 6]],
[[1, 7],
[1, 8],
[1, 9]]])
np.stack((np.ones((3,3),int), arr), axis=2) does the same thing under the covers. np.dstack ('d' for depth) does it as well. The insert in the other answer also does this.
I have an application where I need to reverse the elements along one axis of a NumPy array according to some stride. As an example, let's consider that I have the following array
In [1]: import numpy as np
In [2]: a = np.array([[1,2],[3,4],[5,6],[7,8]])
In [3]: a
Out[3]:
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
I want to reverse this array along the first axis (axis=0) with a stride of 2. In other words, I want to quickly get the following array:
Out[4]:
array([[3, 4],
[1, 2],
[7, 8],
[5, 6]])
Is there a quick way to do this with a built-in NumPy routine?
Reshape to a 3d array; reverse one dimension; return to 2d:
In [51]: a = np.array([[1,2],[3,4],[5,6],[7,8]])
In [52]: a
Out[52]:
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
In [53]: a.reshape(2,2,2)
Out[53]:
array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
In [54]: a.reshape(2,2,2)[:,::-1,:]
Out[54]:
array([[[3, 4],
[1, 2]],
[[7, 8],
[5, 6]]])
In [55]: a.reshape(2,2,2)[:,::-1,:].reshape(4,2)
Out[55]:
array([[3, 4],
[1, 2],
[7, 8],
[5, 6]])