How to select inverse of indexes of a numpy array?

前端 未结 4 1060
夕颜
夕颜 2020-12-07 00:19

I have a large set of data in which I need to compare the distances of a set of samples from this array with all the other elements of the array. Below is a very simple exa

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-07 01:00

    mask = np.ones(len(data), np.bool)
    mask[sample_indexes] = 0
    other_data = data[mask]
    

    not the most elegant for what perhaps should be a single-line statement, but its fairly efficient, and the memory overhead is minimal too.

    If memory is your prime concern, np.delete would avoid the creation of the mask, and fancy-indexing creates a copy anyway.

    On second thought; np.delete does not modify the existing array, so its pretty much exactly the single line statement you are looking for.

提交回复
热议问题