Numpy: Array of `arange`s

2024/9/20 5:53:42

Is there a way to take...

>>> x = np.array([0, 8, 10, 15, 50]).reshape((-1, 1)); ncols = 5

...and turn it into...

array([[ 0,  1,  2,  3,  4],[ 8,  9, 10, 11, 12],[10, 11, 12, 13, 14],[15, 16, 17, 18, 19],[50, 51, 52, 53, 54]])

I was able to do it with np.apply_along_axis...

>>> def myFunc(a, ncols):return np.arange(a, (a+ncols))>>> np.apply_along_axis(myFunc, axis=1, arr=x)

and with for loops...

>>> X = np.zeros((x.size,ncols))
>>> for a,b in izip(xrange(x.size),x):X[a] = myFunc(b, ncols)

but they are too slow. Is there a faster way?

Thanks in advance.

Answer

The following will do it:

In [9]: x = np.array([0, 8, 10, 15, 50]).reshape((-1, 1))In [10]: ncols = 5In [11]: x + np.arange(ncols)
Out[11]: 
array([[ 0,  1,  2,  3,  4],[ 8,  9, 10, 11, 12],[10, 11, 12, 13, 14],[15, 16, 17, 18, 19],[50, 51, 52, 53, 54]])

It adds a row vector to a column vector and relies on broadcasting to do the rest.

This should be as fast as anything: producing a 1000x1000 matrix takes ~1.6ms:

In [17]: %timeit np.arange(1000).reshape((-1, 1)) + np.arange(1000)
1000 loops, best of 3: 1.61 ms per loop
https://en.xdnf.cn/q/72209.html

Related Q&A

Understanding model.summary Keras

Im trying to understand model.summary() in Keras. I have the following Convolutional Neural Network. The values of the first Convolution are: conv2d_4 (Conv2D) (None, 148, 148, 16) 448 …

Determine adjacent regions in numpy array

I am looking for the following. I have a numpy array which is labeled as regions. The numpy array represents a segmented image. A region is a number of adjacent cells with the same value. Each region h…

Python: win32gui.SetForegroundWindow

I have just written simple script to launch an applciation and I am trying to use "SendKeys" module to send keystrokes to this application. There is one "Snapshot" button, but I can…

Building PyCrypto with fastmath (gmp or mpir) via pip on Windows

I installed PyCrypto on Windows via pip but i was not able to build Crypto.PublicKey._fastmath because GMP was not found.I know there is a binary version on voidspace but i would like to build the late…

Get name of current test in setup using nose

I am currently writing some functional tests using nose. The library I am testing manipulates a directory structure. To get reproducible results, I store a template of a test directory structure and cr…

python: find html tags and replace their attributes [duplicate]

This question already has answers here:Replace SRC of all IMG elements using Parser(2 answers)Closed 10 years ago.I need to do the following:take html document find every occurrence of img tag take the…

Django/Apache/mod_wsgi not using virtualenvs Python binary

I have a virtualenv at /opt/webapps/ff/ with its own Python installation. I have WSGIPythonHome set to /opt/webapps/ff in my Apache config file (and this is definitely getting used in some capacity, b…

How to open the users preferred mail application on Linux?

I wrote a simple native GUI script with python-gtk. Now I want to give the user a button to send an email with an attachment.The script runs on Linux desktops. Is there a way to open the users preferr…

finding a set of ranges that a number fall in

I have a 200k lines list of number ranges like start_position,stop position. The list includes all kinds of overlaps in addition to nonoverlapping ones.the list looks like this[3,5] [10,30] [15,25] [5…

Python Tornado Websocket Connections still open after being closed

I have a Tornado Websocket Server and I want to time out after 30 minutes of inactivity. I use self.close() to close the connection after 30 minutes of inactivity. But it seems that some connections st…