Numpy array reshape element-wise - python

I have a 3-D array
size = (3,2,3)
[
[[1, 2, 3],[4, 5, 6]],
[[7, 8, 9],[10,11,12]],
[[13,14,15],[16,17,19]]
]
How to reshape to (3,3,2):
[
[[1,4], [2,5], [3,6]],
[[7,10], [8,11], [9,12],],
[[13,16],[14,17],[15,19]]
]

You task is not to reshape the array. You have to swap the last axis (the third dimension of your array) with the second.
import numpy as np
#input
arr = np.array([
[[1, 2, 3],[4, 5, 6]],
[[7, 8, 9],[10,11,12]],
[[13,14,15],[16,17,19]]
])
#output
np.moveaxis(arr, 2, 1)
#an alternative is
np.swapaxes(arr, 1, 2)

x = [[[1, 2, 3],[4, 5, 6]],
[[7, 8, 9],[10,11,12]],
[[13,14,15],[16,17,19]]]
res = [[[i,j] for (i,j) in zip(sub[0], sub[1])] for sub in x]

Related

transfer 3D NumPy array into a 2D NumPy array

let's say a NumPy array
a = np.array(
[[[1,2,3],
[4,5,6]],
[[7,8,9],
[10,11,12]]])
the shape will be like (2,2,3).
I'd like to make it look like this:
a = np.array(
[[1,2,3],
[7,8,9],
[4,5,6],
[10,11,12]]
)
which shape will be like (4,3).
if I use reshape, it will look like as:
a = np.array(
[[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]]
)
Which is NOT what I want. How to do this?
One way using numpy.stack and vstack:
np.vstack(np.stack(a, 1))
Output:
array([[ 1, 2, 3],
[ 7, 8, 9],
[ 4, 5, 6],
[10, 11, 12]])
By using indexing method, an idx list could be created that specifies which indices of the ex a must be placed as which indices in the new one i.e. idx is a rearranging list:
idx = [0, 2, 1, 3]
a = a.reshape(4, 3)[idx]
a is firstly reshaped to the intended shape, which is (4,3), and then rearranged by the idx. idx[1] = 2 is showing that value in index = 2 of the ex a will be replaced to index = 1 in the new a.
Here is a more pythonic version of your problem.
This uses concatenate so append the rows of your array.
a = np.array(
[[[1,2,3],
[4,5,6]],
[[7,8,9],
[10,11,12]]]
)
def transform_2d(a_arr):
nrow = len(a[:])
all = a_arr[:,0]
for i in range(1,nrow):
all = np.concatenate((all, a_arr[:,i] ))
return all
print(transform_2d(a))
First use transpose (or swapaxes) to bring the desire rows together:
In [268]: a.transpose(1,0,2)
Out[268]:
array([[[ 1, 2, 3],
[ 7, 8, 9]],
[[ 4, 5, 6],
[10, 11, 12]]])
then the reshape follows:
In [269]: a.transpose(1,0,2).reshape(-1,3)
Out[269]:
array([[ 1, 2, 3],
[ 7, 8, 9],
[ 4, 5, 6],
[10, 11, 12]])

Add a numpy array to the beginning or end of each element of another array

I have an array of arrays:
x = array([array([[1, 2],
[3, 4]]),
array([[22, 4],
[ 9, 10],
[ 3, 2]])], dtype=object)
And i have a list of arrays with same length like:
xa = [array([11, 22]), array([33, 44])]
I would like to add, in pure numpy, each element of xa to the end or beginning of x, as follows:
In the end:
result = array([array([[ 1, 2],
[ 3, 4],
[11, 22]]),
array([[22, 4],
[ 9, 10],
[ 3, 2],
[33, 44]])], dtype=object)
In the beginning:
result = array([array([[11, 22],
[ 1, 2],
[ 3, 4]]),
array([[33, 44],
[22, 4],
[ 9, 10],
[ 3, 2]])], dtype=object)
*Numpy version = 1.9.3
assuming you imported numpy like that:
import numpy as np
from numpy import array
(1) not biutifully but this would work:
result = array([np.vstack((x[0], xa[0])), np.vstack((x[1], xa[1]))])
or respectively:
result = array([np.vstack((xa[0], x[0])), np.vstack((xa[1], x[1]))])
(2) better with flexible lengths of the two arrays:
result = array([np.vstack((x[i], xa[i])) for i in range(len(x))])
result = array([np.vstack((xa[i], x[i])) for i in range(len(x))])
(3) a bit more pythonic way of handling this:
result = array([np.vstack((x_i, xa_i)) for (x_i, xa_i) in zip(x, xa)])
result = array([np.vstack((xa_i, x_i)) for (x_i, xa_i) in zip(x, xa)])

