Python Forum
reshaping 2D numpy array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reshaping 2D numpy array
#1
Hi

Does somebody have a more efficient way to reshape the following M matrix?

The only way i've found so far is to extract 2 intermediate matrixes before concatenating them, but I'm persuaded one can do it in a more elegant way.

Thanks for any advice

Paul
import numpy as np

M = np.array([[0, 0, -1],   # P1
              [0, 0, -1], 
              [0, 0, 0], 
              [0, 0, 0],    # P2
              [0, 0, 1],
              [0, 0, 1]])

r, c = np.shape(M) 
k = 2

Target = np.array([[0, 0, -1, 0, 0, 0, 0, 0, 1],
                   [0, 0, -1, 0, 0, 0, 0, 0, 1]])

# M1 = M.reshape(k, c*(r//k))
# Test1 = np.array_equal(Target, M1)

# M2 = M.reshape(k, c*(r//k), order='C')
# Test2 = np.array_equal(Target, M2)

# M3 = M.reshape(k, c*(r//k), order='F')
# Test3 = np.array_equal(Target, M3)

# M4 = M.reshape(k, c*(r//k), order='A')
# Test4 = np.array_equal(Target, M4)

i = np.arange(0, r, k)
j = np.arange(1, r, k)
M5 = np.vstack((M[i, :].reshape(1, (r*c//k)), M[j, :].reshape(1, (r*c//k))))

Test5 = np.array_equal(Target, M5)
print(f"M5 = {M5}")
print(f"Test5 = {Test5}")
Reply
#2
>>> import numpy as np
>>> M = np.array([
...               [0, 0, -1],   # P1
...               [0, 0, -1], 
...               [0, 0, 0], 
...               [0, 0, 0],    # P2
...               [0, 0, 1],
...               [0, 0, 1]
... ])
>>> r, c = np.shape(M) 
>>> X = np.reshape(M, (r,c))
>>> Target = np.reshape(X, (2,9))
>>> Target
array([[ 0,  0, -1,  0,  0, -1,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  1,  0,  0,  1]])
>>>
EDIT: Sorry, This is not correct!
Reply
#3
You can grab indices and using vstack and ravel get desired result:

import numpy as np

arr = np.array([[0, 0, -1],   
              [0, 0, -1], 
              [0, 0, 0], 
              [0, 0, 0],    
              [0, 0, 1],
              [0, 0, 1]])

arr_2 = np.vstack((arr[::2].ravel(), arr[1::2].ravel()))

# arr_2
[[ 0  0 -1  0  0  0  0  0  1]
 [ 0  0 -1  0  0  0  0  0  1]]
But this feels too brute-force. Therefore alternative could be preferred:

arr_3 = arr.reshape(-1, 2, 3).swapaxes(0, 1).reshape(2, -1)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Thanks for all replies.

@Perfringo: interesting solution using 3D arrays; i'll do tests and metrics to figure out how it works and how I can use it for huge arrays

Paul
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 631 Mar-26-2024, 02:18 PM
Last Post: snippsat
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,642 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  Numpy array BrianPA 13 4,989 Jan-23-2021, 09:36 AM
Last Post: Serafim
  Reshaping matrix paul18fr 2 1,853 Jan-05-2021, 04:26 PM
Last Post: paul18fr
  How to fill datetime64 field in numpy structured array? AlekseyPython 0 2,294 Oct-20-2020, 08:17 AM
Last Post: AlekseyPython
  Adding data in 3D array from 2D numpy array asmasattar 0 2,236 Jul-23-2020, 10:55 AM
Last Post: asmasattar
  converting dataframe to int numpy array glennford49 1 2,330 Apr-04-2020, 06:15 AM
Last Post: snippsat
  Replacing sub array in Numpy array ThemePark 5 4,199 Apr-01-2020, 01:16 PM
Last Post: ThemePark
  How to prepare a NumPy array which include float type array elements subhash 0 1,927 Mar-02-2020, 06:46 AM
Last Post: subhash
  numpy.where array search for string in just one coordinate adetheheat 1 2,293 Jan-09-2020, 07:09 PM
Last Post: paul18fr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020