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

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

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

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Python 3.x

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

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Q&A

解決済

2回答

4197閲覧

ValueError: could not convert string to float: 'Left' のエラーを解決したい

YY2

総合スコア5

Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Python 3.x

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

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

0グッド

0クリップ

投稿2021/02/17 10:52

#実現したいこと
ネットよりデータベースを取り込み、分析するツールを作りたく、調べながら、その雛形を作成中です。

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

ValueError Traceback (most recent call last) <ipython-input-60-071ac6e38240> in <module> 6 #データの標準化を行う 7 sc=preprocessing.StandardScaler() ----> 8 sc.fit(x_list2) 9 10 X=sc.transform(x_list2) ~/opt/anaconda3/lib/python3.7/site-packages/sklearn/preprocessing/data.py in fit(self, X, y) 637 # Reset internal state before fitting 638 self._reset() --> 639 return self.partial_fit(X, y) 640 641 def partial_fit(self, X, y=None): ~/opt/anaconda3/lib/python3.7/site-packages/sklearn/preprocessing/data.py in partial_fit(self, X, y) 661 X = check_array(X, accept_sparse=('csr', 'csc'), copy=self.copy, 662 estimator=self, dtype=FLOAT_DTYPES, --> 663 force_all_finite='allow-nan') 664 665 # Even in the case of `with_mean=False`, we update the mean anyway ~/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator) 494 try: 495 warnings.simplefilter('error', ComplexWarning) --> 496 array = np.asarray(array, dtype=dtype, order=order) 497 except ComplexWarning: 498 raise ValueError("Complex data not supported\n" ~/opt/anaconda3/lib/python3.7/site-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: 'Left'

#該当のソースコード

import pandas as pd input_book = pd.ExcelFile('FIFA19_data.xlsx') input_sheet_name = input_book.sheet_names num_sheet = len(input_sheet_name) print(input_sheet_name) print("sheet の数:", num_sheet) input_sheet_df = input_book.parse(input_sheet_name[0]) input_sheet_df = input_sheet_df[input_sheet_df['Position']!= "GK"] input_sheet_df.head(10) import numpy as np #データを読み込む age=input_sheet_df.Age #年齢 overall=input_sheet_df.Overall #総合能力 wage=input_sheet_df.Wage #給与 PreferredFoot=input_sheet_df.PreferredFoot #利き足 Reputation=input_sheet_df.Reputation #レピュテーション least_contract=input_sheet_df.least_contract #残りの契約年数 Height=input_sheet_df.Height #身長 Weight=input_sheet_df.Weight #体重 crossing=input_sheet_df.Crossing #クロス精度 Finishing=input_sheet_df.Finishing #フィニッシュ精度 heading=input_sheet_df.HeadingAccuracy #ヘディング精度 ShortPassing=input_sheet_df.ShortPassing #ショートパス精度 Dribbling=input_sheet_df.Dribbling #ドリブルの精度 Curve=input_sheet_df.Curve #カーブの精度 FKAccuracy=input_sheet_df.FKAccuracy #FK の精度 LongPassing=input_sheet_df.LongPassing #ロングパスの精度 BallControl=input_sheet_df.BallControl #ボールコントロール Acceleration=input_sheet_df.Acceleration #飛び出し SprintSpeed=input_sheet_df.SprintSpeed #スプリントスピード Agility=input_sheet_df.Agility #アジリティ Reactions=input_sheet_df.Reactions #リアクション Balance=input_sheet_df.Balance #バランス ShotPower=input_sheet_df.ShotPower #シュートパワー stamina=input_sheet_df.Stamina #スタミナ Jumping=input_sheet_df.Jumping #ジャンプ Strength=input_sheet_df.Strength #ストレングス LongShots=input_sheet_df.LongShots #ロングシュート Aggression=input_sheet_df.Aggression #アグレッション Interceptions=input_sheet_df.Interceptions #インターセプト Positioning=input_sheet_df.Positioning Vision=input_sheet_df.Vision Penalties=input_sheet_df.Penalties Composure=input_sheet_df.Composure Marking=input_sheet_df.Marking StandingTackle=input_sheet_df.StandingTackle SlidingTackle=input_sheet_df.SlidingTackle #利用するパラメータを指定する equation_df2=pd.concat([wage, age, PreferredFoot, Reputation, least_contract, Height, Weight, \ crossing, Finishing, heading, ShortPassing, dribbling, Curve, FKAccuracy, \ LongPassing, BallControl, Acceleration, SprintSpeed, Agility, Reactions, \ Balance, ShotPower, stamina, Jumping, Strength, LongShots, Aggression, \ Interceptions, Positioning, Vision, Penalties, Composure, Marking, \ StandingTackle, SlidingTackle], axis=1) #被説明変数として利用するものを取り出す wage2 = pd.DataFrame(equation_df2.Wage) #被説明変数を抜き取る x_list2 = equation_df2.drop("Wage",1) #異常値やnull になっている値を除去する x_list2 = x_list2.drop(x_list2.columns[np.isnan(x_list2).any()], axis=1) from sklearn import preprocessing, linear_model import sklearn import seaborn as sns #データの整形を行う #データの標準化を行う sc=preprocessing.StandardScaler() sc.fit(x_list2) X=sc.transform(x_list2) #相関係数を確認する plt.figure(figsize=(30,24)) sns.heatmap(x_list2.pct_change().corr(), annot=True, cmap='Blues')

