user13626513
user13626513

Reputation:

How to create an empty array and append it?

I am new to programming. I am trying to run this code

from numpy import *

x = empty((2, 2), int)

x = append(x, array([1, 2]), axis=0)

x = append(x, array([3, 5]), axis=0)

print(x)

But i get this error

Traceback (most recent call last):
  File "/home/samip/PycharmProjects/MyCode/test.py", line 3, in <module>

    x = append(x, array([1, 2]), axis=0)

  File "<__array_function__ internals>", line 5, in append

  File "/usr/lib/python3/dist-packages/numpy/lib/function_base.py", line 4700, in append

    return concatenate((arr, values), axis=axis)

  File "<__array_function__ internals>", line 5, in concatenate

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)

Upvotes: 4

Views: 44523

Answers (4)

Mikycid
Mikycid

Reputation: 111

as you can see in the error your two arrays must match the same shape, x.shape returns (2,2), and array([1,2]).shape returns (2,) so what you have to do is

x = np.append(x, np.array([1,2]).reshape((1,2)), axis=0)

Printing x returns :

array([[1.966937e-316, 4.031792e-313],
   [0.000000e+000, 4.940656e-324],
   [1.000000e+000, 2.000000e+000]])

Upvotes: 1

hpaulj
hpaulj

Reputation: 231335

I suspect you are trying to replicate this working list code:

In [56]: x = []                                                                 
In [57]: x.append([1,2])                                                        
In [58]: x                                                                      
Out[58]: [[1, 2]]
In [59]: np.array(x)                                                            
Out[59]: array([[1, 2]])

But with arrays:

In [53]: x = np.empty((2,2),int)                                                
In [54]: x                                                                      
Out[54]: 
array([[73096208, 10273248],
       [       2,       -1]])

Despite the name, the np.empty array is NOT a close of the empty list. It has 4 elements, the shape that you specified.

In [55]: np.append(x, np.array([1,2]), axis=0)                                  
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-55-64dd8e7900e3> in <module>
----> 1 np.append(x, np.array([1,2]), axis=0)

<__array_function__ internals> in append(*args, **kwargs)

/usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py in append(arr, values, axis)
   4691         values = ravel(values)
   4692         axis = arr.ndim-1
-> 4693     return concatenate((arr, values), axis=axis)
   4694 
   4695 

<__array_function__ internals> in concatenate(*args, **kwargs)

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)

Note that np.append has passed the task on to np.concatenate. With the axis parameter, that's all this append does. It is NOT a list append clone.

np.concatenate demands consistency in the dimensions of its inputs. One is (2,2), the other (2,). Mismatched dimensions.

np.append is a dangerous function, and not that useful even when used correctly. np.concatenate (and the various stack) functions are useful. But you need to pay attention to shapes. And don't use them iteratively. List append is more efficient for that.

When you got this error, did you look up the np.append, np.empty (and np.concatenate) functions? Read and understand the docs? In the long run SO questions aren't a substitute for reading the documentation.

Upvotes: 2

pykam
pykam

Reputation: 1481

The issue is that the line x = empty((2, 2), int) is creating a 2D array.

Later when you try to append array([1, 2]) you get an error because it is a 1D array.

You can try the code below.

from numpy import *

x = empty((2, 2), int)

x = append(x,[1,2])

print(x)

Upvotes: 1

Viewed
Viewed

Reputation: 1413

You can create empty list by []. In order to add new item use append. For add other list use extend.

x = [1, 2, 3]
x.append(4)
x.extend([5, 6])

print(x) 
# [1, 2, 3, 4, 5, 6]

Upvotes: 1

Related Questions