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

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

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

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python

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

Q&A

1回答

785閲覧

パーセプトロンの学習結果をmatplotlibで表示できない

fujiji

総合スコア6

Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python

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

0グッド

0クリップ

投稿2017/08/14 05:46

###前提・実現したいこと
パーセプトロンの学習結果をmatplotlibで表示したいのですがattribute errorがでますが原因がわかりません

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

4 あああああああああ Traceback (most recent call last): File "ppn.py", line 92, in <module> plot_decision_regions(X=X_combined_std, y=y_combined, classifier=ppn, test_idx=range(105,150)) File "ppn.py", line 73, in plot_decision_regions plt.scatter(x=X[y==cl, 0],y=X[y==cl,1],alpha=0.8, c=cmap(idx),marker=markers[idx], lavel=cl) File "/Users/admin/.pyenv/versions/3.5.0/lib/python3.5/site-packages/matplotlib/pyplot.py", line 3434, in scatter edgecolors=edgecolors, data=data, **kwargs) File "/Users/admin/.pyenv/versions/3.5.0/lib/python3.5/site-packages/matplotlib/__init__.py", line 1898, in inner return func(ax, *args, **kwargs) File "/Users/admin/.pyenv/versions/3.5.0/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 4037, in scatter collection.update(kwargs) File "/Users/admin/.pyenv/versions/3.5.0/lib/python3.5/site-packages/matplotlib/artist.py", line 885, in update for k, v in props.items()] File "/Users/admin/.pyenv/versions/3.5.0/lib/python3.5/site-packages/matplotlib/artist.py", line 885, in <listcomp> for k, v in props.items()] File "/Users/admin/.pyenv/versions/3.5.0/lib/python3.5/site-packages/matplotlib/artist.py", line 878, in _update_property raise AttributeError('Unknown property %s' % k) AttributeError: Unknown property lavel

###該当のソースコード

python

1 2from matplotlib.colors import ListedColormap 3import matplotlib.pyplot as plt 4import numpy as np 5from sklearn import datasets 6from sklearn.model_selection import train_test_split 7from sklearn.preprocessing import StandardScaler 8from sklearn.linear_model import Perceptron 9 10iris = datasets.load_iris()#辞書型っぽい,キーで検索できる 11 12 13#irisデータセットの特徴量を2つだけ使用 14X = iris.data[:,[2,3]] 15#yはラベル 16y = iris.target 17 18# random_stateは毎回同じデータを生成するために指定 19X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3, random_state= 0) 20 21sc = StandardScaler() 22X_train_std = sc.fit_transform(X_train) 23X_test_std = sc.transform(X_test) 24 25 26ppn = Perceptron(n_iter=40, eta0=0.1, random_state=0, shuffle=True) 27ppn.fit(X_train_std, y_train) 28 29y_pred = ppn.predict(X_test_std) 30print((y_test != y_pred).sum()) 31 32 33 34 35 36#作図 37def plot_decision_regions(X, y, classifier, test_idx=None, resolution=0.02): 38 39 markers = ('s', 'x', 'o', '^', 'v') 40 colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan') 41 cmap = ListedColormap(colors[:len(np.unique(y))]) 42 43 44 x1_min, x1_max = X[:, 0].min() -1, X[:, 0].max() + 1 45 x2_min, x2_max = X[:, 1].min() -1, X[:, 1].max() + 1 46 47 48 xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),np.arange(x2_min, x2_max, resolution)) 49 50 Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T) 51 52 Z = Z.reshape(xx1.shape) 53 54 plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap) 55 56 plt.xlim(xx1.min(), xx1.max()) 57 plt.ylim(xx2.min(), xx2.max()) 58 59 for idx, cl in enumerate(np.unique(y)): 60 plt.scatter(x=X[y==cl, 0],y=X[y==cl,1],alpha=0.8, c=cmap(idx),marker=markers[idx], lavel=cl) 61 62 63 if test_idx: 64 X_test, y_test = X[test_idx, :], y[test_idx] 65 plt.scatter(X_test[:, 0], X_test[:, 1], c='', alpha=1.0, linewidths=1, marker='o',s=55, label='test set') 66 67 68 69 70 71 72#トレーニングデータとテストデータの特徴量を行方向に結合 73X_combined_std = np.vstack((X_train_std, X_test_std)) 74# トレーニングデータとテストデータのクラスラベルを結合 75y_combined = np.hstack((y_train, y_test)) 76print("あああああああああ") 77 78# 決定境界のプロット 79plot_decision_regions(X=X_combined_std, y=y_combined, classifier=ppn, test_idx=range(105,150)) 80 81print("いいいいいいいいい") 82 83 84# 軸ラベルの設定 85plt.xlabel('petal length [standardized]') 86plt.ylabel('petal length [standardized]') 87print("ううううううううううう") 88 89plt.legend(loc='upper left') 90 91plt.show() 92 93 94 95 96

###試したこと
plot_decision_regionsのところでエラーが出ています
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報

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

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

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

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

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

guest

回答1

0

原因は誤字です。下記コードのlavel=cllabel=clしてください。

Python

1 plt.scatter(x=X[y==cl, 0],y=X[y==cl,1],alpha=0.8, c=cmap(idx),marker=markers[idx], lavel=cl)

Python

1 plt.scatter(x=X[y==cl, 0],y=X[y==cl,1],alpha=0.8, c=cmap(idx),marker=markers[idx], label=cl)

投稿2017/08/14 05:54

can110

総合スコア38233

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問