only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
Asked Answered
R

4

78

I am implementing fft and when I shuffle the data elements using bit reversal, I get the following error:

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis 
(`None`) and integer or boolean arrays are valid indices.

My code is:

def shuffle_bit_reversed_order(data: np.ndarray) -> np.ndarray:
    x = data.size
    n = x / 2
    y = n * np.mod(x, 2)
    data[x], data[y] = data[y], data[x]
    return data

I think the problem is my data is of type 'float64' and I may have used it as an integer but I don't know how I can solve it.

Reeba answered 22/1, 2016 at 17:29 Comment(0)
B
67

I believe your problem is this: in your while loop, n is divided by 2, but never cast as an integer again, so it becomes a float at some point. It is then added onto y, which is then a float too, and that gives you the warning.

Betoken answered 22/1, 2016 at 17:53 Comment(1)
Broader picture: Indexing with a float array gives IndexError: arrays used as indices must be of integer (or boolean) type, indexing with a naked float or a list with floats gives the error in the title.Pauper
H
42

You can use // instead of single /. That converts to int directly.

Ham answered 22/5, 2017 at 4:15 Comment(2)
Works like a charmOlwena
That was the ticket!Eichelberger
A
5

put a int infront of the all the voxelCoord's...Like this below :

patch = numpyImage [int(voxelCoord[0]),int(voxelCoord[1])- int(voxelWidth/2):int(voxelCoord[1])+int(voxelWidth/2),int(voxelCoord[2])-int(voxelWidth/2):int(voxelCoord[2])+int(voxelWidth/2)]
Alginate answered 13/11, 2019 at 10:14 Comment(0)
P
0

As the message says, this error occurs if you try to index a numpy array using an invalid value such as a float or a string.

Apart from the case where a float is used to index an array, e.g. arr[0.], whose solution is to convert the float into an int like arr[0] (or if the index is created dynamically, just cast it into an int like arr[int(idx)]), another pretty common case occurs when a numpy ndarray is attempted to be indexed using a string, which is especially prevalent when using scikit-learn.

For example, some common preprocessing functions take pandas dataframes but return numpy ndarrays upon transformation, which makes it not possible to select columns using labels. In that case, a solution is either to use it as a ndarray or create a pandas dataframe from the transformed data and select columns of that transformed data.

import pandas as pd
from sklearn.preprocessing import MinMaxScaler

df = pd.DataFrame({'A': [1, 2, 3], 'B': [10, 20, 30]})
sc = MinMaxScaler()
df = sc.fit_transform(df)

df['A']        # <--- IndexError
df[:, 0]       # <--- OK

# explicitly create a pandas dataframe from transformed data
df1 = pd.DataFrame(sc.fit_transform(df), columns=df.columns)
df1['A']       # <--- OK
Pavid answered 13/4 at 7:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.