Python: Split a 2d Array Vertically and Convert It into a 3d Array

This post shows how to split a 2d array into subsets of 2d arrays and convert them into a 3d array.

The following figure shows what we want to do in this post.

Exercise with code snippets

For example, let’ consider m which is a 2d array.

# slice vertically 2d array and convert them to 3d array
 
import numpy as np
 
m = np.array([[ 1, 2, 3, 4, 5, 6],
              [11,12,13,14,15,16],
              [21,22,23,24,25,26],
              [31,32,33,34,35,36],
              [41,42,43,44,45,46]])
m
array([[ 1,  2,  3,  4,  5,  6],
       [11, 12, 13, 14, 15, 16],
       [21, 22, 23, 24, 25, 26],
       [31, 32, 33, 34, 35, 36],
       [41, 42, 43, 44, 45, 46]])

The following code examples split m (2d array) vertically into 2, 3, or 6 sub 2d arrays and concatenate them as a 3d array. The third argument of reshape() function is the number of sub columns. swapaxes(0,1) swaps .shape[0] and .shape[1].

The only thing we do is just to select the number of sub columns.

# slice vertically 2d array and convert them to 3d array
 
#1
m.reshape(m.shape[0],-1, 3).swapaxes(0,1)
#2
m.reshape(m.shape[0],-1, 2).swapaxes(0,1)
#3
m.reshape(m.shape[0],-1, 1).swapaxes(0,1)
#1
array([[[ 1,  2,  3],
        [11, 12, 13],
        [21, 22, 23],
        [31, 32, 33],
        [41, 42, 43]],
       [[ 4,  5,  6],
        [14, 15, 16],
        [24, 25, 26],
        [34, 35, 36],
        [44, 45, 46]]])
#2
array([[[ 1,  2],
        [11, 12],
        [21, 22],
        [31, 32],
        [41, 42]],
       [[ 3,  4],
        [13, 14],
        [23, 24],
        [33, 34],
        [43, 44]],
       [[ 5,  6],
        [15, 16],
        [25, 26],
        [35, 36],
        [45, 46]]])
#3
array([[[ 1],
        [11],
        [21],
        [31],
        [41]],
 
       [[ 2],
        [12],
        [22],
        [32],
        [42]],
 
       [[ 3],
        [13],
        [23],
        [33],
        [43]],
 
       [[ 4],
        [14],
        [24],
        [34],
        [44]],
 
       [[ 5],
        [15],
        [25],
        [35],
        [45]],
 
       [[ 6],
        [16],
        [26],
        [36],
        [46]]])

Originally posted on SH Fintech Modeling blog.

Join the Discussion

Thank you for engaging with IBKR Campus. If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.

Your email address will not be published. Required fields are marked *

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from SHLee AI Financial Model and is being posted with its permission. The views expressed in this material are solely those of the author and/or SHLee AI Financial Model and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.