I want to split the two-dimensional array.

Asked 2 years ago, Updated 2 years ago, 151 views

I would like to divide the 102×102 matrix into 17×17 matrices.
The 102 matrix has the number of the matrix, so
When I read it, I changed it from Dataframe to an array.
I was able to read the 102 matrix, but how should I divide it?
Should I use split for division?
Thank you for your cooperation.

The code to read the matrix is as follows:

dfs=[]
for filename in filenames:
    # print(filename)
    df=pd.read_csv(filename,index_col=0)
    # print(df)

    a_df=df.values
    print(a_df)
    print(a_df.shape)

python array

2022-09-30 21:35

2 Answers

I tried dividing the matrix using only the slicing of numpy.

def func1(arr,block_size,block_num):
    size=block_size*block_num
    result = [ ]
    for row in range (0, size, block_size):
        block=[ ]
        for colin range(0,size,block_size):
            block.append(arr[row:row+block_size, col:col+block_size])
        result.append(block)
    return result

using inclusions
def func2(arr,block_size,block_num):
    size=block_size*block_num
    return [[arr[row:row+block_size, col:col+block_size] for col in range(0,size,block_size)] for row in range(0,size,block_size)]

Google Colab measured 115 ss for func1 and 107 ss for func2 and 470 ss for np.split.


2022-09-30 21:35

a_df is NumPyarray, so I tried to implement np.split to form a block matrix in two stages.

Sample Code:

block_size=6
block_num=17
size=block_size*block_num
arr=np.array([i*j for jin range(size)] for i in range (size)])
h_arr=np.split(arr,block_num,axis=0)
hv_arr = [np.split(elm, block_num,axis=1) for elmin h_arr]

np.split returns a list of NumPyarrays.So I cut them vertically first, and then cut each part vertically to make a list of NumPyarray lists.

Also, I have found that other methods are organized into Slice2d array into smallers, so I will link them for your reference.


2022-09-30 21:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.