IT Learning

実践形式でITのお勉強

Python

PythonでAttributeError: module ‘pandas’ has no attribute ‘scatter_matrix’が出た時の対処法

投稿日:2022年1月3日 更新日:

概要

オライリーのPythonで始める機械学習の第一章のIrisデータセットをグラフ表示するサンプルコードを動かそうとしていたところ以下のエラーに遭遇しました。

grr = pd.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15), marker='o', hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)

--------------------------------
AttributeError: module 'pandas' has no attribute 'scatter_matrix'

対象とした書籍

今回は上記エラーの対処方法についてメモしていきます。

検証環境

  • Python 3.8.10
from platform import python_version

print(python_version())
-------------
3.8.10
  • Pandas 1.3.5
import pandas as pd

print(pd.__version__)

---------------
1.3.5

エラーの原因

Stack OVerflowを参考にさせていただいたところ、どうやらscatter_matrixメソッドはpandasオブジェクトではなく、plottingオブジェクトから呼び出さなければならなかったようです。

python 3.x – How can I solve ” module ‘pandas’ has no attribute ‘scatter_matrix’ ” error? – Stack Overflow

pandas Documationを見てもそのようでした。

pandas.plotting.scatter_matrix
pandas.plotting.scatter_matrix(framealpha=0.5figsize=Noneax=Nonegrid=Falsediagonal=’hist’marker=’.’density_kwds=Nonehist_kwds=Nonerange_padding=0.05**kwargs)

pandas.plotting.scatter_matrix — pandas 1.3.5 documentation (pydata.org)

対処法

以下のように書き換えたところオライリーのサンプルコードは問題なく動きました。

grr = pd.plotting.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15), marker='o', hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)

まとめ

オライリーのサンプルコードが想定しているpandasのバージョンと検証環境のpandasのバージョンの違いによっての差なのかはわかりませんがとりあえず解決しました。

Related

-Python
-,

執筆者:


comment

メールアドレスが公開されることはありません。 が付いている欄は必須項目です