1

I'm having a very hard time appending my x_ array to x while maintaining the correct shape. I tried vstack, but it gave me an error. The axis=0 doesn't seem to be doing anything like it's supposed to. I want an array with dimension (:,len(x_)).

Edit:

The code at the end of the post gives arrays x_ of the following shape:

array([3, 0, 2, 1, 0], dtype=int32) 

I tried:

x_ = np.append(x_,np.array([5,4,6,7,8]), axis = 0)

But gives:

array([3, 0, 2, 1, 0, 5, 4, 6, 7, 8])

However, I want:

array([[3, 0, 2, 1, 0],
       [5, 4, 6, 7, 8]])

I tried vstack(x,x_), but got:

   x = np.vstack(x, x_)

TypeError: vstack() takes 1 positional argument but 2 were given

.

for k in range(2,9):
    temp_ = (2*k)+1
    x = np.zeros(shape=(1,temp_))
    y = []
    for i in range(k, len(number_list)-k-1):
        newk = k
        x_ = []
        while newk >= -k:
            x_.append(name[i-newk])
            newk-=1
        le = preprocessing.LabelEncoder()
        le.fit(x_)
        x_ = le.transform(x_)
        x = np.append(x, x_ , axis=0)
        y.append(residue_area[i])
4
  • 2
    Can you update your question with the full traceback of the error? Commented Apr 18, 2017 at 6:45
  • Please add your input data and the desired result. Commented Apr 18, 2017 at 6:57
  • Appending to a list is faster and less error prone. But if you must join arrays, use the basic np.concatenate. It requires correct array shapes, and doesn't try to compensate or second guess your intentions. Commented Apr 18, 2017 at 7:09
  • Please see the edit. Thanks. Commented Apr 18, 2017 at 7:49

2 Answers 2

4

np.append must be abolished; it distorts the thinking of too many beginners.

np.concatenate and all the np.?stack take a list as the first argument. The only other argument is an axis keyword. np.append changes that convention by replacing that list with 2 arguments - which it then tweaks, puts in a list and calls concatenate. And unless you read the source code you don't know for sure how it modifies the dimensions first.

Pull up an interactive session, and play with np.concatenate until you understand how the dimensions must match. Then revisit this problem.

edit: spelling error concatenate instead of concatenante

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

2 Comments

Thanks, but one question remains. By creating a zeros array with certain shape, I have to create a zeros first entry, which I don't want. How can I create an empty array of certain shape?
If you MUST go the iterative concatenate route, you could start with np.zeros(shape=(0,temp_)). Getting the starting array right is part of why I dislike np.append. Starting a list append with [] is a well known Python idiom. The numpy equivalent is awkward. Much better to collect ALL subarrays in a list, and do concatenate just once.
2

But you can use np.vstack:

>>> import numpy as np
>>> x_ = np.array([3, 0, 2, 1, 0]) 
>>> x_ = np.vstack([x_, np.array([5, 4, 6, 7, 8])])
>>> x_
array([[3, 0, 2, 1, 0],
       [5, 4, 6, 7, 8]])

But generally it's a bad idea (because it's really inefficient) to append or stack arrays regularly. Often it's better to create an array of the final shape and insert into it:

>>> x_ = np.empty((2, 5), dtype=int)
>>> x_[0] = [3, 0, 2, 1, 0]
>>> x_[1] = [5, 4, 6, 7, 8]
>>> x_
array([[3, 0, 2, 1, 0],
       [5, 4, 6, 7, 8]])

2 Comments

Thanks, vstack worked for me, but one question remains. By creating a zeros array with certain shape, I have to create a zeros first entry, which I don't want. How can I create an empty array of certain shape?
No, don't create an empty array and append to it. Like I said that's a very inefficient solution (and I mean terrible inefficient). You can either use a normal list [] and append to it and convert it to an array after you finished appending or you create an empty array of the final shape and then insert into it.

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.