Having problem with concatenate function in python3 : need at least one array to concatenate

Viewed 212
face_data =[]
labels = []

class_id = 0
names = {}

for fx in os.listdir(dataset_path):
    if fx.endswith('.npy'):
        data_item = np.load(dataset_path+fx)
        face_data.append(data_item)

        target = class_id*np.ones((data_item.shape[0],))
        class_id+=1
        labels.append(target)

face_dataset = np.concatenate(face_data,axis=0)   #error here
face_labels = np.concatenate(labels,axis=0).reshape((-1,1))

print(face_dataset.shape)
print(face_labels.shape)

trainset = np.concatenate((face_dataset,face_labels),axis=1)

I am getting this error: need at least one array to concatenate.
Is there a difference in working of concatenate function in python versions? Am I missing some arguments? concatenate function worked fine on the pc in my online lecture.

2 Answers

The error you are getting usually happens when the array you try to concatenate is empty. Have you checked face_data before concatenating?

This is what np.concatenate does.It join a sequence of arrays along an existing axis.

>>> x = np.array([1,2])
>>> y = np.array([3,4])
>>> z = np.concatenate((x,y))
>>> print(x)
[1 2]
>>> print(y)
[3 4]
>>> print(z)
[1 2 3 4]

You are passing in only one array which is probably a 0d array.That is why you are getting the error.

I think what you need there is np.array(face_data)

Related