Python flatten array inside numpy array

2024/10/14 4:25:23

I have a pretty stupid question, but for some reason, I just can't figure out what to do. I have a multi-dimensional numpy array, that should have the following shape:

(345138, 30, 300)

However, it actually has this shape:

(345138, 1)

inside the 1 element-array is the array containing the shape

(30, 300)

So how do I "move" the inside array, so that the shape is correct?

At the moment it looks like this:

[[ array([[0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 0, 0],..., [0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 0, 0]], dtype=int32)][ array([[0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 0, 0],..., 

but I want this without the array(...), dtype=32 and move what is in there into the first array so that the shape is (345138, 30, 300) and looks like this:

[[ [0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 0, 0],..., [0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 0, 0]],[ [0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 0, 0],..., 

Any ideas?

Answer

Looks like you have a 2d array that contains 2d arrays (object dtype). I can construct one like that with:

In [972]: arr = np.empty(4,dtype=object)
In [973]: arr = np.empty((4,1),dtype=object)
In [974]: for i in range(4): arr[i,0]=np.ones((2,3),int)
In [975]: arr
Out[975]: 
array([[array([[1, 1, 1],[1, 1, 1]])],[array([[1, 1, 1],[1, 1, 1]])],[array([[1, 1, 1],[1, 1, 1]])],[array([[1, 1, 1],[1, 1, 1]])]], dtype=object)

Simply wrapping that in np.array does not work; not does applying tolist:

In [976]: np.array(arr)
Out[976]: 
array([[array([[1, 1, 1],[1, 1, 1]])],[array([[1, 1, 1],[1, 1, 1]])],[array([[1, 1, 1],[1, 1, 1]])],[array([[1, 1, 1],[1, 1, 1]])]], dtype=object)
In [977]: arr.tolist()
Out[977]: 
[[array([[1, 1, 1],[1, 1, 1]])], [array([[1, 1, 1],[1, 1, 1]])], [array([[1, 1, 1],[1, 1, 1]])], [array([[1, 1, 1],[1, 1, 1]])]]

One way of 'flattening' is to use some version of concatenate:

In [978]: np.stack(arr.ravel())
Out[978]: 
array([[[1, 1, 1],[1, 1, 1]],[[1, 1, 1],[1, 1, 1]],[[1, 1, 1],[1, 1, 1]],[[1, 1, 1],[1, 1, 1]]])
In [979]: _.shape
Out[979]: (4, 2, 3)

I used ravel to reduce the outer array to 1d, which stack can use as a list. stack acts like np.array in that it combines the elements on a new axis (which we can specify).


tolist and array can work together:

In [981]: np.array(arr.tolist())
Out[981]: 
array([[[[1, 1, 1],[1, 1, 1]]],[[[1, 1, 1],[1, 1, 1]]],[[[1, 1, 1],[1, 1, 1]]],[[[1, 1, 1],[1, 1, 1]]]])
In [982]: _.shape
Out[982]: (4, 1, 2, 3)

Or tolist plus squeeze (which is actually np.asarray(...).squeeze())

In [983]: np.squeeze(arr.tolist())
Out[983]: 
array([[[1, 1, 1],[1, 1, 1]],[[1, 1, 1],[1, 1, 1]],[[1, 1, 1],[1, 1, 1]],[[1, 1, 1],[1, 1, 1]]])
In [984]: _.shape
Out[984]: (4, 2, 3)
https://en.xdnf.cn/q/117999.html

Related Q&A

Peewee and Flask : Database object has no attribute commit_select

Im trying to use Peewee with Flask, but I dont understand why my database connection does not work.config.pyclass Configuration(object): DATABASE = {name: test,engine: peewee.MySQLDatabase,user: root,p…

for loop to create a matrix in python

I am trying to study the probability of having a zero value in my data and I have developed a code that outputs the value of a column of data when the other is zero which is what I need. But having to …

How to convert List of JSON frames to JSON frame

I want to convert List of JSON object ot Single JSON frameHere is my codefor i in user1:name=i.namepassword=i.passwordid1=i.iduser = { "name" : name,"password" : password,"id&q…

Python 2.7 The packaging package is required; normally this is bundled with this package

I expect this has to do with the cryptography module, but Im not sure.Traceback (most recent call last):File "<string>", line 11, in <module>File "c:\python27\lib\site-packag…

Couple the data in all possible combinations

I have data in column in two columns like thisId Value 1 a 2 f 1 c 1 h 2 aand Id like couple the data of the Value column in all possible combinations based on the same Id such as(a,c) (a,h)…

Python - Find date from string

Would anyone know a regex string or another method of obtaining the date and time from this string into variables? The position of the string could change, so line and char no would not work. This is …

Get 1st column values on .csv file on python

i am newbie at python programming, i have a .csv file containing mac address and signal strength data from an AP consider my csv data is:i want to get just mac address values which is the 1st row, ref…

how to click mouse over sub menu in selenium?

I want to click invisible htmls sub menu click.*invisible html source<ul class="options"> <li class="_ranking-attr-filter-container _sub-menu-target"> <span>Hide w…

SSL: CERTIFICATE_VERIFY_FAILED certificate verify failed (_ssl.c.661)

Im trying to install nltk on my Mac, but I keep getting this error message after following these instructions: Install NLTK: sudo pip install -U nltk Install Numpy (optional): sudo pip install -U numpy…

Real-time reading of terminal output from server

Im trying to process images from my camera on my server and get the information after processing on my local machine in real-time. I can get necessary information as terminal outputs on my server, but …