numpy.insert() function insert array into wrong in

2019-04-16 17:26发布

问题:

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: create 2d array using getting data of 2 folder

step5(here problem arise): create a row in 2d array ind inser id array

import numpy as np
import array as arr
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=','))")

    #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
    if array_control_var==1:

        #here use np.array()
        main_f_array=np.array([main_f_array,f_array])
    else:
        main_f_array=np.insert(main_f_array, array_control_var, f_array, 0)

    array_control_var+=1

print(main_f_array)

I want output like this

Initial [[0,0,0],[0,0,0,]]

after insert [[0,0,0],[0,0,0],[0,0,0]]

but the out put is

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

回答1:

As I commented, collecting arrays with insert (or variations on concatenate) is hard to do right, and slow when working. It builds a whole new array each time. Collecting the arrays in a list, and doing one array build at the end is easier and faster. List append is efficient, and easy to use.

That said, your reported result looks suspicious. I can reproduce it with:

In [281]: arr = np.zeros(2, object)
In [282]: arr
Out[282]: array([0, 0], dtype=object)
In [283]: arr[0] = np.array([0,0,0])
In [284]: arr[1] = np.array([0,0,0])
In [285]: arr
Out[285]: array([array([0, 0, 0]), array([0, 0, 0])], dtype=object)
In [286]: np.insert(arr, 2, np.array([0,0,0]), 0)
Out[286]: array([array([0, 0, 0]), array([0, 0, 0]), 0, 0, 0], dtype=object)

At an earlier iteration, main_f_array must have been created as an object dtype array.

If it had been a 'normal' 2d array, the insert would be different:

In [287]: arr1 = np.zeros((2,3),int)
In [288]: np.insert(arr1, 2, np.array([0,0,0]), 0)
Out[288]: 
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]])

Or more iteratively as I think you wanted:

In [289]: f_array = np.array([0,0,0])
In [290]: main = f_array
In [291]: main = np.array([main, f_array])
In [292]: main
Out[292]: 
array([[0, 0, 0],
       [0, 0, 0]])
In [293]: main = np.insert(main, 2, f_array, 0)
In [294]: main
Out[294]: 
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]])


回答2:

When I recommend replacing the insert with a list build, here's what I have in mind.

import numpy as np

alist = []
for i in range(4): 
    f_array = np.array([i, i+2, i+4])
    alist.append(f_array)

print(alist)
main_f_array = np.array(alist)

print(main_f_array)

test run:

1246:~/mypy$ python3 stack54715610.py 
[array([0, 2, 4]), array([1, 3, 5]), array([2, 4, 6]), array([3, 5, 7])]
[[0 2 4]
 [1 3 5]
 [2 4 6]
 [3 5 7]]

If your file loading produces arrays that differ in size you'll get different results

f_array = np.arange(i, i+1+i)

1246:~/mypy$ python3 stack54715610.py 
[array([0]), array([1, 2]), array([2, 3, 4]), array([3, 4, 5, 6])]
[array([0]) array([1, 2]) array([2, 3, 4]) array([3, 4, 5, 6])]

This is a 1d object dtype array, as opposed to the 2d.