Python Forum
How convert multidimensional array to two dimensional array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How convert multidimensional array to two dimensional array
#1
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])
]
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 292 Mar-26-2024, 02:18 PM
Last Post: snippsat
  boolean array: looking for all rows where all is True paul18fr 4 1,175 Jan-04-2023, 09:58 PM
Last Post: paul18fr
  reshaping 2D numpy array paul18fr 3 970 Jan-03-2023, 06:45 PM
Last Post: paul18fr
  Turn list of arrays into an array of lists Cola_Reb 6 1,661 Jul-20-2022, 06:55 PM
Last Post: Cola_Reb
Question how to write a function that accepts a 1D array as a parameter in Python SuperNinja3I3 1 1,552 Jul-02-2022, 01:55 PM
Last Post: Larz60+
  replace sets of values in an array without using loops paul18fr 7 1,629 Jun-20-2022, 08:15 PM
Last Post: paul18fr
  how to parse this array with pandas? netanelst 1 1,299 May-17-2022, 12:42 PM
Last Post: netanelst
  semantics of comma inside array brackets usercat123 2 1,324 Apr-23-2022, 09:08 AM
Last Post: usercat123
  RandomForest --ValueError: setting an array element with a sequence JaneTan 0 1,707 Sep-08-2021, 02:12 AM
Last Post: JaneTan
  Keep inner Values of 2D array timste 0 1,541 Jul-26-2021, 09:04 AM
Last Post: timste

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020