Intersperse items of a numpy array

I have a 3 dimensional numpy array similar to this:
a = np.array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]],
[[9, 10],
[11, 12]]])
What I'd like to do is intersperse each 2D array contained inside the outer array to produce this result:
t = np.array([[[1, 2], [5, 6], [9, 10]],
[[3, 4], [7, 8], [11, 12]]])
I could do this in Python like this, but I'm hoping there's a more efficient, numpy version:
t = np.empty((a.shape[1], a.shape[0], a.shape[2]), a.dtype)
for i, x in np.ndenumerate(a):
t[i[1], i[0], i[2]] = x
As #UdayrajDeshmukh said, you can use the transpose method (which, despite the name that evokes the "transpose" operator in linear algebra, is better understood as "permuting the axes"):
>>> t = a.transpose(1, 0, 2)
>>> t
array([[[ 1, 2],
[ 5, 6],
[ 9, 10]],
[[ 3, 4],
[ 7, 8],
[11, 12]]])
The newly created object t is a shallow array looking into a's data with a different permutation of indices. To replicate your own example, you need to copy it, e.g. t = a.transpose(1, 0, 2).copy()
Try the transpose function. You simply change the first two axes.
t = np.transpose(a, axes=(1, 0, 2))

how can I do 2d 3d multiplication

I have two array one is 3d :
np.array([[[1,2,3],[3,2,1]],
[[2,3,2],[1,2,5]]])
and one 2d array :
np.array([[2,3],
[3,4]])
and I want to multiply these two to get
np.array([[[2,4,6],[9,6,3]],
[[6,9,6],[4,8,20]]])
How can I do this using numpy package? Thanks.
Use broadcasting:
In [129]: b[:,:,None] * a
Out[129]:
array([[[ 2, 4, 6],
[ 9, 6, 3]],
[[ 6, 9, 6],
[ 4, 8, 20]]])
With following names:
main = np.array([[[1,2,3],[3,2,1]],
[[2,3,2],[1,2,5]]])
fac = np.array([[2,3],
[3,4]])
It can managed with iteration as follows:
a1 = []
for i in [0,1]:
a2 = []
for j in [0,1]:
a2.append(main[i][j]*fac[i][j])
a1.append(a2)
print(a1)
Output:
[[array([2, 4, 6]), array([9, 6, 3])], [array([6, 9, 6]), array([ 4, 8, 20])]]

How to delete column in 3d numpy array

I have a numpy array that looks like this
[
[[1,2,3], [4,5,6]],
[[3,8,9], [2,9,4]],
[[7,1,3], [1,3,6]]
]
I want it like this after deleting first column
[
[[2,3], [5,6]],
[[8,9], [9,4]],
[[1,3], [3,6]]
]
so currently the dimension is 3*3*3, after removing the first column it should be 3*3*2
You can slice it as so, where 1: signifies that you only want the second and all remaining columns from the inner most array (i.e. you 'delete' its first column).
>>> a[:, :, 1:]
array([[[2, 3],
[5, 6]],
[[8, 9],
[9, 4]],
[[1, 3],
[3, 6]]])
Since you are using numpy I'll mention numpy way of doing this. First of all, the dimension you have specified for the question seems wrong. See below
x = np.array([
[[1,2,3], [4,5,6]],
[[3,8,9], [2,9,4]],
[[7,1,3], [1,3,6]]
])
The shape of x is
x.shape
(3, 2, 3)
You can use numpy.delete to remove a column as shown below
a = np.delete(x, 0, 2)
a
array([[[2, 3],
[5, 6]],
[[8, 9],
[9, 4]],
[[1, 3],
[3, 6]]])
To find the shape of a
a.shape
(3, 2, 2)

Categories