Expanding NumPy array over extra dimension

2024/10/14 17:16:20

What is the easiest way to expand a given NumPy array over an extra dimension?

For example, suppose I have

>>> np.arange(4)
array([0, 1, 2, 3])
>>> _.shape
(4,)
>>> expand(np.arange(4), 0, 6)
array([[0, 1, 2, 3],[0, 1, 2, 3],[0, 1, 2, 3],[0, 1, 2, 3],[0, 1, 2, 3],[0, 1, 2, 3]])
>>> _.shape
(6, 4)

or this one, a bit more complicated:

>>> np.eye(2)
array([[ 1.,  0.],[ 0.,  1.]])
>>> _.shape
(2, 2)
>>> expand(np.eye(2), 0, 3)
array([[[ 1.,  0.],[ 0.,  1.]],[[ 1.,  0.],[ 0.,  1.]],[[ 1.,  0.],[ 0.,  1.]]])
>>> _.shape
(3, 2, 2)
Answer

I would recommend np.tile.

>>> a=np.arange(4)
>>> a
array([0, 1, 2, 3])
>>> np.tile(a,(6,1))
array([[0, 1, 2, 3],[0, 1, 2, 3],[0, 1, 2, 3],[0, 1, 2, 3],[0, 1, 2, 3],[0, 1, 2, 3]])>>> b= np.eye(2)
>>> b
array([[ 1.,  0.],[ 0.,  1.]])
>>> np.tile(b,(3,1,1))
array([[[ 1.,  0.],[ 0.,  1.]],[[ 1.,  0.],[ 0.,  1.]],[[ 1.,  0.],[ 0.,  1.]]])

Expanding in many dimensions is pretty easy also:

>>> np.tile(b,(2,2,2))
array([[[ 1.,  0.,  1.,  0.],[ 0.,  1.,  0.,  1.],[ 1.,  0.,  1.,  0.],[ 0.,  1.,  0.,  1.]],[[ 1.,  0.,  1.,  0.],[ 0.,  1.,  0.,  1.],[ 1.,  0.,  1.,  0.],[ 0.,  1.,  0.,  1.]]])
https://en.xdnf.cn/q/69386.html

Related Q&A

Django-Haystack giving attribute error?

I am trying to use Haystack and Whoosh with my Django app. I followed the steps on Haystack docs, but i am getting this error when i do a searchAttributeError at /search/ module object has no attribute…

python calendar with holidays [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate:Holiday Calendars, File Formats, et al. Hi, Is there a calendar library in Python with which I can check for holidays, com…

How to choose your conda environment in Jupyter Notebook

I installed Anaconda 5.3 with Python 3.7 (root environment). After that I created a new environment (py36) using Python 3.6I activated the new environment with activate py36 conda env list shows that t…

How do I stagger or offset x-axis labels in Matplotlib?

I was wondering if there is an easy way to offset x-axis labels in a way similar to the attached image.

graphviz segmentation fault

Im building a graph with many nodes, around 3000. I wrote a simple python program to do the trick with graphviz, but it gives me segmentation fault and I dont know why, if the graph is too big or if im…

how to pass char pointer as argument in ctypes python

Please help me in converting below line of c++ code into ctypes python:Ret = openFcn(&Handle, "C:\\Config.xml");below are the declarations of each:typedef uint16_t (* OpenDLLFcnP)(void **…

Restarting a Python Interpreter Quietly

I have a python interpreter embedded inside an application. The application takes a long time to start up and I have no ability to restart the interpreter without restarting the whole application. What…

Unique lists from a list

Given a list I need to return a list of lists of unique items. Im looking to see if there is a more Pythonic way than what I came up with:def unique_lists(l):m = {}for x in l:m[x] = (m[x] if m.get(x) !…

How to run Spyder with Python 3.7 with Anaconda

I have installed Anaconda on a Windows 10 machine which comes with Spyder and Python 3.6 but I wish to upgrade to Python 3.7To create an Anaconda Environment with Python 3.7 is easy by using:conda crea…

Pass in a list of possible routes to Flask?

Im learning Flask and have a question about dynamic routing: is it possible to pass in a list of accepted routes? I noticed the any converter which has potential but had a hard time finding examples o…