[Numpy-discussion] Classes as records within an numpy array

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Mon Apr 11 09:17:28 EDT 2011

On 04/11/2011 05:40 AM, David Crisp wrote:
> On Mon, Apr 11, 2011 at 1:17 PM, David Crisp <david.crisp at gmail.com> wrote:
>> On Mon, Apr 11, 2011 at 11:00 AM, Sturla Molden <sturla at molden.no> wrote:
>>> Den 11.04.2011 02:01, skrev David Crisp:
>>>> Can anybody guide me through this problem?
> I dont know how acceptable it is to answer your own question :P   here goes:
> TO some extent I have solved my problem,  But I am unsure if it is the
> correct way of solving it:
> for each_line in point_scandata:
> 	matrix_x = round_to_value((each_line[0]-x_offset),horizontal_precision)
> 	matrix_y = round_to_value((each_line[1]-y_offset),horizontal_precision)   <---- vertical_precision?
> 	if vegetation_matrix[matrix_x,matrix_y] == None:
> 		vegetation_matrix[matrix_x,matrix_y] = Record()
> 	vegetation_matrix[matrix_x,matrix_y].add(each_line[2])
you should initialize the matrix when creating it. What about
  vegeration_matrix = numpy.array([[Record() for _ in xrange(x_bins+1)] for _ in xrange(y_bins+1)])
Then you could avoid the ugly if.

A more general question: why do you want to use numpy object arrays at all?
You are not using any array operatations with your Record objects. The numpy array
of objects is equivalent to a two-dimensional list. You could use a normal Python
list just as well:
vegetation_matrix = [[Record() for _ in xrange(x_bins+1)] for _ in xrange(y_bins+1)]


More information about the NumPy-Discussion mailing list