python :need at least one array to concatenate

#!/usr/bin/python
#-*- coding:cp936 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
def load_file(filepath):
    dataframe = pd.read_csv(filepath, header=None, delim_whitespace=True)
    return dataframe.values

def load_dataset(data_rootdir, dirname, group):
    '''
    该函数实现将训练数据或测试数据文件列表堆叠为三维数组
    '''
    filename_list = []
    filepath_list = []
    X = []
    
    # os.walk() 方法是一个简单易用的文件、目录遍历器,可以高效的处理文件、目录。
    for rootdir, dirnames, filenames in os.walk(data_rootdir + dirname):
        for filename in filenames:
            filename_list.append(filename)
            filepath_list.append(os.path.join(rootdir, filename))
        #print(filename_list)
        #print(filepath_list)
    
    # 遍历根目录下的文件,并读取为DataFrame格式;
    for filepath in filepath_list:
        X.append(load_file(filepath))
    
    X = np.dstack(X) # dstack沿第三个维度叠加,两个二维数组叠加后,前两个维度尺寸不变,第三个维度增加;
    y = load_file(data_rootdir+'/y_'+group+'.txt')
    print('{}_X.shape:{},{}_y.shape:{}\n'.format(group,X.shape,group,y.shape))
    return X, y

train_rootdir = 'F:/桌面/毕设/python/wanzheng/data/train/'
test_rootdir = 'F:/桌面/毕设/python/wanzheng/data/test/'
data_dirname = '/Inertial Signals/'
trainX, trainy = load_dataset(train_rootdir, data_dirname, 'train')
testX, testy = load_dataset(test_rootdir, data_dirname, 'test')

 

 

如果传给np.dstack()的参数是一个空的列表或元组,就会抛出题主给出的错误信息。建议在数组深度合并前,检查一下X的长度,大概率是空的。

>>> import numpy as np
>>> np.dstack([])
Traceback (most recent call last):
  File "<pyshell#65>", line 1, in <module>
    np.dstack([])
  File "<__array_function__ internals>", line 6, in dstack
  File "C:\Users\xufive\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\lib\shape_base.py", line 723, in dstack
    return _nx.concatenate(arrs, 2)
  File "<__array_function__ internals>", line 6, in concatenate
ValueError: need at least one array to concatenate