Python Forum
How convert multidimensional array to two dimensional array - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: How convert multidimensional array to two dimensional array (/thread-16143.html)



How convert multidimensional array to two dimensional array - tkkhan44 - Feb-15-2019

Here, my code feats value form text file; and create matrices as multidimensional array, but the problem is the code create more then two dimensional array, that I can't manipulate, I need two dimensional array, how I do that?

Explain algorithm of my code:

**Moto of code:**
My code fetch value from a specific folder, each folder contain 7 'txt' file, that generate from one user, in this way multiple folder contain multiple data of multiple user.

step1: Start a 1st for loop, and control it using how many folder have in specific folder,and in variable 'path' store the first path of first folder.

step2: Open the path and fetch data of 7 txt file using 2nd for loop.after feats, it close 2nd for loop and execute the rest code.

step3: Concat the data of 7 txt file in one 1d array.

step4(Here the problem arise): Store the 1d arry of each folder as 2d array.end first for loop.


Code:

    import numpy as np
    from array import *
    import os
    f_path='Result'
    array_control_var=0
    
    #for feacth directory path
    for (path,dirs,file) in os.walk(f_path):
        if(path==f_path):
            continue
        f_path_1= path +'\page_1.txt'
        #Get data from page1 indivisualy beacuse there string type data exiest
        pgno_1 = np.array(np.loadtxt(f_path_1, dtype='U', delimiter=','))
        
        #only for page_2.txt
        f_path_2= path +'\page_2.txt'
        with open(f_path_2) as f:
            str_arr = ','.join([l.strip() for l in f])
        pgno_2 = np.asarray(str_arr.split(','), dtype=int)
    
        #using loop feach data from those text file.datda type = int
        for j in range(3,8):
        
        #store file path using variable
            txt_file_path=path+'\page_'+str(j)+'.txt'
            
        
            if os.path.exists(txt_file_path)==True:
        
                #genarate a variable name that auto incriment with for loop
                foo='pgno_'+str(j)
            else:
                break
        
            #pass the variable name as string and store value
            exec(foo + " = np.array(np.loadtxt(txt_file_path, dtype='i', delimiter=','))")
        
        #z=np.array([pgno_2,pgno_3,pgno_4,pgno_5,pgno_6,pgno_7])
        
        
        
        
        #marge all array from page 2 to rest in single array in one dimensation
        f_array=np.concatenate((pgno_2,pgno_3,pgno_4,pgno_5,pgno_6,pgno_7), axis=0)
        
        #for first time of the loop assing this value
        if array_control_var==0:
            main_f_array=f_array
        else:
            #here the problem arise 
            main_f_array=np.array([main_f_array,f_array])
        array_control_var+=1
    print(main_f_array)
current my code generate array like this(for 3 fo>>lder)

[
array([[0,0,0],[0,0,0]]),
array([0,0,0])
]

Note: I don't know how many dimension it have

But I want

[
array(
[0,0,0]
[0,0,0]
[0,0,0])
]


RE: How convert multidimensional array to two dimensional array - scidam - Feb-20-2019

I am not sure I understand you correctly, but
if you want to convert
[
array([[0,0,0],[0,0,0]]),
array([0,0,0])
]
to
array(
[0,0,0]
[0,0,0]
[0,0,0])
you can do this using numpy.vstack function, e.g.

import numpy as np
sample_data = [np.random.rand(np.random.randint(2, 4), 3) for s in range(10)]
sample_data.append(np.array([5,6,7]))
Ok, sample_data consists of arrays of different size with common number of columns 3,
so these arrays could be stacked using np.vstack:

np.vstack(sample_data)