Transpose 3D Numpy Array - python-3.x

Trying to transpose each numpy array in my numpy array.
Here is an example of what I want:
A:
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
A Transpose:
[[[ 1 4]
[ 2 5]
[ 3 6]]
[[ 7 10]
[ 8 11]
[ 9 12]]]
Tried doing this using np.apply_along_axis function but was not getting the correct results.I am trying to apply this to a very large array and any help would be greatly appreciated!
A=np.arange(1,13).reshape(2,2,3)
A=np.apply_along_axis(np.transpose, 0, A)

You need to swap the second and third axises, for which you can use either np.swapaxes:
A.swapaxes(1,2)
#array([[[ 1, 4],
# [ 2, 5],
# [ 3, 6]],
# [[ 7, 10],
# [ 8, 11],
# [ 9, 12]]])
or transpose:
A.transpose(0,2,1)
#array([[[ 1, 4],
# [ 2, 5],
# [ 3, 6]],
# [[ 7, 10],
# [ 8, 11],
# [ 9, 12]]])

For the sake of completeness, there are is also moveaxis which replaces the deprecated rollaxis:
>>> np.rollaxis(A, 2, 1)
array([[[ 1, 4],
[ 2, 5],
[ 3, 6]],
[[ 7, 10],
[ 8, 11],
[ 9, 12]]])
>>> np.moveaxis(A, 2, 1)
array([[[ 1, 4],
[ 2, 5],
[ 3, 6]],
[[ 7, 10],
[ 8, 11],
[ 9, 12]]])

The transformation you seek:
A = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
solution = np.array([A[0].T,A[1].T])

Related

What's CellArray.GetNumberOfCells()?

I create a CellArray but no matter the input of it, it's GetNumberOfCells() is always 3.
Why the result is 3?
Can I get the real number of cells?
Here is the test code.
import vtk
import numpy as np
from vtk.util.numpy_support import numpy_to_vtkIdTypeArray
def calc_num_cells(cell_ids):
cell_ids = np.concatenate(cell_ids)
cell_array = vtk.vtkCellArray()
cell_array.SetCells(vtk.VTK_LINE,
numpy_to_vtkIdTypeArray(cell_ids))
print(cell_array.GetNumberOfCells())
calc_num_cells(
[
[4, 0, 1, 2, 3],
[2, 4, 5],
[2, 6, 7],
[2, 8, 9],
[2, 10, 11],
]
)
# output: 3
calc_num_cells(
[
[4, 0, 1, 2, 3],
]
)
# output: 3
I just use SetCells in wrong way.
The first param of SetCells should be the num_cells.

Sampling from a 2d numpy array

I was wondering if there was a reasonably efficient way of sampling from a 2d numpy array. If I have a generic array:
dims = (4,4)
test_array = np.arange(np.prod(dims)).reshape(*dims)
test_array
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
Then I'd like to randomly set, say, two elements from it to a specific value (let's say 100). I've tried creating an indexing array and then applying that:
sample_from = np.random.randint(low=0, high=5, size=(2,2))
sample_from
array([[0, 2],
[1, 1]])
But if I try using this to index, it gives me a slightly unexpected answer:
test_array[sample_from]
array([[[ 0, 1, 2, 3],
[ 8, 9, 10, 11]],
[[ 4, 5, 6, 7],
[ 4, 5, 6, 7]]])
What I would have expected (and the kind of result I'd like) is if I'd just entered the indexing array directly:
test_array[[0,2],[1,1]] = 100
test_array
giving:
array([[ 0, 100, 2, 3],
[ 4, 5, 6, 7],
[ 8, 100, 10, 11],
[ 12, 13, 14, 15]])
Any help gratefully received.
You could use np.random.choice + np.unravel_index to assign directly to your array.
test_array[
np.unravel_index(np.random.choice(np.prod(dims), 2, replace=False), dims)
] = 100

Python list unique

How can you access the element '5' from this list = [0 ,[ 6, 3], [4, 6, [7, 5] ] ] ?
The options we have :
1) return list[1][2]
2) return list[len(list)-1][1]
3) return list[2][len(list[2])-1][1]
4) list[2][2][2]
List:
[0 ,[ 6, 3], [4, 6, [7, 5] ] ]
list[2]:
[4, 6, [7, 5] ]
list[2][2]:
[7, 5]
list[2][2][1]:
5
list[2][len(list[2])-1][1] == list[2][2][1]

Transform a column into specific rows using numpy

I am trying to extract a column and arrange into multiple rows.
My Input: data
-2.74889,1.585,223.60
-2.74889,1.553,228.60
-2.74889,1.423,246.00
-2.74889,1.236,249.10
-2.74889,0.928,243.80
-2.74889,0.710,242.20
-2.74889,0.558,243.50
...
...
...
k = np.reshape(data[:,2], (2,10))
Output:
[[ 223.6 228.6 246. 249.1 243.8 242.2 243.5 244. 244.8
245.2 ]
[ 224.6 230. 250.7 249.3 244.4 242.1 242.8 243.8 244.7
245.1 ]]
My question is how to add square brackets for each number(for example 223.6) and remain them in 1 row?
Thanks,
Prasad.
It's not entirely clear what you mean, but perhaps it's something like this?
>>> import numpy as np
>>> data = np.arange(30).reshape(10,3)
>>> data
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17],
[18, 19, 20],
[21, 22, 23],
[24, 25, 26],
[27, 28, 29]])
>>> data[:, 2, None]
array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17],
[20],
[23],
[26],
[29]])
You need to expand the dimensions of the array when you reshape.
Setup
x = np.arange(60).reshape(20, 3)
reshape with an additional dimension
x[:, 2].reshape((-1, 10, 1))
expand_dims with axis=2
np.expand_dims(x[:, 2].reshape(-1, 10), axis=2)
atleast_3d
np.atleast_3d(x[:, 2].reshape(-1, 10))
All three produce:
array([[[ 2],
[ 5],
[ 8],
[11],
[14],
[17],
[20],
[23],
[26],
[29]],
[[32],
[35],
[38],
[41],
[44],
[47],
[50],
[53],
[56],
[59]]])

concentrate 2 arrays of vectors

Hi I have 2 arrays of vectors:
A=np.array([[5,62,7],[5,62,7],[5,62,7]])
B=np.array([[1,2,3],[1,2,3],[1,2,3]])
and I would like to concentrate them like that:
C=[[[5,62,7], [1,2,3]],
[[5,62,7], [1,2,3]],
[[5,62,7], [1,2,3]]]
The newish stack makes this easy:
In [130]: A=np.array([[5,62,7],[5,62,7],[5,62,7]])
...: B=np.array([[1,2,3],[1,2,3],[1,2,3]])
...:
In [131]: np.stack((A,B), axis=1)
Out[131]:
array([[[ 5, 62, 7],
[ 1, 2, 3]],
[[ 5, 62, 7],
[ 1, 2, 3]],
[[ 5, 62, 7],
[ 1, 2, 3]]])
It adds an extra dimension to each of the arrays, and then concatenates. With axis=0 is behave just like np.array.
np.array((A,B)).transpose(1,0,2)
joins them on a new 1st axis, and then moves it over.
hstack().reshape() to the rescue:
import numpy as np
A=np.array([[5,62,7],[5,62,7],[5,62,7]])
B=np.array([[1,2,3],[1,2,3],[1,2,3]])
c = np.hstack((A,B)).reshape(3,2,3)
print(c)
Output:
[[[ 5 62 7] [ 1 2 3]]
[[ 5 62 7] [ 1 2 3]]
[[ 5 62 7] [ 1 2 3]]]
hstack
reshape

Resources