[Numpy-discussion] Resize method

Colin J. Williams cjw@ncf...
Mon Nov 23 18:54:44 CST 2009


Christopher Barker wrote:
> Colin J. Williams wrote:
>> Access by the interpreter prevents array resizing.
>
> yup -- resize is really fragile for that reason. It really should be 
> used quite sparingly.
>
> Personally, I think it should probably only be used when wrapped with 
> a higher level layer.
>
> I've been working on an extendable array class, I call an accumulator 
> (bad name...). The idea is that you can use it to accumulate values 
> when you don't know how big it's going to end up, rather than using a 
> list for this, which is the standard idiom.
>
> In [2]: import accumulator
>
> In [3]: a = accumulator.accumulator((1,2,3,4,))
>
> In [4]: a
> Out[4]: accumulator([1, 2, 3, 4])
>
> In [5]: a.append(5)
>
> In [6]: a
> Out[6]: accumulator([1, 2, 3, 4, 5])
>
> In [8]: a.extend((6,7,8,9))
> In [9]: a
> Out[9]: accumulator([1, 2, 3, 4, 5, 6, 7, 8, 9])
>
>
> At the moment, it only support 1-d arrays, though I'd like to extend 
> it to n-d, probably only allowing growing on the first axis.
>
> This has been discussed on this list a fair bit, with mixed reviews as 
> to whether there is any point. It's slower than lists in common usage, 
> but has other advantages -- I'd like to see a C version, but don't 
> know if I'll ever have the time for that.
>
> I've enclosed to code for your viewing pleasure
>
> -Chris
>
Thanks for this.  My aim is to extract a row of data from a line in a 
file and append it to an array.  The number of columns is fixed but, at 
the start, the number of rows is unknown.

I think that I have sorted out the resize approach but I need more tests 
before I share it.

Your accumulator idea is interesting.  Back in 2004, I worked on 
MyMatrix, based on numarray - abandoned when numpy came onto the scene.

One of the capabilities there was an /append/ method, intended to add a 
conforming matrix to the right or below the given matrix.  It was 
probably not efficient but it provided a means of joining together block 
matrices,

The append signature, from a January 2005 backup is here:

      def append(self, other, toRight= False):
        '''
        Return self, with other appended, to the Right or Below,
    default: Below.

        other - a matrix, a list of matrices,
                or objects which can be converted into matrices.   
       
        '''
        assert self.iscontiguous()
        assert self.rank == 2
        if isinstance(other, _n.NumArray):
          ...

Colin W.


More information about the NumPy-Discussion mailing list