[Numpy-discussion] How do I use numpy to do this?

Robert Kern robert.kern at gmail.com
Fri Jun 2 13:35:39 CDT 2006


Christopher Barker wrote:
> Robert Kern wrote:

>> x = repeat(x, ny)
>> y = concatenate([y] * nx)
>> points = transpose([x, y])
> 
> Somehow I never think to use repeat. And why use repeat for x and
> concatenate for y?

I guess you could use repeat() on y[newaxis] and then flatten it.

y = repeat(y[newaxis], nx).ravel()

> Using numpy
> The Numpy way took: 0.020000 seconds
> My way took: 0.010000 seconds
> Robert's way took: 0.020000 seconds
> Using Numeric
> My way took: 0.010000 seconds
> Robert's way took: 0.020000 seconds
> Using numarray
> My way took: 0.070000 seconds
> Robert's way took: 0.120000 seconds
> Number of X: 4
> Number of Y: 3

Those timings look real funny. I presume you are using a UNIX and time.clock().
Don't do that. It's a very poor timer on UNIX. Use time.time() on UNIX and
time.clock() on Windows(). Even better, please use timeit.py instead. Tim Peters
did a lot of work to make timeit.py do the right thing.

-- 
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





More information about the Numpy-discussion mailing list