使用NumPy

时间:2018-09-03 16:36:59

标签: python arrays numpy matrix

编辑,其中包含有关数据结构的更多详细信息。 (请注意,即使这似乎是一个重复的问题,这也是从数组到矩阵转换的特殊情况。)

在下面的代码末尾,对同一组数据执行了许多操作之后,我通过将数组列表转换为3D数组来构建了最终的3D数组(位于代码的最后几行,注释中指定的详细信息。)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import glob

big_list = [] #list used to store all data built from multiple .txt files.



for a in range(1,21): #loop to open and take data from multiple .txt files.
    for s in range(1,11): 
        for e in range(1,4): 
            #specify path (with vars from loop) 
            path = '/home/user/plots/Files/a{0}_s{1}_e{2}_skeleton.txt'.format(a, s,e) 
            archive = glob.glob(path)
            #if the .txt file's opened, starts operations with data taken from it.      
            if (archive): 
                movement = np.loadtxt(path)
                num_skeletons = int(len(movement)/20)
                list_for_array = [] #list to take all data from the file.
                for i in range(num_skeletons): 
                    #list_for_array is transformed in a 2D array:
                    list_for_array.append(movement[(i*20):((i+1)*20),:3]) 
                #that same 2D array is made into a 3D array:
                coords_array= np.array(list_for_array) 


                # a 3D matrix is made from the 3D array. 
                dist_matrix = np.zeros((num_skeletons,20,20))           
                for k in range(num_skeletons):
                    for i in range(20):
                        for j in range(20):
                            dist_matrix[k,i,j] = np.linalg.norm(coords_array[k,i,:] - coords_array[k,j,:])
                    #end of the 3D matrix construction. 

                #the 3D matrix is made into a list of 2D array (each array with 1 line and 400 columns).
                for m in range(num_skeletons):

                    array = dist_matrix[m].reshape((1,400))
                    #the m 2D arrays are added to a list: 
                    big_list.append(array)
                #dist_matrix = None 

#now, big_list has all the data from all .txt files (many 2D arrays).
big_array = np.array(big_list) #and finally, big_list is made into a 3D array, or, an array made of many 2D arrays.

我没有解释代码的深层含义是什么,因为它会花费很长时间,并且这里的目的是要知道:

如何将最终的3D数组(big_array)转换为2D矩阵?

0 个答案:

没有答案
相关问题