#読み込むデータファイル
https://www.dropbox.com/s/41lap8qzcxez33o/FIFA19_data.xlsx?dl=0

#自分で調べたことや試したこと
類例と解決方法を探しながら進めておりますが、本エラーはうまく見つけられませんでした。
先週末にPython/notebookを初め、DLもその際にしましたので、macの最新version(3.7?)を使用しています。
初心者で詳しくないですが、おそらく、このvalue errorのため?、途中でコードの実行が止まっているいるかと思われます。
詳しい人でしたら、すぐに原因がわかる内容かもしれませんが、お助けくださると感謝いたします。

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

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

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

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

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

guest

回答2

0

ベストアンサー

用するパラメータにPreferredFootを入れているために、文字列が混入しているようです。
取り除くか、0と1に数値化するかしましょう。

投稿2021/02/17 11:21

ppaul

総合スコア24666

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

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

YY2

2021/02/17 12:44 編集

ありがとうございます。修正してみます!
guest

0

google翻訳
ValueError:文字列をfloatに変換できませんでした: 'Left'

ということだそうなので、エラーでてる行の変数のナカミをチェックしてみては

投稿2021/02/17 10:55

y_waiwai

総合スコア87774

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

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

YY2

2021/02/17 12:11

ありがとうございます。調べますと、 このコードに対して、 ``` #異常値やnull になっている値を除去する x_list2 = x_list2.drop(x_list2.columns[np.isnan(x_list2).any()], axis=1) ``` 以下のエラーが出ますね。。 ``` TypeError Traceback (most recent call last) <ipython-input-59-82c97bad44aa> in <module> 53 54 #異常値やnull になっている値を除去する ---> 55 x_list2 = x_list2.drop(x_list2.columns[np.isnan(x_list2).any()], axis=1) TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' ``` このコードが問題なく、動きますと、Value Errorも直るのでしょうか?
y_waiwai

2021/02/17 12:18

それが異常だ、ということがわかったのなら、そこんところを修正して、想定のように動くようにしましょう
YY2

2021/02/17 12:22

ありがとうございます。 この'isnan'以外のコードで書けば、改善するのか?と思い、調査中なのですが、知識不足で、、苦労してます。
y_waiwai

2021/02/17 12:25

x_list2の値が異常なら、異常でないようにする必要があるんでは 後付でいじってもどーしよーもないですよ
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問