1

I'm trying to append a one dimensional numpy array to a two dimensional, so the one dimensional one is inserted on the place of another x-value.

Example:

all_polys = [[5,6],[8,9]] (before the error down below there is nothing stored in it yet)

poly = [1,2]

Expected Result:

all_polys = [[5,6],[8,9],[1,2]]

My Code:

all_polys = numpy.array([[]])
poly = np.expand_dims(poly, axis=0)
print(poly)
print(all_polys)
all_polys = np.concatenate(all_polys, poly)

The Error:

TypeError: only integer scalar arrays can be converted to a scalar index

Print Output before error:

[['400' '815' '650' '815' '650' '745' '400' '745']] (poly with added dimension)

[] (all_polies)

This really frustrates me. What I am doing wrong? It must be a little detail I overlooked, I guess.

2
  • Does all_polys start as a list [[5,6],[8,9], as an array, np.array([[5,6,8,9]]), or as this useless thing np.array([[]])? Is poly a list [1,2] or an array, np.array([1,2])? Commented Mar 9, 2019 at 20:02
  • all_polys started as that "useless thing" and poly was an array filled with a flexible amount of numbers. I changed it, so that all_polys becomes redundant. Instead a list is initiated, with a certain amount of elements, to which i append more lists. Commented Mar 9, 2019 at 21:17

4 Answers 4

2

Starting with a 2d array, and a 1d array:

In [26]: all_polys = np.array([[5,6],[8,9]])                                    
In [27]: poly = np.array([1,2])                                                 

vstack does a nice job of making sure all inputs are 2d, and then concatenating:

In [28]: np.vstack((all_polys, poly))                                           
Out[28]: 
array([[5, 6],
       [8, 9],
       [1, 2]])

You had the right ides with expand_dims:

In [29]: np.concatenate((all_polys, np.expand_dims(poly, axis=0)))              
Out[29]: 
array([[5, 6],
       [8, 9],
       [1, 2]])

But the np.array([[]]) is a poor starting point. Why use that? Are you doing this iteratively?

For iterative work we recommend using lists:

In [30]: alist = []                                                             
In [31]: alist.append([5,6])                                                    
In [32]: alist.append([8,9])                                                    
In [33]: alist.append([1,2])                                                    
In [34]: np.array(alist)                                                        
Out[34]: 
array([[5, 6],
       [8, 9],
       [1, 2]])

I discourage the use of np.append. It is misused too often.

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

1 Comment

@hpaulj: Nice. Was surprised by your use of vstack with a mix of 2d and 1d array. Expected it to throw an error, as it doesn't seem to satisfy the rule "The arrays must have the same shape along all but the first axis" -- they aren't even of the same rank.
0

You can try the append function instead of expand_dims

import numpy as np
all_polys = [[5,6],
             [8,9]]
all_polys = np.append(all_polys,[ [1,2] ], axis=0)
print(all_polys)
#Output=
#all_polys = [[5,6],
#             [8,9],
#             [1,2]]

Comments

0

You just need to do this:

all_polys = np.concatenate((all_polys, poly[None,:]), axis=0)

The two arrays we are concatenating are all_polys, which looks like [[5,6],[8,9]], and poly[None,:], which looks like [[1.2]].

By axis=0, we're specifying that the concatenation must happen along the outermost (first) dimension of these arrays.

2 Comments

Is there also a way to do that to arrays with a different ammount of saved entries? Like all_polys = [[1,4]] and poly = [7,8,4,3] to create [[1,4],[7,8,4,3]]? I get a value error: all the input array dimensions except for the concatenation axis must match exactly
For the example in your question the arrays already have a different number of stored elements. all_polys has 4 elements, and poly has 2 elements. I think the question in your comment now is about flattening the arrays before concatenating. For that you can use something like my_flat = np.concatenate((all_polys.ravel(), poly.ravel()), axis=0). BTW, if you don't pass axis=0, the default of 0 will be used by concatenate(), which will work just fine for us.
0

you should do like this.

arr = [old array]
newArr = numpy.append(arr, [new_array])

Use of append function will works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.