replace sets of values in an array without using loops - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: replace sets of values in an array without using loops (/thread-37497.html) |
replace sets of values in an array without using loops - paul18fr - Jun-19-2022 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]
RE: replace sets of values in an array without using loops - Larz60+ - Jun-19-2022 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. RE: replace sets of values in an array without using loops - paul18fr - Jun-20-2022 Hi Larz60+ As requested:
RE: replace sets of values in an array without using loops - Larz60+ - Jun-20-2022 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:
RE: replace sets of values in an array without using loops - paul18fr - Jun-20-2022 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 RE: replace sets of values in an array without using loops - paul18fr - Jun-20-2022 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] # okIf somebody knows how to, it'll be highly appreciated. Paul Here bellow the screenshot of information gotten under Spyder: RE: replace sets of values in an array without using loops - Larz60+ - Jun-20-2022 Which version of python are you running? from command line: python -V
RE: replace sets of values in an array without using loops - paul18fr - Jun-20-2022 Hi Larz60+ Python 3.9.13 |