2

My question is kind of simple yet I don't seem to find the answer.

I have the following line in my code that works fine:

Uniquesample = np.matrix([[   1.   ,    0.   ],
                   [   2.   , -106.965],
                   [   3.   ,  -83.835],
                   [   4.   ,   12.5  ],
                   [   5.   , -141.6  ],
                   [   6.   ,  -17.25 ],
                   [   7.   ,  -94.785],
                   [   8.   ,  -26.785],
                   [   9.   , -125.835],
                   [  10.   ,    6.865],
                   [  11.   ,   16.23 ],
                   [  12.   ,   61.45 ],
                   [  13.   ,   42.625],
                   [  14.   , -163.655],
                   [  15.   , -116.3  ],
                   [  16.   ,   15.82 ],
                   [  17.   , -166.055],
                   [  18.   ,   90.025],
                   [  19.   ,   14.215],
                   [  20.   ,   82.465]])
L, W = Uniquesample.shape
ModelNumber = 8
Members = np.zeros((L,ModelNumber*W))

seq=[4, 7, 9, 2, 15, 16, 19]
i=0
Count = [7]
for j in seq:
    Members[:Count[i]:,W*i:W*(i+1)] = Uniquesample[j]

Ps: Uniquesample is a 800 x 2 matrix, so i only put the first 20 rows here for simplicity. I tried to change the for loop to another format using "list comprehension":

Members[:Count[i]:,W*i:W*(i+1)] = [Uniquesample[j] for j in seq]

But it does not work. I get the following error: "ValueError: setting an array element with a sequence. The requested array would exceed the maximum number of dimension of 2."

The thing is. Did I "translate" the code correctly to the list comprehension format?

Thank you very much

  • 2
    Could you show us a snippet / demo what ```Members / UniqueSample``` is in order to understand what you want to do? – alfredo Jun 06 '21 at 03:28
  • Can you provide some more context around what is in `seq`, `Uniquesample`, and `Members`? I think `Uniquesample` is a list and with `j` you are trying to access the index? – paul41 Jun 06 '21 at 03:29
  • `:Count[i]:` is just an obfuscated way of slicing `:Count[i]`, right? Is that a base Python array.array, numpy.ndarray, pandas DataFrame? (EDIT: you clarified it's an np.matrix) – smci Jun 06 '21 at 03:43
  • Hey guys, I have updated the question with more information about the code. Sorry it was missing before. – Fatos Mundiais Jun 06 '21 at 03:45
  • Unable to reproduce. I used `seq=[4, 7, 9, 2, 15, 16, 19]`, and replaced the for-loop with the list comprehension statement... and it could run without error – blackraven Jun 06 '21 at 04:02

2 Answers2

1

I used seq=[4, 7, 9, 2, 15, 16, 19] in below example. The initial Members array looks like this: enter image description here

In the for-loop, Members[:Count[i]:,W*i:W*(i+1)] = Uniquesample[j] will replace those 7x2 elements (in red) with the final value in for-loop.

enter image description here

However the list comprehension Members[:Count[i]:,W*i:W*(i+1)] = [Uniquesample[j] for j in seq] will assign each element differently.

enter image description here

FYI, I'm using numpy version '1.18.5', so it may not run properly on www.onlinegdb.com because of old numpy version.

enter image description here

blackraven
  • 5,284
  • 7
  • 19
  • 45
  • Thank you for that! I changed seq to `seq=[4, 7, 9, 2, 15, 16, 19]` like you did but still got the same error from before. I am using Anaconda with Spyder's latest version. I changed the code in the question to help with reproducibility. The strange is that I uploaded the files and run them in Google Colab and it worked. What could be happening? Is Spyder in any way refusing to compute the list comprehension? – Fatos Mundiais Jun 06 '21 at 13:37
  • Have you tried restarting Spyder (fresh start), and run the codes again? – blackraven Jun 06 '21 at 13:42
  • Yes, I actually uninstalled Anaconda and Spyder and reinstalled them again, but the code is still now working. I also tried the code in this online compiler and it shows the error as well. Could you give it a look? https://onlinegdb.com/dy0F2qXrJ – Fatos Mundiais Jun 06 '21 at 13:51
  • Yes very strange. I copied the online codes from `onlinegdb` and could run successfully on my local machine, using Python 3.8.3 and numpy '1.18.5'. Can you check the other IDE using `!python --version` and `np.version.version` – blackraven Jun 06 '21 at 14:22
  • 1
    Spyder's python version is 3.8.10 and numpy is 1.20.2. Google Colab's is 3.7.10 and 1.19.5. I think I will try to downgrade my python version to 3.8.3 like yours and try to run it – Fatos Mundiais Jun 06 '21 at 14:26
  • 1
    I downgraded numpy to version 1.18.5 like yours and now it runs without erros in Spyder. There is something in newer numpy versions that identifies this list comprehension structure as an error – Fatos Mundiais Jun 06 '21 at 20:02
  • interesting findings! So versions do matter in complex codes! I'm glad we found the root cause – blackraven Jun 06 '21 at 20:28
  • 1
    Thank you so much for your help Black Raven. I "lost" three days trying to fix this error. – Fatos Mundiais Jun 07 '21 at 12:34
  • I'm glad you found the solution :-) – blackraven Jun 07 '21 at 12:56
1

The point is, in first code you are assigning one item of Uniquesample[j] each time in Members[:Count[i]:,Wi:W(i+1)]. First Uniquesample[0] to Members[:Count[i]:,Wi:W(i+1)], then Uniquesample[1] to same Members[:Count[i]:,Wi:W(i+1)] and so on.

In your second code you are trying to put a list of Uniquesample items into a location, which can only hold one item. That's why the error.

Khairul
  • 386
  • 1
  • 7