Numpy 2d array extrude

有些话、适合烂在心里 提交于 2019-12-25 01:45:33

问题


I am new to numpy ndarrays and i couldn`t find any solution for my issue. I have say 10 files of floating point data. I apply some operation for every pair of files, that returns 1D array.

What I want is to have block matrix A[10x10] with rows and cols are my ten files and every element in that matrix is block of 1D array that results my operation applied to f_i and f_j.

I gues i need some kind of map, so that i could tell "This f_i and f_j result in certain array" and could access this array by f_i, f_j.

What would be the best way to achive this? Endpoint of that task is to output this matrix into csv file.

Data schema:


回答1:


Maybe you can accomplish your goal just using a nested list (https://docs.python.org/3.7/tutorial/datastructures.html#nested-list-comprehensions):

# build a 10x10 matrix with default value 0
matrix = [[0 for i in range(10)] for j in range(10)]
# assign the result to a cell
matrix[1][1] = ['result', 'of', 'some', 'operation']
# retrieve the result
print (matrix[1][1])
#=> ['result', 'of', 'some', 'operation']



回答2:


You may use np.append method in numpy. You can check the details in numpy.append




回答3:


I think you could do this pretty cleanly with a dictionary like so:

file_pairs_table = {}
file_a = "file_a.txt"
file_b = "file_b.txt"
file_pairs_table[(file_a,file_b)] = np.arange(999) #operation resulting in 1d array here.

Then access the value of the file pair like this:

file_pairs_table[(file_a,file_b)]
>>> array([0,1,...,998])


来源:https://stackoverflow.com/questions/53239103/numpy-2d-array-extrude

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!