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

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

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

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

Q&A

1回答

1400閲覧

could not convert string to float: 'Iris-virginica' がどう解決されるのかわかりません(df.replaceを使用してもダメでした)

afternoon

総合スコア4

Python

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

0グッド

0クリップ

投稿2020/03/15 07:47

前提・実現したいこと

各アルゴリズムの正解率を比較するプログラムを書きたい

発生している問題・エラーメッセージ

AdaBoostClassifier True score = 0.9666666666666667
BaggingClassifier True score = 0.9666666666666667
BernoulliNB True score = 0.26666666666666666
CalibratedClassifierCV True score = 0.9
CategoricalNB True score = 0.9666666666666667

ValueError Traceback (most recent call last)
<ipython-input-4-4a7b5b59e8f3> in <module>()
25 for(name, algorithm) in allAlgorithms:
26 clf = algorithm()
---> 27 clf.fit(x_train, y_train)
28 y_pred = clf.predict(x_test)
29 print(name, " True score = ", accuracy_score(y_test, y_pred))

6 frames
/usr/local/lib/python3.6/dist-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
83
84 """
---> 85 return array(a, dtype, copy=False, order=order)
86
87

ValueError: could not convert string to float: 'Iris-virginica'

該当のソースコード

from sklearn.model_selection import train_test_split
from sklearn.utils.testing import all_estimators
from sklearn.metrics import accuracy_score
import warnings
import urllib.request as req
import pandas as pd

url = "https://raw.githubusercontent.com/pandas-dev/pandas/master/pandas/tests/data/iris.csv"
savefile = "iris.csv"
req.urlretrieve(url, savefile)
iris_data = pd.read_csv(savefile, encoding="utf-8")

y = iris_data.loc[:, "Name"]
y.replace({"Name": {"Iris-setosa": 0}})
y.replace({"Name": {"Iris-versicolor": 1}})
y.replace({"Name": {"Iris-virginica": 2}})

x = iris_data.loc[:, ["SepalLength", "SepalWidth", "PetalLength", "PetalWidth"]]

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, train_size = 0.8, shuffle = True)

warnings.filterwarnings("ignore")
allAlgorithms = all_estimators(type_filter="classifier")

for(name, algorithm) in allAlgorithms:
clf = algorithm()
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
print(name, " True score = ", accuracy_score(y_test, y_pred))

試したこと

「could not convert string to float: 'Iris-virginica' がどう解決されるのかわかりません」
という質問の中にreplace関数を使ったらどうかという回答があったので、それで試してみたのですがそれでもだめでした。また、何回か動作させるとエラーの"Iris-virginica"の部分が"Iris-setosa"になったり、"Iris-versicolor"になったりしたのですがこれはどういうことでしょうか。

補足情報(FW/ツールのバージョンなど)

使っている環境はcolaboratoryで、
iris_dataのファイルは"https://raw.githubusercontent.com/pandas-dev/pandas/master/pandas/tests/data/iris.csv"です。

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

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

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

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

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

guest

回答1

0

y = pd.Categorical(y).codesにてカテゴリ化したコード(数値)を得られます。
なお、上記をおこなっても分類器によっては引数不足などでエラーが発生します。
エラーを回避するにはsciki-learnのall_estimators等で多くの分類器を動かしてみましたに記載されているようにエラーを捕捉するなどすればよいです。

また、何回か動作させるとエラーの"Iris-virginica"の部分が"Iris-setosa"になったり、"Iris-versicolor"になったりしたのですがこれはどういうことでしょうか。

train_test_splitで乱数でデータがシャッフルされているからです。

投稿2020/03/15 09:05

編集2020/03/15 09:10
can110

総合スコア38234

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問