placing 2-dim arrays into 3-dim sheets (SOLVED)

sonettguy
Posts: 115
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

placing 2-dim arrays into 3-dim sheets (SOLVED)

I need some help. I'm on a pi zero w with raspbian stretch and using python 2.7 and numpy. I'm building 2-dim arrays in a loop and would like to store each resulting array. So, I'd like to build a 3-dim array with each sheet being a loop iteration. I'm confused as to how to create the 3-D array and add elements as 2-D arrays. I refer to the manual, which states:
Indexing Multi-dimensional arrays
Things become more complex when multidimensional arrays are indexed, particularly with multidimensional index arrays. These tend to be more unusual uses, but they are permitted, and they are useful for some problems. We’ll start with the simplest multidimensional case (using the array y from the previous examples):
>>> y=np.arange(35).reshape(5,7) #I added this line from previous examples.
>>>
>>> y[np.array([0,2,4]), np.array([0,1,2])]
array([ 0, 15, 30])
In this case, if the index arrays have a matching shape, and there is an index array for each dimension of the array being indexed, the resultant array has the same shape as the index arrays, and the values correspond to the index set for each position in the index arrays. In this example, the first index value is 0 for both index arrays, and thus the first value of the resultant array is y[0,0]. The next value is y[2,1], and the last is y[4,2].
My code:

Code: Select all

``````for i in range(manifolds):
for j in range(known):
for k in range(new):
{lots of stuff)
print "resulting 2-D array",matmul.shape,matmul.dtype
print matmul
if i==0, j==0, k==0:
correl=np.expand_dims(matmul, axis=0)
print "correl is", correl.shape, correl.dtype
correl(i,j,k)=correl.itemset(i, matmul[j,k])``````
I get

Code: Select all

``````   File "nameofprog.py", line 245
correl(i,j,k)=correl.itemset(i,matmul[j,k])
SyntaxError: can't assign to function call``````
I don't see what is wrong.
Last edited by sonettguy on Wed May 01, 2019 3:09 pm, edited 1 time in total.

sonettguy
Posts: 115
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

Re: placing 2-dim arrays into 3-dim sheets

I am wondering if I might have done something to alienate the forum. This is the third topic I've posted asking for help and not getting feedback. If I did something wrong, I apologize, I certainly didn't intend to offend anyone.

pcmanbob
Posts: 6248
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: placing 2-dim arrays into 3-dim sheets

As 99.9% of people here are just other pi users it my be that no one that has looked at your post has any idea on how to solve your problem.

It can take days for the right person to see your post, 24 hours is a very short time for that to occur.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

sonettguy
Posts: 115
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

Re: placing 2-dim arrays into 3-dim sheets

Good to know, @pcmanbob. Thanks.

mahjongg
Forum Moderator
Posts: 11853
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: placing 2-dim arrays into 3-dim sheets

Moved to python. where it might have more success.

gordon77
Posts: 4003
Joined: Sun Aug 05, 2012 3:12 pm

Re: placing 2-dim arrays into 3-dim sheets

Can you provide more info, eg what are manifolds, known, new etc?

When l run it it rejects if i==0, j==0, k==0:, does this work for you?

Paeryn
Posts: 2567
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: placing 2-dim arrays into 3-dim sheets

sonettguy wrote:
Wed Apr 17, 2019 2:22 pm

Code: Select all

``````         if i==0, j==0, k==0:
correl=np.expand_dims(matmul, axis=0)
print "correl is", correl.shape, correl.dtype
correl(i,j,k)=correl.itemset(i, matmul[j,k])
``````
I get

Code: Select all

``````   File "nameofprog.py", line 245
correl(i,j,k)=correl.itemset(i,matmul[j,k])
SyntaxError: can't assign to function call``````
I don't see what is wrong.
As Python told you, you are trying to assign a value correl.itemset(i, matmul[j,k]) to a function call correl(i,j,k).
I've no experience with numpy but looking at the docs Array.itemset() returns None (it doesn't return anything), are you sure you want to assign the value None to whatever you were thinking of on the left-hand side? Which brings you on to the error that Python gave...

correl(i,j,k) says call the object correl with arguments i, j, k, but correl is an Array which is not callable and you can't assign a value to a function call, that makes no sense which is why Python gave the error there, it hadn't gotten around to trying to call correl(i,j,k) when it noticed the assignment attempt.
She who travels light — forgot something.

sonettguy
Posts: 115
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

Re: placing 2-dim arrays into 3-dim sheets

That's helpful @Paeryn. I hadn't meant the left side to be a function, but rather a specific element of a matrix. I should have used square brackets, I think. @Gordon77, I included only a section of code. Perhaps I should include the imports and variable assignments. If I still have trouble after changing the brackets, I'll do that in the next iteration of questions. In general, if you think of four dimensions as (books, sheets, rows, columns), then manifolds are books, sources are sheets. I have two 4D arrays, one is a bank of known sources and the other is a measured collection of new sources, which I compare to the known sources. None of that is really pertinent to the problem I'm having, but it might be interesting to the readers. I'll clean up my code and let you know how it goes.

sonettguy
Posts: 115
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

Re: placing 2-dim arrays into 3-dim sheets

10 days of trying different things and reading so many tutorials and posts, but I am still working on this. I'm so confused now, I need to take a step back to better define exactly what I want to end up with. Forgive the delay.

gordon77
Posts: 4003
Joined: Sun Aug 05, 2012 3:12 pm

Re: placing 2-dim arrays into 3-dim sheets

Here's one way of doing it... It simply fills the array with consecutive numbers as an example

Code: Select all

``````from numpy import zeros
import numpy as np

# define 2d array size
a = 5
b = 7

# max 3d layers
c = 10

# setup initial 3d array
q = zeros((1, a, b))
r = zeros((a,b))

x = 0
i = 0
while i < c:
for j in range(0,a):
for k in range(0,b):
# if j and k = 0 add new 3d layer
if k == 0 and j == 0 and x > j * k:
i +=1
q = np.vstack((q,r[None]))
# populate each element
q[i][j][k]= x
x +=1

print (q)``````

sonettguy
Posts: 115
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

Re: placing 2-dim arrays into 3-dim sheets

YES. Thank you, @gordon77. I was not familiar with the 'stack' commands. That is what I was looking for. Greatly appreciated.