Python Forum
reshaping 2D numpy array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reshaping 2D numpy array
#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


Messages In This Thread
reshaping 2D numpy array - by paul18fr - Jan-01-2023, 06:31 PM
RE: reshaping 2D numpy array - by Larz60+ - Jan-01-2023, 08:42 PM
RE: reshaping 2D numpy array - by perfringo - Jan-01-2023, 11:05 PM
RE: reshaping 2D numpy array - by paul18fr - Jan-03-2023, 06:45 PM

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