我得到一个错误, IndexError:只有整数,切片(:),省略号(...),numpy.newaxis(无)和整数或布尔数组是有效的索引。 我正在制作声音识别应用程序。 我的代码是IndexError可以将int用作索引吗?
import numpy as np
import pandas as pd
import scipy as sp
import pickle
from scipy import fft
from time import localtime, strftime
import matplotlib.pyplot as plt
from skimage.morphology import disk,remove_small_objects
from skimage.filter import rank
from skimage.util import img_as_ubyte
import wave
folder = 'mlsp_contest_dataset/'
essential_folder = folder+'essential_data/'
supplemental_folder = folder+'supplemental_data/'
spectro_folder =folder+'my_spectro/'
single_spectro_folder =folder+'my_spectro_single/'
dp_folder = folder+'DP/'
# Each audio file has a unique recording identifier ("rec_id"), ranging from 0 to 644.
# The file rec_id2filename.txt indicates which wav file is associated with each rec_id.
rec2f = pd.read_csv(essential_folder + 'rec_id2filename.txt', sep = ',')
# There are 19 bird species in the dataset. species_list.txt gives each a number from 0 to 18.
species = pd.read_csv(essential_folder + 'species_list.txt', sep = ',')
num_species = 19
# The dataset is split into training and test sets.
# CVfolds_2.txt gives the fold for each rec_id. 0 is the training set, and 1 is the test set.
cv = pd.read_csv(essential_folder + 'CVfolds_2.txt', sep = ',')
# This is your main label training data. For each rec_id, a set of species is listed. The format is:
# rec_id,[labels]
raw = pd.read_csv(essential_folder + 'rec_labels_test_hidden.txt', sep = ';')
label = np.zeros(len(raw)*num_species)
label = label.reshape([len(raw),num_species])
for i in range(len(raw)):
line = raw.iloc[i]
labels = line[0].split(',')
labels.pop(0) # rec_id == i
for c in labels:
if(c != '?'):
print(label)
label[i,c] = 1
我运行此代码, 我在这一点上label[i,c] = 1
得到了错误。 我试图通过print(label)
label
看到label
变量是像
warn(skimage_deprecation('The `skimage.filter` module has been renamed '
[[ 0. 0. 0. ..., 0. 0. 0.]
[ 0. 0. 0. ..., 0. 0. 0.]
[ 0. 0. 0. ..., 0. 0. 0.]
...,
[ 0. 0. 0. ..., 0. 0. 0.]
[ 0. 0. 0. ..., 0. 0. 0.]
[ 0. 0. 0. ..., 0. 0. 0.]]
我认为,该错误意味着整数,切片(:),省略号(...),numpy.newaxis(无)和整数或布尔不能用作数组索引,但我把int放入数组索引很多时候,所以我不明白为什么会发生这个错误。 调试告诉我,
labels
具有标签:: [ '?']。
c
的
for c in labels[i]:
有 '?',我真的不明白? type.I认为这个?导致错误,但我不知道如何解决这个问题。 我该如何解决这个问题?
'在标签C:...','不过是labels'字符串列表。字符串是不是在设置“*整数,切片(:),省略号(...),numpy.newaxis(无)和整数或布尔*”。 (另请注意:'np.zeros((LEN(原料),num_species))'是简单的。) –
@AndrasDeak非常感谢你!哪一部分是np。你告诉我的零((len(raw),num_species))?我怎样才能解决这个问题? – user21063
我只注意到for循环之前的两行可以在一行中完成,而无需重新整形。至于你的问题:我不知道你想要做什么,但试图使用字符作为numpy数组索引肯定是行不通的。 –