[Numpy-discussion] Indexing question
This seems like something that can be done with indexing, but I haven't found the solution. out is a 2D array is initialized to zeros. x is a 1D array whose values correspond to the columns of out. For each row in out, set out[row,x[row]] = 1. Here is working code: def orthogonal_mod (x, nbits): out = np.zeros ((len(x), 1
Re: [Numpy-discussion] Indexing question
On Thu, May 20, 2021 at 9:47 AM Neal Becker wrote: > This seems like something that can be done with indexing, but I > haven't found the solution. > > out is a 2D array is initialized to zeros. x is a 1D array whose > values correspond to the columns of out. For each row in out, set > out[row,x[row]] = 1. Here is working code: > def orthogonal_mod (x, nbits): > out = np.zeros ((len(x), 1< for e in range (len (x)): > out[e,x[e]] = 1 > return out > > Any idea to do this without an explicit python loop? > i = np.arange(len(x)) j = x[i] out[i, j] = 1 -- Robert Kern ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Indexing question
Thanks! On Thu, May 20, 2021 at 9:53 AM Robert Kern wrote: > > On Thu, May 20, 2021 at 9:47 AM Neal Becker wrote: >> >> This seems like something that can be done with indexing, but I >> haven't found the solution. >> >> out is a 2D array is initialized to zeros. x is a 1D array whose >> values correspond to the columns of out. For each row in out, set >> out[row,x[row]] = 1. Here is working code: >> def orthogonal_mod (x, nbits): >> out = np.zeros ((len(x), 1<> for e in range (len (x)): >> out[e,x[e]] = 1 >> return out >> >> Any idea to do this without an explicit python loop? > > > > i = np.arange(len(x)) > j = x[i] > out[i, j] = 1 > > -- > Robert Kern > ___ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion -- Those who don't understand recursion are doomed to repeat it ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] interval wrapping / remainder / angle normalization
When I understand correctly and what you desire is equivalent to integer overflowing, the function can indeed be applied as well. I tested. But to be sure, maybe some examples are better. Am Do., 20. Mai 2021 um 03:22 Uhr schrieb ZinGer_KyoN : > I have similar needs, but for int array and integer interval (like > -32768~32767), currently I'm using bitwise and/or (&/|) to do this trick. > > It will be nice if there is an optimized function, both for float and int > > > > -- > Sent from: http://numpy-discussion.10968.n7.nabble.com/ > ___ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion > ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Indexing question
Or as a one-liner: out[np.arange(len(x)), x] = 1 If NEP 21 is accepted ( https://numpy.org/neps/nep-0021-advanced-indexing.html) this would be even simpler: out.vindex[:, x] = 1 Was there ever a decision about that NEP? I didn't follow the discussion too closely at the time. On Thu, May 20, 2021 at 10:06 AM Neal Becker wrote: > Thanks! > > On Thu, May 20, 2021 at 9:53 AM Robert Kern wrote: > > > > On Thu, May 20, 2021 at 9:47 AM Neal Becker wrote: > >> > >> This seems like something that can be done with indexing, but I > >> haven't found the solution. > >> > >> out is a 2D array is initialized to zeros. x is a 1D array whose > >> values correspond to the columns of out. For each row in out, set > >> out[row,x[row]] = 1. Here is working code: > >> def orthogonal_mod (x, nbits): > >> out = np.zeros ((len(x), 1< >> for e in range (len (x)): > >> out[e,x[e]] = 1 > >> return out > >> > >> Any idea to do this without an explicit python loop? > > > > > > > > i = np.arange(len(x)) > > j = x[i] > > out[i, j] = 1 > > > > -- > > Robert Kern > > ___ > > NumPy-Discussion mailing list > > NumPy-Discussion@python.org > > https://mail.python.org/mailman/listinfo/numpy-discussion > > > > -- > Those who don't understand recursion are doomed to repeat it > ___ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion > ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Indexing question
On Thu, May 20, 2021 at 1:40 PM CJ Carey wrote: > Or as a one-liner: > > out[np.arange(len(x)), x] = 1 > Ah, right. `x[arange(len(x))]` is a no-op. > If NEP 21 is accepted ( > https://numpy.org/neps/nep-0021-advanced-indexing.html) this would be > even simpler: > > out.vindex[:, x] = 1 > > Was there ever a decision about that NEP? I didn't follow the discussion > too closely at the time. > IIRC, I think there was broad agreement on the final plan as stated in the NEP. I suspect, though, that the general appetite for adding to the array API surface has declined even from its anemic starting point, now that deep learning frameworks with ndarray-mimicking APIs have taken off. -- Robert Kern ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Indexing question
On Thu, 2021-05-20 at 13:46 -0400, Robert Kern wrote: > On Thu, May 20, 2021 at 1:40 PM CJ Carey > wrote: > > If NEP 21 is accepted ( > > https://numpy.org/neps/nep-0021-advanced-indexing.html) this would > > be > > even simpler: > > > > out.vindex[:, x] = 1 > > > > Was there ever a decision about that NEP? I didn't follow the > > discussion > > too closely at the time. > > > > IIRC, I think there was broad agreement on the final plan as stated > in the > NEP. I suspect, though, that the general appetite for adding to the > array > API surface has declined even from its anemic starting point, now > that deep > learning frameworks with ndarray-mimicking APIs have taken off. True, I am not sure on which side we would land now. Although, NumPy's advanced indexing is too odd to expect ndarray-mimicking APIs to copy it. At least with new attributes you have a chance to define clearly what should happen. I personally still have appetite for it. But expect there will be enough "small" things to fix-up (improve the NEP, figure out how subclassing should be done, clean-up the old code) that this is still a decent sized project. The old 80-20 problem, I guess... So, it just never quite reached the motivation/priority threshold for me since the original push. Cheers, Sebastian > > ___ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion