Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7178895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:00:58+00:00 2026-05-28T17:00:58+00:00

I have an object of type ‘numpy.ndarray’, called myarray, that when printed to the

  • 0

I have an object of type ‘numpy.ndarray’, called “myarray”, that when printed to the screen using python’s “print”, looks like hits

[[[ 84   0 213 232] [153   0 304 363]]
 [[ 33   0  56 104] [ 83   0  77 238]]
 [[ 0  0  9 61] [ 0  0  2 74]]]

“myarray” is made by another library. The value of myarray.shape equals (3, 2). I expected this to be a 3dimensional array, with three indices. When I try to make this structure myself, using:

second_array = array([[[84, 0, 213, 232], [153, 0, 304, 363]],
 [[33, 0, 56,  104], [83,  0, 77,  238]],
 [[0,  0, 9,   61],  [0,   0,  2, 74]]])

I get that second_array.shape is equal to (3, 2, 4), as expected. Why is there this difference? Also, given this, how can I reshape “myarray” so that the two columns are merged, i.e. so that the result is:

[[[ 84   0 213 232 153   0 304 363]]
 [[ 33   0  56 104  83   0  77 238]]
 [[ 0  0  9 61  0  0  2 74]]]

Edit: to clarify, I know that in the case of second_array, I can do second_array.reshape((3,8)). But how does this work for the ndarray which has the format of myarray but does not have a 3d index?

myarray.dtype is “object” but can be changed to be ndarray too.

Edit 2: Getting closer, but still cannot quite get the ravel/flatten followed by reshape. I have:

a = array([[1, 2, 3],
       [4, 5, 6]])
b = array([[ 7,  8,  9],
       [10, 11, 12]])
arr = array([a, b])

I try:

arr.ravel().reshape((2,6))

But this gives [[1, 2, 3, 4, 5, 6], ...] and I wanted [[1, 2, 3, 7, 8, 9], ...]. How can this be done?

thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:00:59+00:00Added an answer on May 28, 2026 at 5:00 pm

    Indeed, ravel and hstack can be useful tools for reshaping arrays:

    import numpy as np
    
    myarray = np.empty((3,2),dtype = object)
    myarray[:] = [[np.array([ 84,   0, 213, 232]), np.array([153, 0, 304, 363])],
     [np.array([ 33,   0,  56, 104]), np.array([ 83,   0,  77, 238])],
     [np.array([ 0, 0,  9, 61]), np.array([ 0,  0,  2, 74])]]
    
    myarray = np.hstack(myarray.ravel()).reshape(3,2,4)
    print(myarray)
    # [[[ 84   0 213 232]
    #   [153   0 304 363]]
    
    #  [[ 33   0  56 104]
    #   [ 83   0  77 238]]
    
    #  [[  0   0   9  61]
    #   [  0   0   2  74]]]
    
    myarray = myarray.ravel().reshape(3,8)
    print(myarray)
    # [[ 84   0 213 232 153   0 304 363]
    #  [ 33   0  56 104  83   0  77 238]
    #  [  0   0   9  61   0   0   2  74]]
    

    Regarding Edit 2:

    import numpy as np
    
    a = np.array([[1, 2, 3],
           [4, 5, 6]])
    b = np.array([[ 7,  8,  9],
           [10, 11, 12]])
    arr = np.array([a, b])
    print(arr)
    # [[[ 1  2  3]
    #   [ 4  5  6]]
    
    #  [[ 7  8  9]
    #   [10 11 12]]]
    

    Notice that

    In [45]: arr[:,0,:]
    Out[45]: 
    array([[1, 2, 3],
           [7, 8, 9]])
    

    Since you want the first row to be [1,2,3,7,8,9], the above shows that you want the second axis to be the first axis. This can be accomplished with the swapaxes method:

    print(arr.swapaxes(0,1).reshape(2,6))
    # [[ 1  2  3  7  8  9]
    #  [ 4  5  6 10 11 12]]
    

    Or, given a and b, or equivalently, arr[0] and arr[1], you could form arr directly with the hstack method:

    arr = np.hstack([a, b])
    # [[ 1  2  3  7  8  9]
    #  [ 4  5  6 10 11 12]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object of type Hash that I want to loop over via
I have an object of the type IEnumerable<KeyValuePair<T,U>> keyValueList , I am using var
I have an object of type CGPDFDictionaryRef returned somehow from a method that is
Suppose you have an object of type BlahChild that is an extension of BlahParent,
I have a certain object type that is stored in a database. This type
I have created an object type.. and after initilializing I am pushing that into
I have an object called Settings that inherits from NSMutableDictionary . When I try
I have an object of Type MyTypeOneViewModel that is displayed in the first column
Have one object of type A that is related to a bunch of objects
I have two questions. I have an object here that is of type ArrayList

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.