# 教程 | 初学者入门：如何用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 pd
wine = 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)`

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

`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 data
scaler.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,
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        13

avg / 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`

0 条评论

## 相关文章

1203

3685

3947

### （数据科学学习手札35）tensorflow初体验

TensorFlow时谷歌于2015年11月宣布在Github上开源的第二代分布式机器学习系统，目前仍处于快速开发迭代中，有大量的新功能新特性在陆续研发中；

4166

### CS231n 课后作业第二讲 : Assignment 2（含代码实现）| 分享总结

CS231n 是斯坦福大学开设的计算机视觉与深度学习的入门课程，授课内容在国内外颇受好评。其配套的课后作业质量也颇高，因此雷锋网 AI 研习社在近期的线上公开...

48010

1561

63113

3506

3554

3418