Auto Byte

Science AI

# 初学者入门：如何用Python和SciKit Learn 0.18实现神经网络？

GitHub 链接：https://github.com/Rogerh91/Springboard-Blog-Tutorials/blob/master/Neural%20Networks%20/JMPortilla_SpringBoard_Blog_Neural_Network.ipynb

SciKit-Learn

Anaconda 和 iPython Notebook

Anaconda 的 iPython Notebook（Jupyter Notebook）软件可以轻松地帮助你安装 SciKit-Learn 以及所需的所有工具。下面的链接中有如何安装这些软件的教程（https://www.safaribooksonline.com/blog/2013/12/12/start-ipython-notebook/），以便你快速在 Python 中构建神经网络。

`import pandas as pdwine = pd.read_csv('wine_data.csv', names = ["Cultivator", "Alchol", "Malic_Acid", "Ash", "Alcalinity_of_Ash", "Magnesium", "Total_phenols", "Falvanoids", "Nonflavanoid_phenols", "Proanthocyanins", "Color_intensity", "Hue", "OD280", "Proline"])`

In [9]:

`wine.head()`

Out[9] 为了符合本文的格式，我们截取了部分列（实际数据有更多列）：

In [12]:

`wine.describe().transpose()`

Out[12]为了符合本文的格式，从输出中去掉了标准偏差（std）和计数列：

In [13]:

`# 178 data points with 13 features and 1 label columnwine.shape`

Out[13]:

`(178, 14)`

In [14]:

`X = wine.drop('Cultivator',axis=1)y = wine['Cultivator']`

In [15]:

`from sklearn.model_selection import train_test_split`

In [16]:

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

In [17]:

`from sklearn.preprocessing import StandardScaler`

In [18]:

`scaler = StandardScaler()`

In [19]:

`# Fit only to the training datascaler.fit(X_train)`

Out[19]:

`StandardScaler(copy=True, with_mean=True, with_std=True)`

In [20]:

`# Now apply the transformations to the data:X_train = scaler.transform(X_train)X_test = scaler.transform(X_test)`

In [21]:

`from sklearn.neural_network import MLPClassifier`

In [24]:

`mlp = MLPClassifier(hidden_layer_sizes=(13,13,13),max_iter=500)`

In [25]:

`mlp.fit(X_train,y_train)`

Out[25]:

`MLPClassifier(activation='relu', alpha=0.0001, batch_size='auto', beta_1=0.9,       beta_2=0.999, early_stopping=False, epsilon=1e-08,       hidden_layer_sizes=(13, 13, 13), learning_rate='constant',       learning_rate_init=0.001, max_iter=500, momentum=0.9,       nesterovs_momentum=True, power_t=0.5, random_state=None,       shuffle=True, solver='adam', tol=0.0001, validation_fraction=0.1,       verbose=False, warm_start=False)`

In [26]:

`predictions = mlp.predict(X_test)`

In [27]:

`from sklearn.metrics import classification_report,confusion_matrix`

In [28]:

`print(confusion_matrix(y_test,predictions))`
`[[17  0  0] [ 0 14  1] [ 0  0 13]]`

In [29]:

`print(classification_report(y_test,predictions))`
`            precision    recall  f1-score   support          1       1.00      1.00      1.00        17          2       1.00      0.93      0.97        15          3       0.93      1.00      0.96        13avg / total       0.98      0.98      0.98        45`

coefs_ 是权重矩阵的列表，其中索引 i 处的权重矩阵表示层 i 和层 i+1 之间的权重。

intercepts_ 是偏差向量的列表，其中索引 i 处的向量表示添加到层 i+1 的偏差值。

In [30]:

`len(mlp.coefs_)`

Out[30]:

`4`

In [31]:

`len(mlp.coefs_[0])`

Out[31]:

`13`

In [32]:

`len(mlp.intercepts_[0])`

Out[32]:

`13`