Macter
Macter

Reputation: 132

Initialize empty vector with 3 dimensions

I want to initialize an empty vector with 3 columns that I can add to. I need to perform some l2 norm distance calculations on the rows after I have added to it, and I'm having the following problem.

I start with an initial empty array:

accepted_clusters = np.array([])

Then I add my first 1x3 set of values to this:

accepted_clusters = np.append(accepted_clusters, X_1)

returning:

[ 0.47843416  0.50829221  0.51484499]

Then I add a second set of 1x3 values in the same way, and I get the following:

[ 0.47843416  0.50829221  0.51484499  0.89505277  0.8359252   0.21434642]

However, what I want is something like this:

[ 0.47843416  0.50829221  0.51484499]
[ 0.89505277  0.8359252   0.21434642]
.. and so on

This would enable me to calculate distances between the rows. Ideally, the initial empty vector would be of undefined length, but something like a 10x3 of zeros would also work if the code for that is easy.

Upvotes: 0

Views: 256

Answers (2)

Alon Gelber
Alon Gelber

Reputation: 123

You can try using vstack to add rows.

accepted_clusters=np.vstack([accepted_clusters,(0.89505277,  0.8359252,   0.21434642)])

Upvotes: 1

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 95948

The most straightforward way is to use np.vstack:

In [9]: arr = np.array([1,2,3])

In [10]: x = np.arange(20, 23)

In [11]: arr = np.vstack([arr, x])

In [12]: arr
Out[12]:
array([[ 1,  2,  3],
       [20, 21, 22]])

Note, your entire approach has major code smell, doing the above in a loop will give you quadratic complexity. Perhaps you should work with a list and then convert to an array at the end (which will at least be linear-time). Or maybe rethink your approach entirely.

Or, as you imply, you could pre-allocate your array:

In [18]: result = np.zeros((10, 3))

In [19]: result
Out[19]:
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

In [20]: result[0] = x

In [21]: result
Out[21]:
array([[ 20.,  21.,  22.],
       [  0.,   0.,   0.],
       [  0.,   0.,   0.],
       [  0.,   0.,   0.],
       [  0.,   0.,   0.],
       [  0.,   0.,   0.],
       [  0.,   0.,   0.],
       [  0.,   0.,   0.],
       [  0.,   0.,   0.],
       [  0.,   0.,   0.]])

Upvotes: 3

Related Questions