1

Here is a MWE (minimal-working example) that I have:

import numpy as np

errors = np.array([])
for i in range(7):
    np.append(errors, i)
print(errors)

Unfortunately, the output from Jupyter Notebook is only an empty array, but why?

UJIN
  • 1,648
  • 13
  • 28

2 Answers2

3

To answer your question directly, "why is the created array empty", it's pretty simple: you created an empty array.

errors = np.array([]) # this array is empty!

From the documentation: https://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html

numpy.append(arr, values, axis=None)

Returns: append : ndarray A copy of arr with values appended to axis. Note that append does not occur in-place: a new array is allocated and filled.

Your array errors is only assigned once, you'll need to re-assign the appended array to errors to get the result you are expecting, else it will stay empty.

errors = np.append(errors, i)
Hymns For Disco
  • 7,530
  • 2
  • 17
  • 33
0

Because numpy.append() does not work in-place, i.e. the old object is not overwritten. Try this:

import numpy as np

errors = np.array([])
for i in range(7):
    errors = np.append(errors, i)
print(errors)

I would recommend going with a simple list in your case:

errors = []
for i in range(7):
    errors +=[i]
print(errors)
emilaz
  • 1,722
  • 1
  • 15
  • 31