0

I have n arrays of shape

(86,) 

I have an empty array

complete = np.array([])

To which I want to append these n arrays. When I do it right now using this

complete = np.append(complete, feature_1)

when I keep appending the array shape changes to (172,) , (258)... etc , but I would like to append in such a way that it goes like this (1, 86), (2, 86) , (3, 86) etc.. resulting in the final shape I would like (n, 86).

Any suggestions on how I may be able to achieve this would be helpful. Thanks in advance.

13
  • Efficiency is not really on my priority list at the moment, getting this done is Commented Jul 7, 2018 at 8:06
  • @ roganjosh Will look into it Commented Jul 7, 2018 at 8:07
  • 1
    I think you want vstack Commented Jul 7, 2018 at 8:07
  • Post a small example of your shape array. Commented Jul 7, 2018 at 8:11
  • @kabanus the shape of my array is (86,) , and i have 128 of those, So i would like to loop through these array's and get shape of (128, 86), hope im clear here Commented Jul 7, 2018 at 8:11

1 Answer 1

2

I guess you'd better create an empty 2D array and fill it later...

import numpy as np

N = how_many()
L = how_long()

complete = np.empty((N, L), dtype=float)

for i in range(N):
    complete(i, :) = extract_feature(i)

In place of dtype=float you can use a different numeric type, as required by your problem.

E.g.,

In [14]: a = np.empty((2,4), dtype=float)

In [15]: for i in (0, 1):
    ...:     a[i,:] = np.ones(4)*i
    ...:     

In [16]: a
Out[16]: 
array([[0., 0., 0., 0.],
       [1., 1., 1., 1.]])

Addendum

Comment on efficiency   If one knows beforehand the dimensions of an array they are going to construct row-by-row, the approach sketched above is better, because it avoids constantly alllocating new memory, copying the temporary result and the new row to the new memory and deallocating the memory used to hold the previous temporary result.

Alternative using concatenation   If, on the other hand, one doesn't know beforehand how many rows will be produced during the creation of the array, or if one insists on using a sub-optimal solution, they can use np.vstack, encapsulating the productions of new rows in a generator

import numpy as np

def features_factory(stuff):
    while true:
        feature = new_feature(stuff)
        if feature:
            yield feature
        else:
            return

complete = np.vstack(features_factory(todays_stuff))

E.g.,

In [1]: import numpy as np

In [2]: np.random.seed((2018+7+8)) # today's stuff... ;)

In [3]: def features_factory(stuff):
   ...:     n = 0
   ...:     while True:
   ...:         if n<stuff:
   ...:             yield np.ones(5)*n
   ...:             n = n+1
   ...:         else:
   ...:             return        

In [4]: complete = np.vstack(features_factory(np.random.randint(5,11)))

In [5]: complete
Out[5]: 
array([[0., 0., 0., 0., 0.],
       [1., 1., 1., 1., 1.],
       [2., 2., 2., 2., 2.],
       [3., 3., 3., 3., 3.],
       [4., 4., 4., 4., 4.],
       [5., 5., 5., 5., 5.],
       [6., 6., 6., 6., 6.],
       [7., 7., 7., 7., 7.]])

In [6]: 
Sign up to request clarification or add additional context in comments.

4 Comments

let me check this out
this solution Works
Not only it Works, it's also efficient because one doesn't go through, iteration after iteration, to 1. allocating new memory 2. copying what has be done so far and 3. marking for garbage collection the memory that was previously used...
Hi , you think what i want to achieve can be done using a concatenation operation too ?, i mean as i loop keep concatenating the array's?

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.