<jupyter_start><jupyter_text>Multilayer PerceptronsMLPs aka (vanilla) feed-forward neural networks, or sometimes just neural networks1. MLPs can be viewed as generalizations of linear models that perform multiple stages of processing to come to a decision1. In an MLP weighted sums are computed multiple times from input to yield output1. Computing a series of weighted sums is mathematically the same as computing just one weighted sum, so to make this model truly more powerful than a linear model a nonlinear function is applied to the result: relu or tanh1. Data must be properly scaled before training**Disadvantages**1. Large Neural Networks take long to train1. They also require careful preprocessing of the data1. Similarly to SVMs, they work best with “homogeneous” data (Else we must use Tree based models)1. Tuning neural network parameters is also an art unto itself> http://scikit-learn.org/stable/modules/neural_networks_supervised.html<jupyter_code>import pandas as pd

from sklearn.neural_network import MLPClassifier

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score

import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set()
iris_df = pd.read_csv('../data/iris.csv', dtype = {'species': 'category'})
iris_df.head(3)
X = iris_df.iloc[:, :-1]
y = iris_df.species

X_train, X_test, y_train, y_test = train_test_split(X, y, stratify = y)

scaler = StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
mlp = MLPClassifier(solver = "lbfgs", max_iter = 10000, hidden_layer_sizes = (10, 100, 200), alpha = 10).fit(X_train, y_train)
mlp
y_pred = mlp.predict(X_test)
y_pred
accuracy_score(y_test, y_pred)
accuracy_score(y_train, mlp.predict(X_train))
sns.set_context('talk')

plt.figure(figsize = (15, 5))

sns.heatmap(mlp.coefs_[0], cmap = 'viridis')    # Heat Map of First Layer

plt.title("Heat Map of First Layer Weights")
plt.yticks([0.5, 1.5, 2.5, 3.5], iris_df.columns, rotation = 0)
plt.xlabel("Columns in Weight Matrix")
plt.ylabel("Input features")

plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup