Reputation: 673
I want to read from a .npy file to do some signal processing tasks but during this task I received this error:
IndexError: only integers, slices (
:
), ellipsis (...
), numpy.newaxis (None
) and integer or boolean arrays are valid indices
this is my code:
import numpy as np
import matplotlib.pyplot as plt
file = '/signal/data.npy'
d = np.load(file,allow_pickle=True,encoding = 'latin1')
d['soma'][0]
There are same questions but I could not use them to solve this one.So can anyone help me to fix It? Thanks
This is part of my data( d is equal to res):
Upvotes: 0
Views: 197
Reputation: 26
your data consists of arrays of dictionaries. for each array you have some keys with its values. the solution as @ hpaulj said is:
res[array_index]["your_key"]
Upvotes: 1
Reputation: 12407
You have a numpy array d
and you are trying to access e.g. "soma"
index which is not possible. Numpy indexing rule is:
only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices.
If your numpy array includes dictionaries, you need to extract dictionaries. d['soma']
does not extract elements of numpy array.
This loops over array d
and extracts the first element of values of key 'soma'
for all dictionaries in d
that has key 'soma'
:
lfp = [i['soma'][0] for i in d if 'soma' in i]
And if it is a dataframe instead of numpy array, try:
d = pd.read_pickle(file)
Upvotes: 1