Re: [Numpy-discussion] Most efficient trim of arrays

2010-12-14 Thread Robert Kern
On Tue, Dec 14, 2010 at 12:20, Mark Janikas  wrote:
> Hello All,
>
> I was wondering what the best way to trim an array based on some values I do
> not want….  I could use NUM.where or NUM.take… but let me give you an
> example:
>
> import numpy as NUM
>
> n = 100 (Length of my dataset)
> data = NUM.empty((n,), float)
> badRecords = []
> for ind, record in enumerate(records):
>     if record == someValueIDOntWant:
>     badRecords.append(ind)
>     else:
>     data[ind] = record
>
> Now, I want to “trim” my array using badRecords.  I guess I want to avoid
> copying.  Any thoughts on the best way to do it?  I do not want to use lists
> and then subsequently array the result as it is nice to pre-allocate the
> space.

Don't fear the copy. Use boolean indexing.

http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#boolean

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] Most efficient trim of arrays

2010-12-14 Thread Mark Janikas
Hello All,

I was wondering what the best way to trim an array based on some values I do 
not want  I could use NUM.where or NUM.take... but let me give you an 
example:

import numpy as NUM
n = 100 (Length of my dataset)
data = NUM.empty((n,), float)
badRecords = []
for ind, record in enumerate(records):
if record == someValueIDOntWant:
badRecords.append(ind)
else:
data[ind] = record


Now, I want to "trim" my array using badRecords.  I guess I want to avoid 
copying.  Any thoughts on the best way to do it?  I do not want to use lists 
and then subsequently array the result as it is nice to pre-allocate the space.

Thanks much,

MJ


___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion