0

I am struggling in appending a numpy array to other one..

data_X = load_dataset()
print(data_X.shape) # (6794, 11)
result_array = np.empty((0, 110))

for i in range(0, 1000, 10):
    arr_1d = data_X[i:i+10].reshape(1, 110)
    np.append(result_array, arr_1d, axis=0)

print(result_array.shape)

Not sure what I am doing wrong.. There is no error but shape of result_array is (0,110). Please help.

Avag Sargsyan
  • 2,437
  • 3
  • 28
  • 41
Himanshu
  • 11
  • 2

1 Answers1

2

Unlike list.append, numpy.append does not work in-place but returns a new object. To make your loop work you'd have to reassign to result_array.

Please also note that numpy.append is rather inefficient and should not be used that way.

Recommended ways of achieving the equivalent of your loop include

  • preallocating using np.empty and then using slice assignment
  • appending to a list and using np.concatenate in the end
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
  • Thanks for your help. It worked after re-assignment. result_array=np.append(result_array, arr_1d, axis=0) – Himanshu Feb 21 '18 at 11:44