2017-05-28 9 views
-3

エラーが発生しました IndexError:整数、スライス(:)、省略記号(...)、numpy.newaxis(なし)、整数またはブール配列のみが有効なインデックスです。 私は音声認識アプリを作っています。 私のコードは、私はこの時点label[i,c] = 1でエラーが発生しました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 

私はこのコードを実行し、 です。私はprint(label) labellabel変数を参照してくださいしようとした は私が考える

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 

ラベルを持って、私に言った:: [ '?']。

for c in labels[i]: 

c 

ている '?'、私は本当に理解できないのですか?私はこれと思いますか?エラーが発生しますが、これを修正する方法はわかりません。 これを修正するにはどうすればよいですか?

+0

':...'が、 'labels'は、文字列のリストです。文字列は、 "*整数、スライス(:)、省略記号(...)、numpy.newaxis(なし)、整数またはブール値*"にはありません。 (また、 'np.zeros((len(raw)、num_species))'は簡単です) –

+0

@AndrasDeakありがとうございます!どの部分がnpです。あなたが私に言ったゼロ((len(生)、num_species))?どうすればこの問題を解決できますか? – user21063

+0

私は、forループの前の2行を1行にまとめることができました。あなたの問題については、あなたが何をしようとしているのか分かりませんが、数字の配列インデックスとして文字を使用しようとすると、うまくいきません。 –

答えて

0

エラーメッセージがnumpyのアレイ

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

labelにインデックスを付ける場合は、ループ0:

label = np.zeros([len(raw),num_species]) 

の電流値で、浮動小数点数の2次元配列であることを言っている:

for i in range(len(raw)):  # i=0,1,2,... 

rawはどのようなものですか? pd.read_csvから来て、私はそれがデータフレームだと思います。 iloc[i]は行を選択しますが、まだ列に分割されていませんか?

line = raw.iloc[i] 
    labels = line[0].split(',') 
    labels.pop(0) # rec_id == i 

labelsとは何ですか?私はそれが2次元配列のための

for c in labels: 
     if(c != '?'):   # evidently `c` is a string 
      print(label)  # prints the 2d array 
      label[i,c] = 1 

インデックスがlabel[0,1]のようなものである必要があり、文字列の任意の配列です推測しています。 cは、エラーメッセージの他のものの1つになる可能性があります。しかし、それは文字列にすることはできません。

データフレームでは、文字列のインデックス作成が可能です。これはパンダの機能です。しかし、配列の数が少ない場合は、数値インデックスまたはいくつかの選択肢が必要です。それらは文字列で索引付けされません(構造化配列の場合を除く)。

ラベルでC用
In [209]: label = np.zeros((3,5)) 
In [210]: label 
Out[210]: 
array([[ 0., 0., 0., 0., 0.], 
     [ 0., 0., 0., 0., 0.], 
     [ 0., 0., 0., 0., 0.]]) 
In [211]: label[1,3] 
Out[211]: 0.0 
In [212]: label[1,3]=1  # index with integers OK 
In [213]: label[0,2]=1 
In [214]: label[0,'?'] =1 # index with a string - ERROR 
--------------------------------------------------------------------------- 
IndexError        Traceback (most recent call last) 
<ipython-input-214-3738f623c78e> in <module>() 
----> 1 label[0,'?'] =1 

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

In [215]: label[0,:] =2  # index with a slice 
In [216]: label 
Out[216]: 
array([[ 2., 2., 2., 2., 2.], 
     [ 0., 0., 0., 1., 0.], 
     [ 0., 0., 0., 0., 0.]]) 
関連する問題