Home > Net >  numpy where function TypeError
numpy where function TypeError

Time:12-14

I need to find the Index of an 2D array, where the first Column equals 0.1. So something like this:

Data = [[0, 1], [0.075, 1], [0.1, 1], [0.11, 1], [0.125, 1]]
print(np.where(Data[:,0] == 0.1))

But then I get the following Error:

TypeError: list indices must be integers or slices, not tuple

Would be great if somebody could help me :)

CodePudding user response:

just a typo.. you not initialised Data into an array it is in list form

Code:-

import numpy as np
Data = np.array([[0, 1], [0.075, 1], [0.1, 1], [0.11, 1], [0.125, 1]])
print(np.where(Data[:,0] == 0.1))

Output:-

(array([2]),)

CodePudding user response:

The error:

TypeError: list indices must be integers or slices, not tuple

is because Data is a list not a numpy array, notice that it says tuple because you are passing the tuple (:, 0) to the __getitem__ method of Data. For more info see this, to fix it just do:

import numpy as np

Data = np.array([[0, 1], [0.075, 1], [0.1, 1], [0.11, 1], [0.125, 1]])
print(np.where(Data[:, 0] == 0.1))

Output

(array([2]),)
  • Related