<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 mlxtend - Multilayer Perceptron Examples
Sections
Classify Iris
Classify handwritten digits from MNIST
<br>
<br>
Classify Iris
Load 2 features from Iris (petal length and petal width) for visualization purposes.
Step1:  Train neural network for 3 output flower classes ('Setosa', 'Versicolor', 'Virginica'), regular gradient decent (minibatches=1), 30 hidden units, and no regularization.
Step2:  Now, check if the gradient descent converged after 5000 epochs, and choose smaller learning rate (eta) otherwise.
Step3:  Standardize features for smoother and faster convergence.
Step4:  Visualize the decision regions.
Step5:  <br>
<br>
Classify handwritten digits from MNIST
Load a 5000-sample subset of the MNIST dataset.
Step6:  Visualize a sample from the MNIST dataset.
Step7:  Initialize the neural network to recognize the 10 different digits (0-10) using 300 epochs and minibatch learning.
Step8:  Learn the features while printing the progress to get an idea about how long it may take.
Step9:  Check for convergence.
<ASSISTANT_TASK:>
Python Code:
from mlxtend.data import iris_data
X, y = iris_data()
X = X[:, 2:]
Explanation: mlxtend - Multilayer Perceptron Examples
Sections
Classify Iris
Classify handwritten digits from MNIST
<br>
<br>
Classify Iris
Load 2 features from Iris (petal length and petal width) for visualization purposes.
End of explanation
from mlxtend.classifier import NeuralNetMLP
import numpy as np
nn1 = NeuralNetMLP(n_output=3, 
         n_features=X.shape[1], 
         n_hidden=30, 
         l2=0.0, 
         l1=0.0, 
         epochs=5000, 
         eta=0.001, 
         alpha=0.00,
         minibatches=1, 
         shuffle=True,
         random_state=0)
nn1.fit(X, y)
y_pred = nn1.predict(X)
acc = np.sum(y == y_pred, axis=0) / X.shape[0]
print('Accuracy: %.2f%%' % (acc * 100))
Explanation: Train neural network for 3 output flower classes ('Setosa', 'Versicolor', 'Virginica'), regular gradient decent (minibatches=1), 30 hidden units, and no regularization.
End of explanation
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(range(len(nn1.cost_)), nn1.cost_)
plt.ylim([0, 300])
plt.ylabel('Cost')
plt.xlabel('Epochs')
plt.grid()
plt.show()
Explanation: Now, check if the gradient descent converged after 5000 epochs, and choose smaller learning rate (eta) otherwise.
End of explanation
X_std = np.copy(X)
for i in range(2):
    X_std[:,i] = (X[:,i] - X[:,i].mean()) / X[:,i].std()
nn2 = NeuralNetMLP(n_output=3, 
         n_features=X_std.shape[1], 
         n_hidden=30, 
         l2=0.0, 
         l1=0.0, 
         epochs=1000, 
         eta=0.05,
         alpha=0.1,
         minibatches=1, 
         shuffle=True,
         random_state=1)
nn2.fit(X_std, y)
y_pred = nn2.predict(X_std)
acc = np.sum(y == y_pred, axis=0) / X_std.shape[0]
print('Accuracy: %.2f%%' % (acc * 100))
plt.plot(range(len(nn2.cost_)), nn2.cost_)
plt.ylim([0, 300])
plt.ylabel('Cost')
plt.xlabel('Epochs')
plt.show()
Explanation: Standardize features for smoother and faster convergence.
End of explanation
from mlxtend.evaluate import plot_decision_regions
plot_decision_regions(X, y, clf=nn1)
plt.xlabel('petal length [cm]')
plt.ylabel('petal width [cm]')
plt.show()
Explanation: Visualize the decision regions.
End of explanation
from mlxtend.data import mnist_data
X, y = mnist_data()
Explanation: <br>
<br>
Classify handwritten digits from MNIST
Load a 5000-sample subset of the MNIST dataset.
End of explanation
def plot_digit(X, y, idx):
    img = X[idx].reshape(28,28)
    plt.imshow(img, cmap='Greys',  interpolation='nearest')
    plt.title('true label: %d' % y[idx])
    plt.show()
plot_digit(X, y, 4)    
Explanation: Visualize a sample from the MNIST dataset.
End of explanation
nn = NeuralNetMLP(n_output=10, n_features=X.shape[1], 
         n_hidden=100, 
         l2=0.0, 
         l1=0.0, 
         epochs=300, 
         eta=0.0005,
         alpha=0.0,
         minibatches=50, 
         random_state=1)
Explanation: Initialize the neural network to recognize the 10 different digits (0-10) using 300 epochs and minibatch learning.
End of explanation
nn.fit(X, y, print_progress=True)
y_pred = nn.predict(X)
acc = np.sum(y == y_pred, axis=0) / X.shape[0]
print('Accuracy: %.2f%%' % (acc * 100))
Explanation: Learn the features while printing the progress to get an idea about how long it may take.
End of explanation
plt.plot(range(len(nn.cost_)), nn.cost_)
plt.ylim([0, 500])
plt.ylabel('Cost')
plt.xlabel('Mini-batches * Epochs')
plt.show()
plt.plot(range(len(nn.cost_)//50), nn.cost_[::50], color='red')
plt.ylim([0, 500])
plt.ylabel('Cost')
plt.xlabel('Epochs')
plt.show()
Explanation: Check for convergence.
End of explanation
<END_TASK>