#!/usr/bin/env python # coding: utf-8 # ##
Neural Networks in scikit-learn
# # #### Linear models: # #
ลท = w[0] \* x[0] + w[1] \* x[1] + ... + w[p] \* x[p] + b
# In[8]: import mglearn import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') mglearn.plots.plot_logistic_regression_graph() # In[2]: mglearn.plots.plot_single_hidden_layer_graph() # In[3]: mglearn.plots.plot_two_hidden_layer_graph() # In[4]: from sklearn.neural_network import MLPClassifier from sklearn.datasets import load_breast_cancer from sklearn.model_selection import train_test_split cancer = load_breast_cancer() X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, random_state=0) mlp = MLPClassifier(random_state=42) mlp.fit(X_train, y_train) print('Accuracy on the training subset: {:.3f}'.format(mlp.score(X_train, y_train))) print('Accuracy on the test subset: {:.3f}'.format(mlp.score(X_test, y_test))) # In[5]: print('The maximum per each feature:\n{}'.format(cancer.data.max(axis=0))) # In[6]: from sklearn.preprocessing import StandardScaler scaler = StandardScaler() X_train_scaled = scaler.fit(X_train).transform(X_train) X_test_scaled = scaler.fit(X_test).transform(X_test) mlp = MLPClassifier(max_iter=1000, random_state=42) mlp.fit(X_train_scaled, y_train) print('Accuracy on the training subset: {:.3f}'.format(mlp.score(X_train_scaled, y_train))) print('Accuracy on the test subset: {:.3f}'.format(mlp.score(X_test_scaled, y_test))) # In[7]: mlp = MLPClassifier(max_iter=1000, alpha=1, random_state=42) mlp.fit(X_train_scaled, y_train) print('Accuracy on the training subset: {:.3f}'.format(mlp.score(X_train_scaled, y_train))) print('Accuracy on the test subset: {:.3f}'.format(mlp.score(X_test_scaled, y_test))) # In[9]: plt.figure(figsize=(20,5)) plt.imshow(mlp.coefs_[0], interpolation='None', cmap='GnBu') plt.yticks(range(30), cancer.feature_names) plt.xlabel('Columns in weight matrix') plt.ylabel('Input feature') plt.colorbar() # In[ ]: