Posts: 300
Threads: 72
Joined: Apr 2019
Hi,
I've been thinking I've ever done this, but I do not remember how; in any way I do not understand the Type Error sine I'm using intergers: what I'm missing?
Thanks
import numpy as np
A=np.array([1, 2, 3])
M=np.zeros((10,10), dtype=float)
N=np.copy(M)
P = np.copy(M)
# Explicitly on 1 location
r=3
c=2
M[r, c:c+3]=A
# now on 2 locations
# with a loop
r=np.array([3, 5])
c=np.array([2, 5])
for i in range(2):
N[r[i], c[i]:c[i]+3]=A
#without a loop
i=np.arange(2)
j=c[i]+3*np.ones(2, dtype=int)
k = np.ones((2,1), dtype=int)*A
P[r[i], c[i]:j[i]]=k[i] Error: TypeError: only integer scalar arrays can be converted to a scalar index
Posts: 12,031
Threads: 485
Joined: Sep 2016
you won't get a type error unless their really is one.
Please show complete, unaltered error traceback (always), it includes valuable program execution order information.
Posts: 300
Threads: 72
Joined: Apr 2019
Hi Larz60+
As requested:
Error: runfile('C:/forum.py', wdir='C:/MyDir')
Traceback (most recent call last):
File "C:\outils\Anaconda3\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
exec(code, globals, locals)
File "c:/forum.py", line 26, in <module>
P[r[i], c[i]:j[i]]=k[i]
TypeError: only integer scalar arrays can be converted to a scalar index
Posts: 12,031
Threads: 485
Joined: Sep 2016
You can write the code as follows to display values if TypeError encountered:
import numpy as np
import sys
A = np.array([1, 2, 3])
M = np.zeros((10, 10), dtype=float)
N = np.copy(M)
P = np.copy(M)
# Explicitly on 1 location
r = 3
c = 2
M[r, c : c + 3] = A
print(f"\nM(r, c:c_3): {M[r, c:c+3]}")
# now on 2 locations
# with a loop
r = np.array([3, 5])
c = np.array([2, 5])
for i in range(2):
try:
N[r[i], c[i] : c[i] + 3] = A
except TypeError:
print(f"First loop: Values when TypeError occurs:, i: {i}, c[i]: {c[i]}")
sys.exit(-1)
# without a loop
i = np.arange(2)
j = c[i] + 3 * np.ones(2, dtype=int)
k = np.ones((2, 1), dtype=int) * A
try:
P[r[i], c[i] : j[i]] = k[i]
except TypeError:
print(
f"\nTypeError, Values when attempting P[r[i], c[i]:j[i]]=k[i]:\n"
f" i: {i}, r[i]: {r[i]}, c[i]: {c[i]}, j[i]: {j[i]}"
)
sys.exit(-1) output shows where error occurs:
Output: M(r, c:c_3): [1. 2. 3.]
TypeError, Values when attempting P[r[i], c[i]:j[i]]=k[i]:
i: [0 1], r[i]: [3 5], c[i]: [2 5], j[i]: [5 8]
Posts: 300
Threads: 72
Joined: Apr 2019
Ok I keep it in mind (I know that I need to implement more Exceptions in my code).
Nonetheless I'm here looking for a way to not use a loop to slice following columns; in the example hereafter, it works with a single arugument/tuple, but I cannot go further
Paul
#without a loop
r=np.array([3, 5])
c=np.array([2, 5])
i=np.arange(2)
d=c[i]+3*np.ones(2, dtype=int)
k = np.ones((2,1), dtype=int)*A
# P[r[i], 2:5]=k[i] # ok
P[r[i], slice(2, 5)]=k[i] # ok
Posts: 300
Threads: 72
Joined: Apr 2019
After some tests and trials, I can say that the issue comes from the Slice object, but I still trying to understand how to fix it.
import numpy as np
import sys
A=np.array([1, 2, 3])
M=np.zeros((10,10), dtype=float)
N=np.copy(M)
P=np.copy(M)
# # Explicitly on 1 location
r=3
c=2
M[r, c:c+3]=A
# now on 2 locations
# with a loop
r=np.array([3, 5])
c=np.array([2, 5])
for i in range(2):
N[r[i], c[i]:c[i]+3]=A
#without a loop
i=np.arange(2)
d=c[i]+3*np.ones(2, dtype=int)
SliceIndexes = np.vstack((c,d))
k = np.ones((2,1), dtype=int)*A
SliceObject = slice(SliceIndexes[i][0], SliceIndexes[i][1])
P[r[i], SliceObject ]=k[i] # ko
# P[r[i], 0:3]=k[i] # ok
# P[r[i], slice(2,5)]=k[i] # ok If somebody knows how to, it'll be highly appreciated.
Paul
Here bellow the screenshot of information gotten under Spyder:
Posts: 12,031
Threads: 485
Joined: Sep 2016
Which version of python are you running?
from command line: python -V
Posts: 300
Threads: 72
Joined: Apr 2019
Hi Larz60+
Python 3.9.13
|