4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

機械学習エラー対応集

Last updated at Posted at 2021-05-06

はじめに

機械学習・データ分析のプログラミングで遭遇したエラーや警告をメモしてみました。随時更新予定です。
こういうエラーあるよ!とかこういう対処法の方がいいよ!とかあれば是非コメント下さい。

AttributeError: 'DataFrame' object has no attribute 'as_matrix'

.as_matrixは古い仕様のものなので、.valuesを使用してください。

# 警告
Python: Method .as_matrix will be removed in a future version. Use .values instead 
# エラー
AttributeError: 'DataFrame' object has no attribute 'as_matrix' 

対処法
.asmatrix() を .valuesに 書き換える

# 書き換え前
X = df.loc[:, ['temp']].as_matrix()
# 書き換え後                
X = df.loc[:, ['temp']].values

ValueError: Input contains NaN, infinity or a value too large for dtype('float64').

入力データにNaNや無限大が含まれています。

# エラー
ValueError: Input contains NaN, infinity or a value too large for dtype('float64').

対処法
入力データからNaNや無限大を除去する

# X から NaN を含む列を削除する
X.drop(X.columns[np.isnan(X).any()], axis=1)

ImportError: Install xlrd >= 1.0.0 for Excel support

xlrdがインストールされていないのでエクセルファイルが開けません。

# エラー
ImportError: Install xlrd >= 1.0.0 for Excel support

対処法
xlrdをインストールする

# xlrdのインストール
$ pip install xlrd

ValueError: Your version of xlrd is 2.0.1. In xlrd >= 2.0, only the xls format is supported. Install openpyxl instead.

拡張子が.xlsxのエクセルファイルはこのままでは読み込めません。

# エラー
ValueError: Your version of xlrd is 2.0.1. In xlrd >= 2.0, only the xls format is supported. Install openpyxl instead.

対処法
①openpyxlをインストールする

# openpyxlのインストール
$ pip install openpyxl

②プログラムのread_excel()メソッドの第2引数にengine='openpyxl'を指定する

UserWarning: Using a target size (torch.Size([3])) that is different to the input size (torch.Size([3, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.

入力値の形と正解値の形が異なっているのでどっちかに合わせてください。(PyTorch)

# 警告
UserWarning: Using a target size (torch.Size([3])) that is different to the input size (torch.Size([3, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.

対処法
unsqueezeを使って入力値の形状を、N x 1 の形に変更する。

# 形を変更する
y = y.unsqueeze(1)

TypeError: ('Keyword argument not understood:', 'border_mode')

border_modeは古い仕様のものなので、paddingを使用してください。
(border_modeを使用しているサイトや本は情報が古い可能性が高いので、できれば別のサイトを参考にした方がいいかもです…自分はまるっと変更しました)

# エラー
TypeError: ('Keyword argument not understood:', 'border_mode')

対処法
border_mode を padding に書き換える

# 書き換え前
model.add(Convolution2D(n_filter, 3,3, border_mode='same', 
                        input_shape = (1, size_img, size_img)))
# 書き換え後                
model.add(Convolution2D(n_filter, 3,3, padding='same', 
                        input_shape = (1, size_img, size_img)))

◆参考サイト

4
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?