numpy.insert()函数将数组插入错误的索引 [英] numpy.insert() function insert array into wrong index
问题描述
在这里,我的代码专长于表单文本文件;并将矩阵创建为多维数组,但是问题是代码创建了比二维数组更多的代码,我无法操纵,我需要二维数组,我该怎么做?
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?
解释我的代码的算法:
代码Moto:我的代码从特定的文件夹中获取值,每个文件夹包含一个用户生成的7个"txt"文件,这样,多个文件夹将包含多个用户的多个数据.
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.
步骤1:启动第一个for循环,并使用特定文件夹中的文件夹数量来控制它,并在变量"path"中存储第一个文件夹的第一个路径.
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.
第2步:使用2nd for loop打开路径并获取7个txt文件的数据.专长之后,它关闭2nd for loop并执行其余代码.
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.
步骤3:将1个1d数组中的7个txt文件的数据连接起来.
step3: Concat the data of 7 txt file in one 1d array.
第4步:使用获取2个文件夹的数据创建2d数组
step4: create 2d array using getting data of 2 folder
step5(此处会出现问题):在2d数组ind inser id数组中创建一行
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)
我想要这样的输出
初始 [[0,0,0],[0,0,0,]]
Initial [[0,0,0],[0,0,0,]]
插入后 [[0,0,0],[0,0,0],[0,0,0]]
after insert [[0,0,0],[0,0,0],[0,0,0]]
但是输出是
[array([0, 0, 0])
array([0, 0, 0])
0 0 0]
推荐答案
当我建议用列表构建替换insert
时,这就是我的想法.
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)
试运行:
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])]
这是一个1d对象dtype数组,与2d相反.
This is a 1d object dtype array, as opposed to the 2d.
这篇关于numpy.insert()函数将数组插入错误的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!