Apply numpy broadcast_to on each vector in an array - python

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]]])

Related

Count set of elements in numpy array

i have numpy array
array([[1, 2, 3],
[1, 2, 5],
[3, 4, 6],
[2, 5, 4],
[5, 4, 3],
[3, 5, 1],
[2, 5, 1]])
i want function to count how many times set of values appears in array. For example
count_set([1,2])
#output
3
# because set[1,2] appears in elements 0,1,6
I have tried some np.notezero , but it doesnt workout
Use broadcasted comparison with all/any:
a = np.array([[1, 2, 3],
[1, 2, 5],
[3, 4, 6],
[2, 5, 4],
[5, 4, 3],
[3, 5, 1],
[2, 5, 1]])
def count_set(a, elems):
return (a[..., None]==elems).any(-2).all(-1).sum()
count_set(a, [1, 2])
# 3

building a 3d Numpy array from a 2d numpy array

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]]])

Insert item and change the array's dimension

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.

How do I reshape the following array into the other one?

How do I reshape the following numpy array with reshape:
array([[1, 2],
[3, 4],
[5, 6]])
into this one:
array([[1, 3, 5],
[2, 4, 6]])
Transpose it using - x.T
Output -
array([[1, 3, 5],
[2, 4, 6]])
The transformation that you are trying to achieve is a transpose.
a = np.array([[1, 2],
[3, 4],
[5, 6]])
a.T # array([[1, 3, 5], [2, 4, 6]])

How to add ones to matrix?

I have an array:
X = [[2, 2, 2],
[3, 3, 3],
[4, 4, 4]]
I need to add extra column in numpy array and fill it with ones using hstack and reshape. Like that:
X = [[2, 2, 2, 1],
[3, 3, 3, 1],
[4, 4, 4, 1]]
What I do:
X = np.hstack(X, np.ones(X.reshape(X, (2,3))))
And a get an error:
TypeError: only length-1 arrays can be converted to Python scalars
What's a problem? What I've done wrong?
Here's a couple ways with numpy.append, numpy.hstack or numpy.column_stack:
# numpy is imported as np
>>> x
array([[2, 2, 2],
[3, 3, 3],
[4, 4, 4]])
>>> np.append(x, np.ones([x.shape[0], 1], dtype=np.int32), axis=1)
array([[2, 2, 2, 1],
[3, 3, 3, 1],
[4, 4, 4, 1]])
>>> np.hstack([x, np.ones([x.shape[0], 1], dtype=np.int32)])
array([[2, 2, 2, 1],
[3, 3, 3, 1],
[4, 4, 4, 1]])
>>> np.column_stack([x, np.ones([x.shape[0], 1], dtype=np.int32)])
array([[2, 2, 2, 1],
[3, 3, 3, 1],
[4, 4, 4, 1]])
You can use numpy.insert():
>>> X
array([[2, 2, 2],
[3, 3, 3],
[4, 4, 4]])
Ones at the begining of matrix:
>>> X=np.insert(X,0,1.0,axis=1)
>>> X
array([[1, 2, 2, 2],
[1, 3, 3, 3],
[1, 4, 4, 4]])
Ones at the end of matrix
>>> X=np.insert(X,3,1.0,axis=1)
>>> X
array([[2, 2, 2, 1],
[3, 3, 3, 1],
[4, 4, 4, 1]])

Categories