[Numpy-discussion] repeat array along new axis without making a copy

2012-02-15 Thread Steve Schmerler
Hi

I'd like to repeat an array along a new axis (like broadcast):

In [8]: a
Out[8]: 
array([[0, 1, 2],
   [3, 4, 5]])
In [9]: b=repeat(a[None,...], 3, axis=0)
In [10]: b
Out[10]: 
array([[[0, 1, 2],
[3, 4, 5]],

   [[0, 1, 2],
[3, 4, 5]],

   [[0, 1, 2],
[3, 4, 5]]])

In [18]: id(a); id(b[0,...]); id(b[1,...]); id(b[2,...])
Out[18]: 40129600
Out[18]: 39752080
Out[18]: 40445232
Out[18]: 40510272


Can I do this such that each sub-array b[i,...] is a view and not a copy?

Background: I'm working on a container class to store trajectory-like
data. The API requires that each array has a time axis (axis=0 here)
along which sub-arrays are stored which may be the same in some cases.
Then, I don't want to store redundant information if possible.

Thanks!

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


Re: [Numpy-discussion] repeat array along new axis without making a copy

2012-02-15 Thread Steve Schmerler
On Feb 15 06:25 -0600, Warren Weckesser wrote:
 Yes, such an array can be created using the as_strided() function from the
 module numpy.lib.stride_tricks:

Thank you, I will look into that.

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


[Numpy-discussion] subtle behavior when subtracting sub-arrays

2010-07-05 Thread Steve Schmerler
Hi

I stumbled upon some numpy behavior which I was not aware of.

Say I have an array of shape (2,2,3) and want to subtract the sub-array
a[...,0] of shape (2,2) from each a[...,i], i=0,1,2 .

## ok ##

In [1]: a=arange(2*2*3).reshape(2,2,3)

# Copy the array to be subtracted.
In [2]: a0=a[...,0].copy()

# Trivial approach. That works.
In [3]: for k in range(a.shape[-1]):
   ...: a[...,k] -= a0
   ...: 
   ...: 

# OK
In [4]: a
Out[4]: 
array([[[0, 1, 2],
[0, 1, 2]],

   [[0, 1, 2],
[0, 1, 2]]])

In [5]: a=arange(2*2*3).reshape(2,2,3)

# The same, with broadcasting.
In [6]: a=a-a[...,0][...,None]

# OK
In [7]: a
Out[7]: 
array([[[0, 1, 2],
[0, 1, 2]],

   [[0, 1, 2],
[0, 1, 2]]])

## not ok ##

In [8]: a=arange(2*2*3).reshape(2,2,3)

In [9]: a-=a[...,0][...,None]

# NOT OK
In [10]: a
Out[10]: 
array([[[ 0,  1,  2],
[ 0,  4,  5]],

   [[ 0,  7,  8],
[ 0, 10, 11]]])

In [11]: a=arange(2*2*3).reshape(2,2,3)

# NOT OK, same as above
In [12]: for k in range(a.shape[-1]):
...: a[...,k] -= a[...,0]
...: 
...: 

In [14]: a
Out[14]: 
array([[[ 0,  1,  2],
[ 0,  4,  5]],

   [[ 0,  7,  8],
[ 0, 10, 11]]])

To sum up, I find it a bit subtle that
a = a - a[...,0][...,None]
works as expected, while
a -= a[...,0][...,None]
does not.
I guess the reason is that in the latter case (and the corresponding
loop), a[...,0] itself is changed during the loop, while in the former
case, numpy makes a copy of a[...,0] ?

Is this intended? This is with numpy 1.3.0.

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


[Numpy-discussion] subclassing ndarray

2008-12-05 Thread Steve Schmerler
Hi all

I'm subclassing ndarray following [1] and I'd like to know if i'm doing
it right. My goals are

- ndarray subclass MyArray with additional methods
- replacement for np.array, np.asarray on module level returning MyArray
  instances
- expose new methods as functions on module level

import numpy as np

class MyArray(np.ndarray):  
  
def __new__(cls, arr, **kwargs):   
return np.asarray(arr, **kwargs).view(dtype=arr.dtype, type=cls)

# define new methods here ...   
  
def print_shape(self):  
  
print self.shape
  

  
# replace np.array()
  
def array(*args, **kwargs): 
  
return MyArray(np.array(*args, **kwargs))   
  

  
# replace np.asarray()
def asarray(*args, **kwargs):   
  
return MyArray(*args, **kwargs) 
  

  
# expose array method as function   
  
def ps(a):  
  
asarray(a).print_shape() 

Would that work?

PS: I found a little error in [1]:

In section __new__ and __init__, the class def should read

class C(object):
def __new__(cls, *args):
+   print 'cls is:, cls
print 'Args in __new__:', args
return object.__new__(cls, *args)
def __init__(self, *args):
+   print 'self is:, self
print 'Args in __init__:', args


[1] http://docs.scipy.org/doc/numpy/user/basics.subclassing.html

best,
steve
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] numpy array change notifier?

2008-10-30 Thread Steve Schmerler
On Oct 27 16:43 -0400, Pierre GM  wrote:
 
 Erik, may be you could try the trick presented here : 
 http://www.scipy.org/Subclasses
 in the __array_wrap__ section.

Stupid question: How do I find pages like
http://www.scipy.org/Subclasses on scipy.org?

I can find them with the search function, but only when I know that it's
there :)

best,
steve
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] numpy array change notifier?

2008-10-30 Thread Steve Schmerler
On Oct 30 10:06 -0400, Pierre GM  wrote:
 Steve,
 Right, there's not a lot of visibility for this one. 

Yes, wouldn't it be better placed in the Cookbook section (or any other
suitable place that's visible to anyone entering scipy.org)?  Other
pages are unfortunately also not visible, like
http://www.scipy.org/EricsBroadcastingDoc .

best,
steve
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] how to save a large array into a file quickly

2008-10-15 Thread Steve Schmerler
On Oct 14 15:29 -1000, Eric Firing  wrote:
 frank wang wrote:
  Hi,
   
  I have a large ndarray that I want to dump to a file. I know that I can 
  use a for loop to write one data at a time. Since Python is a very 
  powerfully language, I want to find a way that will dump the data fast 
  and clean. The data can be in floating point or integer.
 
 Use numpy.save for a single array, or numpy.savez for multiple ndarrays, 
 assuming you will want to read them back with numpy.  If you want to 
 dump to a text file, use numpy.savetxt.  If you want to dump to a binary 
 file to be read by another program, you might want to use the tofile 
 method of the ndarray.
 

I've just updated [1] to mention scipy.io.npfile as well as numpy.save 
friends.  Now, I hope that all common ways to read/write arrays are
present in one place.

[1] http://scipy.org/Cookbook/InputOutput

best,
steve
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion