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

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

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

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

1回答

3736閲覧

string から float に配列全体をキャストしたい

退会済みユーザー

退会済みユーザー

総合スコア0

NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2017/05/29 02:51

編集2017/05/30 06:19

ValueError: could not convert string to float: 'rec_id'
とエラーが出ました。
コードには

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 Name Setting ########################### 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/' ################################################### ## Read the Essential Data ## labels, training-test split,file_names etc. ################################################### # 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]) label = np.zeros((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[i]: #if(c =="?"): #label[i,c] = 1 label = pd.DataFrame(label) label['rec_id'] = cv.rec_id label['fold'] = cv.fold label['filename'] = rec2f.filename # Sparse training set # training species 1%--5%--20% spec_avg = label[label.fold ==0][:num_species].mean() spec_avg.sort_values(inplace=True) plt.plot(spec_avg,'go')

と書きました。
Tracebackには

/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/skimage/filter/__init__.py:6: skimage_deprecation: The `skimage.filter` module has been renamed to `skimage.filters`. This placeholder module will be removed in v0.13. warn(skimage_deprecation('The `skimage.filter` module has been renamed ' Traceback (most recent call last): File "test1.py", line 67, in <module> plt.plot(spec_avg,'go') File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3317, in plot ret = ax.plot(*args, **kwargs) File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/__init__.py", line 1898, in inner return func(ax, *args, **kwargs) File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 1407, in plot self.add_line(line) File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 1793, in add_line self._update_line_limits(line) File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 1815, in _update_line_limits path = line.get_path() File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/lines.py", line 989, in get_path self.recache() File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/lines.py", line 676, in recache x = np.asarray(xconv, np.float_) File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/numpy/core/numeric.py", line 531, in asarray return array(a, dtype, copy=False, order=order) ValueError: could not convert string to float: 'rec_id'

と表示されました。 'rec_id'には16.157895 の値が入っていて、配列全体をstring から float にキャストするにはどうしたら良いのでしょうか?どう書けば良いのでしょうか?

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

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

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

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

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

guest

回答1

0

/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/skimage/filter/init.py:6: skimage_deprecation: The skimage.filter module has been renamed to skimage.filters. This placeholder module will be removed in v0.13.

warn(skimage_deprecation('The skimage.filter module has been renamed '

Tracebackに書かれているのは、「'skimage.filter'というモジュールは、'skimage.filters'に改名されています」という注意です。

だから、

from skimage.filter import rank

from skimage.filters import rank

に変更すれば良いのではないでしょうか?

v0.10からv0.11に変わる際に改名されたようです(以下の記事の3.3.2.1. データ型の章の注意書き)
3.3. Scikit-image: 画像処理

投稿2017/05/30 09:17

coco_bauer

総合スコア6915

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

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

退会済みユーザー

退会済みユーザー

2017/05/30 10:55

ありがとうございます。 Traceback (most recent call last): File "test1.py", line 68, in <module> plt.plot(spec_avg,'go') File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3317, in plot ret = ax.plot(*args, **kwargs) File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/__init__.py", line 1898, in inner return func(ax, *args, **kwargs) File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 1407, in plot self.add_line(line) File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 1793, in add_line self._update_line_limits(line) File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 1815, in _update_line_limits path = line.get_path() File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/lines.py", line 989, in get_path self.recache() File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/lines.py", line 676, in recache x = np.asarray(xconv, np.float_) File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/numpy/core/numeric.py", line 531, in asarray return array(a, dtype, copy=False, order=order) ValueError: could not convert string to float: 'rec_id'
退会済みユーザー

退会済みユーザー

2017/05/30 10:56

直したのですが、まだエラーが出ます。どういう所が悪いのでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問