<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Dec 23, 2015 at 4:01 AM, Nicolas P. Rougier <span dir="ltr"><<a href="mailto:Nicolas.Rougier@inria.fr" target="_blank">Nicolas.Rougier@inria.fr</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Typed list in numpy would be a nice addition indeed and your cython implementation is nice (and small).<br></blockquote><div><br></div><div>It seems we have a lot of duplicated effort here. Pernonally, I have two needs:</div><div><br></div><div>1) ragged arrays</div><div>2) "growable" arrays. </div><div><br></div><div>I have semi-complete version of both of these, which are completely independent -- not sure if it makes sense to combine them, I suppose not.</div><div><br></div><div>But we've talked a bit about  "typed list", and I'm not sure what that means -- is it something that is entirely like a python list, except that all the elements have the same type? </div><div><br></div><div>Anyway: I've been thinking about this fromt eh opposite direction: I want a numpy array that you can append/extend. This comes from the fact that it's not uncommon to need to build up an array where you don't know how large it will be when you start. The common recommendation for doing that now is to built it up in a list, and then, when you are done, turn it into an ndarray.</div><div><br></div><div>But that means you are limited to python types (or putting numpy scalars in a list...), and it's not very memory efficient.</div><div><br></div><div>My version used a ndarray internally, and over allocates it a bit, using ndarray.resize() to resize. this means that you can get the data pointer if you want for Cython, etc... but also that it's getting re-allocated, so that pointer is fragile, and you don't want other arrays to have views on it.</div><div><br></div><div>Interestingly, if you are adding one float, for example, at a time to the array, it's actually a bit faster to build it up in a list, and then make an array out of it.</div><div><br></div><div>But it is more memory efficient and faster if you are using numpy dtypes and especially if you are extend()ing it with chunks from other arrays.</div><div><br></div><div>I also have a not-quite finished version in Cython that statically handles the core C data types -- that should be faster, but I haven't really profiled it.</div><div><br></div><div>I'll try to get the code up on gitHub.</div><div><br></div><div>It would be nice to combine efforts.</div><div><br></div><div>-CHB</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
In my case I need to ensure a contiguous storage to allow easy upload onto the GPU.<br>
But my implementation is quite slow, especially when you add one item at a time:<br>
<br>
>>> python benchmark.py<br>
Python list, append 100000 items: 0.01161<br>
Array list, append 100000 items: 0.46854<br>
Array list, append 100000 items at once: 0.05801<br>
Python list, prepend 100000 items: 1.96168<br>
Array list, prepend 100000 items: 12.83371<br>
Array list, append 100000 items at once: 0.06002<br>
<br>
<br>
<br>
I realize I did not answer all Chris' questions:<br>
<span class=""><br>
>>> L = ArrayList( [[0], [1,2], [3,4,5], [6,7,8,9]] )<br>
</span>>>> for item in L: print(item)<br>
<span class="">[0]<br>
[1 2]<br>
[3 4 5]<br>
[6 7 8 9]<br>
<br>
</span>>>> print (type(L.data))<br>
<class 'numpy.ndarray'><br>
>>> print(L.data.dtype)<br>
int64<br>
>>> print(L.data.shape)<br>
(10,)<br>
<br>
<br>
I did not implement operations yet, but it would be a matter for transferring call to the underlying numpy data array.<br>
>>> L._data *= 2<br>
>>> print(L)<br>
[[0], [4 8], [12 16 20], [24 28 32 36]]<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
<br>
> On 23 Dec 2015, at 09:34, Stephan Hoyer <<a href="mailto:shoyer@gmail.com">shoyer@gmail.com</a>> wrote:<br>
><br>
> We have a type similar to this (a typed list) internally in pandas, although it is restricted to a single dimension and far from feature complete -- it only has .append and a .to_array() method for converting to a 1d numpy array. Our version is written in Cython, and we use it for performance reasons when we would otherwise need to create a list of unknown length:<br>
> <a href="https://github.com/pydata/pandas/blob/v0.17.1/pandas/hashtable.pyx#L99" rel="noreferrer" target="_blank">https://github.com/pydata/pandas/blob/v0.17.1/pandas/hashtable.pyx#L99</a><br>
><br>
> In my experience, it's several times faster than using a builtin list from Cython, which makes sense given that it needs to copy about 1/3 the data (no type or reference count for individual elements). Obviously, it uses 1/3 the space to store the data, too. We currently don't expose this object externally, but it could be an interesting project to adapt this code into a standalone project that could be more broadly useful.<br>
><br>
> Cheers,<br>
> Stephan<br>
><br>
><br>
><br>
> On Tue, Dec 22, 2015 at 8:20 PM, Chris Barker <<a href="mailto:chris.barker@noaa.gov">chris.barker@noaa.gov</a>> wrote:<br>
><br>
> sorry for being so lazy as to not go look at the project pages, but....<br>
><br>
> This sounds like it could be really useful, and maybe supercise a coupl eof half-baked projects of mine. But -- what does "dynamic" mean?<br>
><br>
> - can you append to these arrays?<br>
> - can it support "ragged arrrays" -- it looks like it does.<br>
><br>
> >>> L = ArrayList( [[0], [1,2], [3,4,5], [6,7,8,9]] )<br>
> >>> print(L)<br>
> [[0], [1 2], [3 4 5], [6 7 8 9]]<br>
><br>
> so this looks like a ragged array -- but what do you get when you do:<br>
><br>
> for row in L:<br>
>     print row<br>
><br>
><br>
> >>> print(L.data)<br>
> [0 1 2 3 4 5 6 7 8<br>
><br>
> is .data a regular old 1-d numpy array?<br>
><br>
> >>> L = ArrayList( np.arange(10), [3,3,4])<br>
> >>> print(L)<br>
> [[0 1 2], [3 4 5], [6 7 8 9]]<br>
> >>> print(L.data)<br>
> [0 1 2 3 4 5 6 7 8 9]<br>
><br>
><br>
> does an ArrayList act like a numpy array in other ways:<br>
><br>
> L * 5<br>
><br>
> L* some_array<br>
><br>
> in which case, how does it do broadcasting???<br>
><br>
> Thanks,<br>
><br>
> -CHB<br>
><br>
> >>> L = ArrayList(["Hello", "world", "!"])<br>
> >>> print(L[0])<br>
> 'Hello'<br>
> >>> L[1] = "brave new world"<br>
> >>> print(L)<br>
> ['Hello', 'brave new world', '!']<br>
><br>
><br>
><br>
> Nicolas<br>
><br>
> _______________________________________________<br>
> NumPy-Discussion mailing list<br>
> <a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
> <a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
><br>
><br>
><br>
><br>
> --<br>
><br>
> Christopher Barker, Ph.D.<br>
> Oceanographer<br>
><br>
> Emergency Response Division<br>
> NOAA/NOS/OR&R            <a href="tel:%28206%29%20526-6959" value="+12065266959">(206) 526-6959</a>   voice<br>
> 7600 Sand Point Way NE   <a href="tel:%28206%29%20526-6329" value="+12065266329">(206) 526-6329</a>   fax<br>
> Seattle, WA  98115       <a href="tel:%28206%29%20526-6317" value="+12065266317">(206) 526-6317</a>   main reception<br>
><br>
> <a href="mailto:Chris.Barker@noaa.gov">Chris.Barker@noaa.gov</a><br>
><br>
> _______________________________________________<br>
> NumPy-Discussion mailing list<br>
> <a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
> <a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R            (206) 526-6959   voice<br>7600 Sand Point Way NE   (206) 526-6329   fax<br>Seattle, WA  98115       (206) 526-6317   main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a></div>
</div></div>