Fastest way to grow a numpy numeric array

后端 未结 5 2111
后悔当初
后悔当初 2020-11-27 12:01

Requirements:

  • I need to grow an array arbitrarily large from data.
  • I can guess the size (roughly 100-200) with no guarantees that the array will fit
5条回答
  •  有刺的猬
    2020-11-27 12:48

    I tried a few different things, with timing.

    import numpy as np
    
    1. The method you mention as slow: (32.094 seconds)

      class A:
      
          def __init__(self):
              self.data = np.array([])
      
          def update(self, row):
              self.data = np.append(self.data, row)
      
          def finalize(self):
              return np.reshape(self.data, newshape=(self.data.shape[0]/5, 5))
      
    2. Regular ol Python list: (0.308 seconds)

      class B:
      
          def __init__(self):
              self.data = []
      
          def update(self, row):
              for r in row:
                  self.data.append(r)
      
          def finalize(self):
              return np.reshape(self.data, newshape=(len(self.data)/5, 5))
      
    3. Trying to implement an arraylist in numpy: (0.362 seconds)

      class C:
      
          def __init__(self):
              self.data = np.zeros((100,))
              self.capacity = 100
              self.size = 0
      
          def update(self, row):
              for r in row:
                  self.add(r)
      
          def add(self, x):
              if self.size == self.capacity:
                  self.capacity *= 4
                  newdata = np.zeros((self.capacity,))
                  newdata[:self.size] = self.data
                  self.data = newdata
      
              self.data[self.size] = x
              self.size += 1
      
          def finalize(self):
              data = self.data[:self.size]
              return np.reshape(data, newshape=(len(data)/5, 5))
      

    And this is how I timed it:

    x = C()
    for i in xrange(100000):
        x.update([i])
    

    So it looks like regular old Python lists are pretty good ;)

提交回复
热议问题