4

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)
2
  • x = np.append(x, np.array([1, 2]).reshape(1,2), axis=0) Commented Jun 4, 2020 at 16:14
  • when i run this from numpy import * x = empty((2, 2), int) x = append(x, array([1, 2]).reshape((1, 2)), axis=0) x = append(x, array([3, 5]).reshape((1, 2)), axis=0) print(x) i get this result [[30945408 0] [ 16 16] [ 1 2] [ 3 5]] what i want is only third and forth line [ 1 2] [ 3 5]] please help me Commented Jun 5, 2020 at 16:36

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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]

Comments

2

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)

1 Comment

i want 2d value i am getting this [22493056 0 16 16 1 2] and i don't want 22493056 0 16 16
2

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]])

Comments