質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

29027閲覧

IndexError

退会済みユーザー

退会済みユーザー

総合スコア0

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2017/05/27 12:09

IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices とエラーが出ました。

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の中身を見て見たのですが、

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.]]

と出力されました。
エラー文は、配列の要素はintegers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean は使えない、という意味だと思っていますが、配列の要素に数字(int)を入れたことはたくさんあるので、なぜここでエラーが出るのかわかりません。
どのように直せば良いのでしょうか?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

変数c は、文字列ですから、以下のどれにも当てはまりません。
integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays

= 質問のコードの理解 =
質問のコードの中の関連部分を遡りで解釈していくと以下のようになって、cが文字列であることは明らかです。

[label[i,c] = 1 の3行上]
for c in labels:で cは、配列labels の1つの要素の値になっています。
[その3行上と2行上]
line = raw.iloc[i]
labels = line[0].split(',')
linesは、rawの1行を、カンマ(',')で分割したものですから、文字列の一次元配列です。
[さらに4行上]
raw = pd.read_csv(essential_folder + 'rec_labels_test_hidden.txt', sep = ';')
rawは、セミコロン区切りのテキストファイルをcsvファイルのように読み込んだ文字列配列です。

== 推測に基づく解決策の提案 ==
label[i,c] という使い方をしてらっしゃいますから、cには数に見えるもの、1つ以上の数字から成る文字列が入っているのだと推測しました。
もしも、cが数を表す文字列なのであれば、数を表す文字列を整数に変換する関数 intを使って

Python

1 label[i,c] = 1

Python

1 label[i,int(c)] = 1

に修正すれば良いと思われます。

投稿2017/05/29 07:13

coco_bauer

総合スコア6915

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問