reshaping 3D matrix into 2D matrix using tensorflow - python

I have a 3D matrix of dimensions, 549x19x50 I need to create a 2D matrix which gets me a 549x950 matrix.
What i did so far is using tensorflow;
#data_3d is the 3D matrix
data_2d = tf.reshape(data_3d,[549,-1])
This prints out all the values of data_3d in the prompt and when I try to access data_2d it gives me an NameError
data_3d is a list of list of lists. Not a tensor or a ndarray. If we cant do this for lists, is there any way to easily convert lists to ndarrays?
Thanks in advance,
Bhashithe

There is a simple way to do so using numpy:
import numpy as np
data_3d = np.arange(27).reshape((3,3,3))
data_2d = data_3d.swapaxes(1,2).reshape(3,-1)
Ouput:
data_2d
[[ 0 3 6 1 4 7 2 5 8]
[ 9 12 15 10 13 16 11 14 17]
[18 21 24 19 22 25 20 23 26]]
print data_3d
[[[ 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]]]
Note: swapaxes(1,2) is the main thing here - you need to define which axes you want to swap.

Related

Split Horizontally Python Array Manipulation

How can I write a function named split which accepts three parameters a, b, c and then do the following.
create a n dimensional array 'x' having first a natural numbers (use np.arange method).
change the shape of x to (c, b) and assign to new array y.
split the array y horizontally into two arrays, then assign it to i and j.
display i and j.
I tried using hsplit and array_split methods and then assign it to i and j. But the output is not matching as given below.
import numpy as np
x=np.arange(20)
y = np.array(x)
z= y.reshape(10,2)
#a = np.hsplit(z,2)
(a,b)=np.array_split(z,2,axis=0)
print(a)
print(b)
Actual output:-
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
[[10 11]
[12 13]
[14 15]
[16 17]
[18 19]]
Desired output:-
[[ 0 1 2 3 4]
[10 11 12 13 14]]
[[ 5 6 7 8 9]
[15 16 17 18 19]]
You were right with hsplit, the problem is just the shape is the other way around to get the desired output:
import numpy as np
x=np.arange(20)
y = np.array(x)
z= y.reshape(2,10)
a,b = np.hsplit(z,2)
print(a)
print(b)
output:
[[ 0 1 2 3 4]
[10 11 12 13 14]]
[[ 5 6 7 8 9]
[15 16 17 18 19]]

numpy vectorized resampling like pandas DataFrame resample

I have an (4, 2000) numpy array and want to resample each column (N=4) for every 5 elements with such as max, min, left, right, which makes its shape as (4, 400).
I can do with Pandas.DataFrame using .resample('5Min').agg(~) or with numpy array and for loop like result = [max(input[i:i+5]) for i in range(0, len(input), 5)]. However, it takes amount of time with large input array since it's not vectorized. Is there any way that I can do with vectorized computation with np array?
Here is another way that uses numpy strides under the hood (a is your array):
from skimage.util import view_as_blocks
a = view_as_blocks(a, (4,5))
Now, you can use methods/slicing for parameters you want:
#max
a.max(-1)[0].T
#min
a.min(-1)[0].T
#left
a[...,0][0].T
#right
a[...,-1][0].T
example:
a
#[[ 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]
# [30 31 32 33 34 35 36 37 38 39]]
output for max
#[[ 4 9]
# [14 19]
# [24 29]
# [34 39]]

NUMPY: select even lines, last column

I am trying to learn numpy and I can't manage to complete this question: take the even lines, last column of the M matrix:
[[ 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 30]]
What I did : print(M[0:, -1, 2], '\n')
error: IndexError: too many indices for array
Why isn't this working ? I select all the lines with 0:, the last column with -1, with step 2.
Your array is 2-dimensional, but you're using three indices as if your array had 3 dimensions, you can use this index to get what you want:
print(M[::2, -1])
Output:
[ 5 15 25]

Deleting certain elements from a matrix

I have the following problem:
I have a matrix. Now, I want to delete one entry in each row of the matrix: In rows that contain a certain number (say 4) I want to delete the entry with that number, and in other rows I simply want to delete the last element.
E.g. if I have the matrix
matrix=np.zeros((2,2))
matrix[0,0]=2
matrix[1,0]=4
matrix
which gives
2 0
4 0
after the deletion it should simply be
2
0
thanks for your help!
so, assuming there's maximum only one 4 in a row, what you want to do is:
iterate all rows, and if there's a four use roll so it becomes the last element
delete the last column
in rows that have 4, it will delete this 4 and shift the remaining values that come after it,
in rows that don't have 4, it will delete the last element.
(I took the liberty of trying with a little bigger matrix just to make sure output is as expected)
try this:
import numpy as np
# Actual solution
def remove_in_rows(mat, num):
for i, row in enumerate(mat):
if num in row.tolist():
index = row.tolist().index(num)
mat[i][index:] = np.roll(row[index:], -1)
return np.delete(mat, -1, 1)
# Just some example to demonstrate it works
matrix = np.array([[10 * y + x for x in range(6)] for y in range(6)])
matrix[1, 2] = 4
matrix[3, 3] = 4
matrix[4, 0] = 4
print("BEFORE:")
print(matrix)
matrix = remove_in_rows(matrix, 4)
print("AFTER:")
print(matrix)
Output:
BEFORE:
[[ 0 1 2 3 4 5]
[10 11 4 13 14 15]
[20 21 22 23 24 25]
[30 31 32 4 34 35]
[ 4 41 42 43 44 45]
[50 51 52 53 54 55]]
AFTER:
[[ 0 1 2 3 5]
[10 11 13 14 15]
[20 21 22 23 24]
[30 31 32 34 35]
[41 42 43 44 45]
[50 51 52 53 54]]

In Python how do you split a list into evenly sized chunks starting with the last element from the previous chunk?

What would be the most pythonic way to convert a list like:
mylist = [0,1,2,3,4,5,6,7,8]
into chunks of n elements that always start with the last element of the previous chunk.
The last element of the last chunk should be identical to the first element of the first chunk to make the data structure circular.
Like:
[
[0,1,2,3],
[3,4,5,6],
[6,7,8,0],
]
under the assumption that len(mylist) % (n-1) == 0 . So that it always works nicely.
What about the straightforward solution?
splitlists = [mylist[i:i+n] for i in range(0, len(mylist), n-1)]
splitlists[-1].append(splitlists[0][0])
A much less straightforward solution involving numpy (for the sake of overkill):
from numpy import arange, roll, column_stack
n = 4
values = arange(10, 26)
# values -> [10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25]
idx = arange(0, values.size, n) # [ 0 4 8 12]
idx = roll(idx, -1) # [ 4 8 12 0]
col = values[idx] # [14 18 22 10]
values = column_stack( (values.reshape(n, -1), col) )
[[10 11 12 13 14]
[14 15 16 17 18]
[18 19 20 21 22]
[22 23 24 25 10]]

Categories

Resources