## Accessing NumPy array elements not in a given index list

Related searches

I have a NumPy array with the shape (100, 170, 256). And I have an array consisting of indexes [0, 10, 20, 40, 70].

I can get the sub-arrays corresponding to the indexes as follows:

```sub_array = array[..., index]
```

This returns an array with the shape (100, 170, 5) as expected. Now, I am trying to take the complement and get the sub-array NOT corresponding to those indexes. So, I did:

```sub_array = array[..., ~index]
```

This still returns me an array of shape (100, 170, 5) for some reason. I wonder how to do this complement operation of these indexes in python?

[EDIT]

Also tried:

```sub_array = array[..., not(index.any)]
```

However, this does not do the thing I want as well (getting array of shape (100, 170, 251).

have a look at what ~index gives you - I think it is:

```array([ -1, -11, -21, -41, -71])
```

```sub_array = array[..., ~index]
```

will return 5 entries, corresponding to indices [ -1, -11, -21, -41, -71] i.e. 255, 245, 235, 215 and 185 in your case

Similarly, not(index.any) gives

```False
```

hence why your second try doesn't work

This should work:

```sub_array = array[..., [i for i in xrange(256) if i not in index]]
```

Indexing — NumPy v1.20.dev0 Manual, Unlike lists and tuples, numpy arrays support multidimensional indexing for That means that it is not necessary to separate each dimension's index into its own involves giving a boolean array of the proper shape to indicate the values to� A typical array function looks something like this: numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0) Here, all attributes other than objects are optional.

The way you have your data, the simplest approach is to use `np.delete`:

```sub_array = np.delete(array, index, axis=2)
```

Alternatively, the logical operators you were trying to use can be applied with boolean arrays as @DSM suggests:

```mask = np.ones(a.shape[2], dtype=bool)
```

(I wouldn't call your array `array` but I followed the names in your question)

Integer array indexing, Then, if i is not given it defaults to 0 for k > 0 and n - 1 for k < 0 . You may use slicing to set values in the array, but (unlike lists) you can fields of the array can be accessed by indexing the array with strings, dictionary-like. Access Array Elements. Array indexing is the same as accessing an array element. You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

I tend to work with boolean arrays rather than indices where possible to avoid this issue. You could use `in1d` to get one, even though it isn't very pretty:

```>>> arr[..., index].shape
(100, 170, 5)
>>> arr[..., np.in1d(np.arange(arr.shape[-1]),index)].shape
(100, 170, 5)
>>> arr[..., ~np.in1d(np.arange(arr.shape[-1]),index)].shape
(100, 170, 251)
```

Numpy, A question arises that why do we need NumPy when python lists are already there. array with a negative step: \n",a) # Indexes are specified inside the np. array import numpy as np # NumPy array with elements from 1 to 9 x = np.array ([1, 2, For example x[, arr1, arr2, :] but not x[arr1, :, 1] since 1 is an advanced index� Negative values can be used to access the end of the array. b [0,-1] # => 4. Access using integer index arrays. To get a subset of an array via the indices, integer arrays can be used. Say we want to access the first, third and fifth element of a one-dimensional array:

I'm assuming `index` is a numpy array - if so, the explanation for what the tilde operator is doing can be found here:

What does the unary operator ~ do in numpy?

As for what you're trying to accomplish, you could assemble a complementary index array:

```notIndex = numpy.array([i for i in xrange(256) if i not in index])
```

And then use `notIndex` instead of `index`.

Basic Slicing and Advanced Indexing in NumPy Python , in Python � Python Membership and Identity Operators | in, not in, is, is not A question arises that why do we need NumPy when python lists are already there. The answer to it is we cannot perform operations on all the elements of two list directly. Indexes are specified inside the np.array method. Data manipulation in Python is nearly synonymous with NumPy array manipulation: even newer tools like Pandas are built around the NumPy array.This section will present several examples of using NumPy array manipulation to access data and subarrays, and to split, reshape, and join the arrays.

The question is answered but I propose a benchmark of the three methods here.

Fastest solution is boolean mask (with small and larger index array size)

```mask = np.ones(arr.size, dtype=bool)
```

It is 2000 times faster than the list comprehension and marginaly faster than `np.delete`

##### Code to reproduce

Three proposed solutions: list comprehension (`sol1`), boolean mask (`sol2`) or `np.delete` (`sol3`)

```d = 100000
a = np.random.rand(d)
idx = np.random.randint(d, size = 10)

# list comprehension
def sol1(arr, indexes):
return arr[[i for i in range(arr.size) if i not in indexes]]
sol1(a, idx)
# Out[30]: array([0.13044518, 0.68564961, 0.03033223, ..., 0.03796257, 0.40137137, 0.45403929])

def sol2(arr, indexes):
sol2(a, idx)
# Out[32]: array([0.13044518, 0.68564961, 0.03033223, ..., 0.03796257, 0.40137137, 0.45403929])

# np.delete
def sol3(arr, indexes):
return np.delete(arr, indexes)
sol3(a, idx)
# Out[36]: array([0.13044518, 0.68564961, 0.03033223, ..., 0.03796257, 0.40137137, 0.45403929])

```
##### Results
```%timeit sol1(a, idx)
384 ms ± 2.75 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit sol2(a, idx)
154 µs ± 15.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit sol3(a, idx)
194 µs ± 18.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

idx = np.random.randint(d, size = 1000)
%timeit sol1(a, idx)
386 ms ± 7.75 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit sol2(a, idx)
171 µs ± 11.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit sol3(a, idx)
205 µs ± 10.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
```

In the above example, we deleted the second element which has the index of 1. Check if NumPy array is empty. We can use the size method which returns the total number of elements in the array. In the following example, we have an if statement that checks if there are elements in the array by using ndarray.size where ndarray is any given NumPy

Then it will return a tuple of arrays (one for each axis) containing indices where value was TRUE in given bool numpy array i.e. [ 4 7 11] If element not found in numpy array. If the given element doesn’t exist in numpy array then returned array of indices will be empty i.e.

Index arrays¶ NumPy arrays may be indexed with other arrays (or any other sequence- like object that can be converted to an array, such as lists, with the exception of tuples; see the end of this document for why this is). The use of index arrays ranges from simple, straightforward cases to complex, hard-to-understand cases.

Python numpy.where(), elements of the NumPy array ndarray that satisfy the conditions can be replaced or performed specified processing. What If the element not found in numpy array. If the given item doesn’t exist in a numpy array, then the returned array of indices will be empty. See the following code.