MLPClassifierから(loss_curve_)によって取得された損失値を適切にプロットするにはどうすればよいですか? (Matplotlib)
MLPCIassifierから(loss_curve_)によって取得された損失値を適切にプロットするには、次の手順を実行できます-
- 図のサイズを設定し、サブプロット間およびサブプロットの周囲のパディングを調整します。
- 辞書のリストであるパラメータを作成します。
- ラベルのリストを作成し、引数をプロットします。
- nrows=2およびncols= を使用して、図とサブプロットのセットを作成します
- アイリスデータセットを読み込んで返します(分類)。
- データセットからx_digitsとy_digitsを取得します。
- カスタマイズされたdata_set、タプルのリストを取得します。
- zipped、axes、data_sets、およびタイトル名のリストを繰り返します。
- plot_on_dataset() 方法;現在の軸のタイトルを設定します。
- 多層パーセプトロン分類子インスタンスを取得します。
- mlpsを入手する 、つまりmlpcインスタンスのリスト。
- mlpsを繰り返します mlp.loss_curve _をプロットします plot()を使用する メソッド。
- 図を表示するには、 show()を使用します メソッド。
例
import warnings
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import MinMaxScaler
from sklearn import datasets
from sklearn.exceptions import ConvergenceWarning
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
params = [{'solver': 'sgd', 'learning_rate': 'constant', 'momentum': 0, 'learning_rate_init': 0.2},
{'solver': 'sgd', 'learning_rate': 'constant', 'momentum': .9, 'nesterovs_momentum': False, 'learning_rate_init': 0.2},
{'solver': 'sgd', 'learning_rate': 'constant', 'momentum': .9, 'nesterovs_momentum': True, 'learning_rate_init': 0.2},
{'solver': 'sgd', 'learning_rate': 'invscaling', 'momentum': 0, 'learning_rate_init': 0.2},
{'solver': 'sgd', 'learning_rate': 'invscaling', 'momentum': .9, 'nesterovs_momentum': True, 'learning_rate_init': 0.2},
{'solver': 'sgd', 'learning_rate': 'invscaling', 'momentum': .9, 'nesterovs_momentum': False, 'learning_rate_init': 0.2},
{'solver': 'adam', 'learning_rate_init': 0.01}]
labels = ["constant learning-rate", "constant with momentum", "constant with Nesterov's momentum", "inv-scaling learning-rate", "inv-scaling with momentum", "inv-scaling with Nesterov's momentum", "adam"]
plot_args = [{'c': 'red', 'linestyle': '-'},
{'c': 'green', 'linestyle': '-'},
{'c': 'blue', 'linestyle': '-'},
{'c': 'red', 'linestyle': '--'},
{'c': 'green', 'linestyle': '--'},
{'c': 'blue', 'linestyle': '--'},
{'c': 'black', 'linestyle': '-'}]
def plot_on_dataset(X, y, ax, name):
ax.set_title(name)
X = MinMaxScaler().fit_transform(X)
mlps = []
if name == "digits":
max_iter = 15
else:
max_iter = 400
for label, param in zip(labels, params):
mlp = MLPClassifier(random_state=0, max_iter=max_iter, **param)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=ConvergenceWarning, module="sklearn")
mlp.fit(X, y)
mlps.append(mlp)
for mlp, label, args in zip(mlps, labels, plot_args):
ax.plot(mlp.loss_curve_, label=label, **args)
fig, axes = plt.subplots(2, 2)
iris = datasets.load_iris()
X_digits, y_digits = datasets.load_digits(return_X_y=True)
data_sets = [(iris.data, iris.target), (X_digits, y_digits), datasets.make_circles(noise=0.2, factor=0.5, random_state=1), datasets.make_moons(noise=0.3, random_state=0)]
for ax, data, name in zip(axes.ravel(), data_sets,
['iris', 'digits', 'circles', 'moons']):
plot_on_dataset(*data, ax=ax, name=name)
fig.legend(ax.get_lines(), labels, ncol=3, loc="upper center")
plt.show() 出力
-
Matplotlibプロットからデータを抽出する方法は?
matplotlibのプロットからデータを抽出するには、 get_xdata()を使用できます。 およびget_ydata() メソッド。 ステップ 図のサイズを設定し、サブプロット間およびサブプロットの周囲のパディングを調整します。 numpyを使用してy個のデータポイントを作成します。 yデータポイントをcolor=redでプロットします およびlinewidth=5 。 データ抽出用のステートメントを印刷します。 get_xdata()を使用します およびget_ydata() プロットからデータを抽出する方法(ステップ3)。 xおよびyデータを印刷します(ステップ5)。 図を
-
MatplotlibでNaN値をプロットして操作する方法は?
matplotlibでNaN値をプロットして操作するには、次の手順を実行できます- いくつかのNaN値を持つnumpyを使用してデータを作成します。 imshow()を使用します データを画像として、つまり2D通常のラスター上に、カラーマップとデータを使用して表示する方法(手順1から)。 図を表示するには、 show()を使用します メソッド。 例 import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50]