0

I'm trying add some numpy arrays into a single array, my code looks like:

m1=symarray('', 2)
for i in range(0,len(countersum)):
  if countersum[i]==1:
    m1.append(gmcounter[i])

This give error

AttributeError: 'numpy.ndarray' object has no attribute 'append'

I have also tried changing append to vstack but it gives the same error

If I modify the last line to have m1=gcounter[i] it works but only selects the first element of gcounter meeting the condition, and disregards everything afterwards.

Does anyone know how I can resolve this?

I have seen the thread Append a NumPy array to a NumPy array but I am unable to declare what I need to append as a numpy array beforehand.

Many thanks

Community
  • 1
  • 1
user124123
  • 1,642
  • 7
  • 30
  • 50
  • You cannot modify the size of a numpy array. You **must** create a new bigger array. numpy should be used when you want to vectorize operations; it isn't a magical library that speeds up operations on sequences, independently of what they are. If you want omogeneous arrays of variable size use the `array` module in the stdlib. – Bakuriu May 14 '13 at 14:49
  • @Bakuriu I'm using them as symbols not matrices with numeric values, so i need to use symarray, I think the ones in the stdlib won't work for my purpose. Do you know if there is any way I can do the append function for the symarray? – user124123 May 14 '13 at 15:00
  • As I said *there is no way to append a value to a numpy array*. Create a new array with size increased by one and add the new value. By the way, the `vstack` is a function in the `numpy` package, *not* a method of `ndarray`. – Bakuriu May 14 '13 at 15:19

1 Answers1

2

@Bakuriu is correct, you can not extend a numpy array without copying. However, depending on the application, you can just convert the numpy array to a list and manipulate it from there:

m1 = sympy.symarray('', 2)

m2 = list(m1)
x = sympy.symbols('x')
m2.append(x)

print m2

This gives

>>> [_0, _1, x]
Hooked
  • 84,485
  • 43
  • 192
  • 261
  • How Would I do this in reverse i.e m1=[], then fill m1 with values, and then declare it to be a sympy array? – user124123 May 14 '13 at 15:23
  • A symarray is simply a helper function that creates a (numpy) array filled with prefixed symbols http://nullege.com/codes/search/sympy.symarray. There is no such thing as a "sympy array", only an iterable filled with sympy objects. So yes, create an empty list and fill it with the sympy objects (symbols) as you see fit. – Hooked May 14 '13 at 15:28
  • I have just tried filling m1 using append as fr a list, then declaring it as a symarray, `m1=symarray(m1,2)` and its returning : `[[[0, 2], [2, 0]]_0 [[0, 2], [2, 0]]_1]`. This is cryptic to me, is there a way to get it to return [[2,0],[0,2]]?? – user124123 May 14 '13 at 15:33
  • @TariqRobinMuman I don't think I was clear enough in my last comment. Make a list `m=[]` and add symbols to it via `m.append(sympy.symbol('x')` where `'x'` is whatever you want to call your symbol. Symarray just creates symbols named `_0,_1,_2` by default. – Hooked May 14 '13 at 15:55
  • ah this is a problem as I want the symbols to be [2,0] and [0,2] (referring to indices of another list) for example, those elements selected from gcounter[i] that meet conditions in the if loop. – user124123 May 14 '13 at 16:03
  • @TariqRobinMuman Then create the symbols as you see fit. The original question, I think, has been answered, "Appending list elements to a symarray". If you need help with making a specific set of symbols a new question might be better, in that case you'll need to provide more information with the _exact_ problem you are having. Be concise, but give enough information for us to help! – Hooked May 14 '13 at 16:20