Modify a subset with advanced NumPy re-fetch indexing

I have multiple column repeat that I use to select a subset. Sort of

>>> x
   array([ ('label1',True,3),
          ('label2',True,2),
          ('label1',False,4)],
         dtype=[('status', '|S16'), ('select', '|b1'), ('somedata', '<i4')])

Data is fetched from this array using an approach similar to the previous question of the previous SO. .

condit=(x['status']=='label1')&(x['select']==True)
x_subids=numpy.where(condit)[0]
x_sub=x[x_subids]

Then I do some work on the subset and update the original.

x[x_subids]=x_sub

I understand that x_subthis is a copy, not a view due to advanced indexing, and I was wondering if there was an elegant way to avoid copying an array and just work with the original, given the conditions that I need to subset the data.

+3
source share
2

, , numpy.place() function:

>>> import numpy
>>> x = numpy.array([("label1",True,3), ("label2",False,2), ("label1",True,4)],
...     dtype=[("status", "|S16"), ("select", "|b1"), ("somedata", ">> mask = x["select"]
>>> numpy.place(x["somedata"], mask, (5, 6))
>>> print x
[('label1', True, 5) ('label2', False, 2) ('label1', True, 6)]
>>> numpy.place(x["status"], mask, "label3")
>>> print x
[('label3', True, 5) ('label2', False, 2) ('label3', True, 6)]

,

  • .

  • , mask True, , .

  • ==True condit , :)

+3

" ":

masked = numpy.ma.array(x, 
                        mask=(x['status']!='label1')|(x['select']!=True),
                        copy=False)

, condit, , True, . numpy ufunc , , .

+2

Source: https://habr.com/ru/post/1773351/


All Articles