0

Giving this code:

import numpy as np
array1 = []

for i in range(10):
    array1 = np.vstack((array1,i))

Is there a way to do this without having to treat the first case specifically? In my real code i have to iterate over 100000 times, so It feels to me that including something like this:

import numpy as np
array1 = []

for i in range(10):
    if np.size(array1)==0:
        array1 = np.append(array1,i)
    else:
        array1 = np.vstack((array1,i))

would bé a very ineficient way to solve it.

Is there anyway of making this step more eficient?

Edit: The code is just a simplifyed version of the code, here im just vtacking simple numbers. In the real code I stack arrays of 2 values so final array gives something like:

out_array = [[0.5,0.1],[0.2,0.1],...]
xgrimau
  • 664
  • 8
  • 19
  • `vstack` is inefficient. Use list append if need to do it 1000 times. – hpaulj Oct 30 '16 at 15:48
  • What are you trying to accomplish? – wwii Oct 30 '16 at 15:49
  • @wwii this is an example code, the purpose of the real one is to stack features from a text mining function and have an array of features as output. Something like 'output_array = [[0.2,0.1],[0.34 , 0.7], ...]' – xgrimau Oct 30 '16 at 16:01
  • Your 1st code block raises a `ValueError` !!! — I haven't tested the second block. What do you want to do? I could try to guess, but it's you that should explain better your intention… – gboffi Oct 30 '16 at 16:05
  • Do you know how big the final array will be in advance? – wwii Oct 30 '16 at 16:05
  • @gboffi Yup, it raises it because you can't vstack [] and [1,2], One way of solving it is with the code in the second block, the question is if there is any other way to do this. – xgrimau Oct 30 '16 at 16:47
  • Just append to a list and do `np.array([[0.5,0.1],[0.2,0.1],...]) at the end. – hpaulj Oct 30 '16 at 18:18
  • @hpaulj yeah, read your answer in the post you said, thank you! – xgrimau Oct 30 '16 at 19:07

0 Answers0