Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

MLPClassifier से प्राप्त लॉस वैल्यूज़ (loss_curve_) को Matplotlib में कैसे प्लॉट करें?


MLPClassifier से प्राप्त लॉस वैल्यूज़ (loss_curve_) को उचित तरीके से प्लॉट करने के लिए, हम निम्नलिखित चरण ले सकते हैं:

  • फिगर साइज सेट करें और सबप्लॉट्स के बीच और आसपास पैडिंग एडजस्ट करें।
  • पैरामीटर्स की एक लिस्ट, डिक्शनरीज़ की लिस्ट बनाएं।
  • लेबल्स और प्लॉट आर्गुमेंट्स की लिस्ट बनाएं।
  • nrows=2 और ncols= के साथ एक फिगर और सबप्लॉट्स का सेट बनाएं।
  • आइरिस डेटासेट (क्लासिफिकेशन) लोड और रिटर्न करें।
  • डेटासेट से x_digits और y_digits प्राप्त करें।
  • कस्टमाइज़्ड डेटा_सेट, टपल्स की लिस्ट प्राप्त करें।
  • ज़िप्ड, एक्सेस, डेटा_सेट्स और टाइटल्स के नाम की लिस्ट पर इटरेट करें।
  • plot_on_dataset() मेथड में; करंट एक्सिस का टाइटल सेट करें।
  • मल्टी-लेयर पर्सेप्ट्रॉन क्लासिफायर इंस्टेंस प्राप्त करें।
  • mlps प्राप्त करें, यानी mlpc इंस्टेंसेज की लिस्ट।
  • mlps पर इटरेट करें और plot() मेथड का उपयोग करके mlp.loss_curve_ प्लॉट करें।
  • फिगर प्रदर्शित करने के लिए, 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()

आउटपुट

MLPClassifier से प्राप्त लॉस वैल्यूज़ (loss_curve_) को Matplotlib में कैसे प्लॉट करें?

MLPClassifier से प्राप्त लॉस वैल्यूज़ (loss_curve_) को Matplotlib में कैसे प्लॉट करें?


  1. Matplotlib फिगर के मार्जिन्स कैसे सेट करें? Matplotlib फिगर के मार्जिन्स कैसे सेट करें?

    Matplotlib फिगर के मार्जिन्स सेट करने के लिए margins() मेथड का उपयोग करें। चरण फिगर का आकार सेट करें और सबप्लॉट्स के बीच पैडिंग एडजस्ट करें। NumPy का उपयोग करके t और y डेटा पॉइंट्स बनाएं। इंडेक्स 1 पर करंट फिगर में सबप्लॉट जोड़ें। plot() मेथड का उपयोग करके t और y डेटा पॉइंट्स प्लॉट करें। प्लॉट का

  1. Matplotlib प्लॉट से डेटा कैसे एक्सट्रैक्ट करें? Matplotlib प्लॉट से डेटा कैसे एक्सट्रैक्ट करें?

    प्लॉट किए गए लाइन ऑब्जेक्ट से get_xdata() और get_ydata() मेथड्स से डेटा वापस प्राप्त करें। चरण फिगर साइज़ और ऑटोलेआउट सेट करें। y डेटा से प्लॉट करें और लाइन ऑब्जेक्ट को वेरिएबल में स्टोर करें (curve, = plt.plot(...))। curve.get_xdata() और curve.get_ydata() से डेटा एक्सट्रैक्ट करें। प्रिंट करके वेरि

  1. Matplotlib में NaN मानों के साथ प्लॉट और काम कैसे करें? Matplotlib में NaN मानों के साथ प्लॉट और काम कैसे करें?

    Matplotlib में NaN मानों के साथ प्लॉट और काम करने के लिए, हम निम्नलिखित चरण अपना सकते हैं: कुछ NaN मानों के साथ NumPy का उपयोग करके डेटा बनाएं। क्लरमैप और डेटा (चरण 1 से) के साथ डेटा को इमेज के रूप में डिस्प्ले करने के लिए imshow() मेथड का उपयोग करें, यानी 2D रेगुलर रास्टर पर। फिगर डिस्प्ले क