【python 神经网络】BP神经网络python实现-iris数据集分类

输入数据集iris:
(只有两类Iris-virginica or Iris-versicolor. 100条)

sepal_length    sepal_width petal_length    petal_width species
7   3.2 4.7 1.4 Iris-versicolor
6.4 3.2 4.5 1.5 Iris-versicolor
6.9 3.1 4.9 1.5 Iris-versicolor
5.5 2.3 4   1.3 Iris-versicolor
6.5 2.8 4.6 1.5 Iris-versicolor
5.7 2.8 4.5 1.3 Iris-versicolor
6.3 3.3 4.7 1.6 Iris-versicolor
4.9 2.4 3.3 1   Iris-versicolor
6.6 2.9 4.6 1.3 Iris-versicolor
5.2 2.7 3.9 1.4 Iris-versicolor
5   2   3.5 1   Iris-versicolor
5.9 3   4.2 1.5 Iris-versicolor
6   2.2 4   1   Iris-versicolor
6.1 2.9 4.7 1.4 Iris-versicolor
5.6 2.9 3.6 1.3 Iris-versicolor
6.7 3.1 4.4 1.4 Iris-versicolor
5.6 3   4.5 1.5 Iris-versicolor
5.8 2.7 4.1 1   Iris-versicolor
6.2 2.2 4.5 1.5 Iris-versicolor
5.6 2.5 3.9 1.1 Iris-versicolor
5.9 3.2 4.8 1.8 Iris-versicolor
6.1 2.8 4   1.3 Iris-versicolor
6.3 2.5 4.9 1.5 Iris-versicolor
6.1 2.8 4.7 1.2 Iris-versicolor
6.4 2.9 4.3 1.3 Iris-versicolor
6.6 3   4.4 1.4 Iris-versicolor
6.8 2.8 4.8 1.4 Iris-versicolor
6.7 3   5   1.7 Iris-versicolor
6   2.9 4.5 1.5 Iris-versicolor
5.7 2.6 3.5 1   Iris-versicolor
5.5 2.4 3.8 1.1 Iris-versicolor
5.5 2.4 3.7 1   Iris-versicolor
5.8 2.7 3.9 1.2 Iris-versicolor
6   2.7 5.1 1.6 Iris-versicolor
5.4 3   4.5 1.5 Iris-versicolor
6   3.4 4.5 1.6 Iris-versicolor
6.7 3.1 4.7 1.5 Iris-versicolor
6.3 2.3 4.4 1.3 Iris-versicolor
5.6 3   4.1 1.3 Iris-versicolor
5.5 2.5 4   1.3 Iris-versicolor
5.5 2.6 4.4 1.2 Iris-versicolor
6.1 3   4.6 1.4 Iris-versicolor
5.8 2.6 4   1.2 Iris-versicolor
5   2.3 3.3 1   Iris-versicolor
5.6 2.7 4.2 1.3 Iris-versicolor
5.7 3   4.2 1.2 Iris-versicolor
5.7 2.9 4.2 1.3 Iris-versicolor
6.2 2.9 4.3 1.3 Iris-versicolor
5.1 2.5 3   1.1 Iris-versicolor
5.7 2.8 4.1 1.3 Iris-versicolor
6.3 3.3 6   2.5 Iris-virginica
5.8 2.7 5.1 1.9 Iris-virginica
7.1 3   5.9 2.1 Iris-virginica
6.3 2.9 5.6 1.8 Iris-virginica
6.5 3   5.8 2.2 Iris-virginica
7.6 3   6.6 2.1 Iris-virginica
4.9 2.5 4.5 1.7 Iris-virginica
7.3 2.9 6.3 1.8 Iris-virginica
6.7 2.5 5.8 1.8 Iris-virginica
7.2 3.6 6.1 2.5 Iris-virginica
6.5 3.2 5.1 2   Iris-virginica
6.4 2.7 5.3 1.9 Iris-virginica
6.8 3   5.5 2.1 Iris-virginica
5.7 2.5 5   2   Iris-virginica
5.8 2.8 5.1 2.4 Iris-virginica
6.4 3.2 5.3 2.3 Iris-virginica
6.5 3   5.5 1.8 Iris-virginica
7.7 3.8 6.7 2.2 Iris-virginica
7.7 2.6 6.9 2.3 Iris-virginica
6   2.2 5   1.5 Iris-virginica
6.9 3.2 5.7 2.3 Iris-virginica
5.6 2.8 4.9 2   Iris-virginica
7.7 2.8 6.7 2   Iris-virginica
6.3 2.7 4.9 1.8 Iris-virginica
6.7 3.3 5.7 2.1 Iris-virginica
7.2 3.2 6   1.8 Iris-virginica
6.2 2.8 4.8 1.8 Iris-virginica
6.1 3   4.9 1.8 Iris-virginica
6.4 2.8 5.6 2.1 Iris-virginica
7.2 3   5.8 1.6 Iris-virginica
7.4 2.8 6.1 1.9 Iris-virginica
7.9 3.8 6.4 2   Iris-virginica
6.4 2.8 5.6 2.2 Iris-virginica
6.3 2.8 5.1 1.5 Iris-virginica
6.1 2.6 5.6 1.4 Iris-virginica
7.7 3   6.1 2.3 Iris-virginica
6.3 3.4 5.6 2.4 Iris-virginica
6.4 3.1 5.5 1.8 Iris-virginica
6   3   4.8 1.8 Iris-virginica
6.9 3.1 5.4 2.1 Iris-virginica
6.7 3.1 5.6 2.4 Iris-virginica
6.9 3.1 5.1 2.3 Iris-virginica
5.8 2.7 5.1 1.9 Iris-virginica
6.8 3.2 5.9 2.3 Iris-virginica
6.7 3.3 5.7 2.5 Iris-virginica
6.7 3   5.2 2.3 Iris-virginica
6.3 2.5 5   1.9 Iris-virginica
6.5 3   5.2 2   Iris-virginica
6.2 3.4 5.4 2.3 Iris-virginica
5.9 3   5.1 1.8 Iris-virginica

代码:


#coding:utf-8
from __future__ import division
import pandas
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import roc_auc_score
from sklearn.metrics import confusion_matrix
################定义激活函数~##########################################

def sigmoid_activation(x, theta):
    x = np.asarray(x)
    theta = np.asarray(theta)
    return 1 / (1 + np.exp(-np.dot(theta.T, x)))



# 将模型的函数凝结为一个类,这是很好的一种编程习惯
class NNet3:
    # 初始化必要的几个参数
    def __init__(self, learning_rate=0.5, maxepochs=1e4, convergence_thres=1e-5, hidden_layer=4):
        self.learning_rate = learning_rate
        self.maxepochs = int(maxepochs)
        self.convergence_thres = 1e-5
        self.hidden_layer = int(hidden_layer)

    #  计算最终的误差
    def _multiplecost(self, X, y):
        # l1是中间层的输出,l2是输出层的结果
        l1, l2 = self._feedforward(X)
        # 计算误差,这里的l2是前面的h
        inner = y * np.log(l2) + (1-y) * np.log(1-l2)
        # 添加符号,将其转换为正值
        return -np.mean(inner)

    # 前向传播函数计算每层的输出结果
    def _feedforward(self, X):
        # l1是中间层的输出
        l1 = sigmoid_activation(X.T, self.theta0).T
        # 为中间层添加一个常数列
        l1 = np.column_stack([np.ones(l1.shape[0]), l1])
        # 中间层的输出作为输出层的输入产生结果l2
        l2 = sigmoid_activation(l1.T, self.theta1)
        return l1, l2

    # 传入一个结果未知的样本,返回其属于1的概率
    def predict(self, X):
        _, y = self._feedforward(X)
        return y

    # 学习参数,不断迭代至参数收敛,误差最小化
    def learn(self, X, y):
        nobs, ncols = X.shape
        self.theta0 = np.random.normal(0,0.01,size=(ncols,self.hidden_layer))
        self.theta1 = np.random.normal(0,0.01,size=(self.hidden_layer+1,1))

        self.costs = []
        cost = self._multiplecost(X, y)
        self.costs.append(cost)
        costprev = cost + self.convergence_thres+1
        counter = 0

        for counter in range(self.maxepochs):
            # 计算中间层和输出层的输出
            l1, l2 = self._feedforward(X)

            # 首先计算输出层的梯度,再计算中间层的梯度
            l2_delta = (y-l2) * l2 * (1-l2)
            l1_delta = l2_delta.T.dot(self.theta1.T) * l1 * (1-l1)

            # 更新参数
            self.theta1 += l1.T.dot(l2_delta.T) / nobs * self.learning_rate
            self.theta0 += X.T.dot(l1_delta)[:,1:] / nobs * self.learning_rate

            counter += 1
            costprev = cost
            cost = self._multiplecost(X, y)  # get next cost
            self.costs.append(cost)
            if np.abs(costprev-cost) < self.convergence_thres and counter > 500:
                break



if __name__ == '__main__':

    ###################################################数据处理#############################
    #############读取数据##################
    iris = pandas.read_csv("c:/pic2/iris2.csv")

    # 打乱数据顺序################
    shuffled_rows = np.random.permutation(iris.index)

    iris = iris.iloc[shuffled_rows]
    print(iris.species.unique())

    # 添加一个值全为1的属性iris["ones"],截距
    iris["ones"] = np.ones(iris.shape[0])
    X = iris[['ones', 'sepal_length', 'sepal_width', 'petal_length', 'petal_width']].values

    # 将Iris-versicolor类标签设置为1,Iris-virginica设置为0
    y = (iris.species == 'Iris-versicolor').values.astype(int)

    # First 70 rows to X_train and y_train
    # Last 30 rows to X_train and y_train
    X_train = X[:70]
    y_train = y[:70]

    X_test = X[-30:]
    y_test = y[-30:]


    ###################################################训练模型##################################


    # Set a learning rate
    learning_rate = 0.5
    # Maximum number of iterations for gradient descent
    maxepochs = 10000
    # Costs convergence threshold, ie. (prevcost - cost) > convergence_thres
    convergence_thres = 0.00001
    # Number of hidden units
    hidden_units = 8

    # Initialize model
    model = NNet3(learning_rate=learning_rate, maxepochs=maxepochs,
                  convergence_thres=convergence_thres, hidden_layer=hidden_units)
    model.learn(X_train, y_train)
    train_yhat = model.predict(X_train)[0]

    print y_train
    print train_yhat
    train_auc = roc_auc_score(y_train, train_yhat)

    print train_auc



    ########################################预测数据##############################

    # 因为predict返回的是一个二维数组,此处是(1,30),取第一列作为一个列向量
    yhat = model.predict(X_test)[0]

    print y_test
    print yhat






    predict=[]
    for each in yhat:
        if each>0.5:
            predict.append(1)
        else:
            predict.append(0)


    print predict

    auc = roc_auc_score(y_test, yhat)

    print auc


    #################################写出数据##########################
    ######################合并各列数据#########################
    result=np.column_stack([X_test,y_test,predict])
    print result
    count=0
    for i in range(0,len(result)):
         if result[i,5]==result[i,6]:
             count+=1

    ################计算准确率#############################
    print count,len(result)
    acurate=count/len(result)


    print("分类正确率是:%.2f%%" % (acurate * 100))

    labels = list(set(predict))
    print labels
    conf_mat = confusion_matrix(y_test, predict, labels=labels)

    print conf_mat




    #####################数组转换为数据框##########################
    result=pandas.DataFrame(result[:,1:])

    result.columns = ['sepal_length', 'sepal_width','petal_length', 'petal_width', 'species', 'predict']


    print result
    #########################写出数据到excel################

    pandas.DataFrame.to_excel(result,"c:/pic2/iris_test.xlsx",index=False)



    # Plot costs
    plt.plot(model.costs,color="red")
    plt.title("Convergence of the Cost Function")
    plt.ylabel("J($\Theta$)")
    plt.xlabel("Iteration")
    plt.show()

**参考文档:**
http://blog.csdn.net/zm714981790/article/details/51251759

数据结果:

"D:\Program Files\Python27\python.exe" D:/PycharmProjects/learn2017/神经网络2.py
['Iris-virginica' 'Iris-versicolor']
[0 0 0 0 1 1 0 1 1 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 1 0 0 1 1 1 1 1 0 1 0 1 0
 1 1 0 1 0 1 1 0 0 0 0 1 0 0 1 0 0 1 1 1 0 0 0 0 1 0 0 0 0 1 1 1 1]
[ 0.01278021  0.03439898  0.05224163  0.02579433  0.96424581  0.97245354
  0.04763097  0.99804094  0.98466651  0.01102125  0.99479731  0.46451001
  0.96449451  0.97320347  0.01321102  0.99788935  0.0436803   0.01704671
  0.9684925   0.02788267  0.99258103  0.72842201  0.07494959  0.99612339
  0.99474585  0.02124948  0.25277002  0.12688416  0.99590699  0.92917313
  0.91612793  0.91472397  0.15668975  0.94841691  0.00800775  0.99040117
  0.10989638  0.50687487  0.7886392   0.02579433  0.99743259  0.07686945
  0.99563854  0.96114819  0.0088116   0.30712743  0.02865614  0.09481013
  0.99820782  0.00899377  0.18691793  0.4053217   0.03111883  0.35882085
  0.9983492   0.99575405  0.80469569  0.09394371  0.02035053  0.00950211
  0.0730268   0.95653785  0.01866106  0.0120546   0.34266925  0.10870798
  0.99268798  0.98584769  0.98791089  0.99770239]
0.993469387755
[0 0 1 0 1 0 1 1 1 1 0 0 0 1 0 0 1 0 0 1 1 1 1 1 0 0 0 0 1 1]
[ 0.02195583  0.02268384  0.98236664  0.25561003  0.99966941  0.00412936
  0.98963493  0.99168628  0.99901144  0.99799403  0.00805732  0.01058136
  0.00322866  0.97047     0.04617823  0.08379857  0.99846099  0.01088541
  0.01550168  0.99963445  0.8980127   0.99856116  0.99852156  0.9974298
  0.02843341  0.02362997  0.03991824  0.30031872  0.66308005  0.97105807]
[0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1]
1.0
[[ 1.   6.9  3.2  5.7  2.3  0.   0. ]
 [ 1.   7.3  2.9  6.3  1.8  0.   0. ]
 [ 1.   6.8  2.8  4.8  1.4  1.   1. ]
 [ 1.   6.3  2.7  4.9  1.8  0.   0. ]
 [ 1.   5.7  2.6  3.5  1.   1.   1. ]
 [ 1.   6.3  3.3  6.   2.5  0.   0. ]
 [ 1.   6.7  3.1  4.7  1.5  1.   1. ]
 [ 1.   5.   2.   3.5  1.   1.   1. ]
 [ 1.   5.6  2.9  3.6  1.3  1.   1. ]
 [ 1.   5.8  2.7  4.1  1.   1.   1. ]
 [ 1.   7.7  2.8  6.7  2.   0.   0. ]
 [ 1.   6.7  3.3  5.7  2.5  0.   0. ]
 [ 1.   7.7  2.6  6.9  2.3  0.   0. ]
 [ 1.   6.5  2.8  4.6  1.5  1.   1. ]
 [ 1.   6.4  2.7  5.3  1.9  0.   0. ]
 [ 1.   5.9  3.   5.1  1.8  0.   0. ]
 [ 1.   6.7  3.1  4.4  1.4  1.   1. ]
 [ 1.   6.3  3.4  5.6  2.4  0.   0. ]
 [ 1.   5.7  2.5  5.   2.   0.   0. ]
 [ 1.   5.1  2.5  3.   1.1  1.   1. ]
 [ 1.   5.5  2.6  4.4  1.2  1.   1. ]
 [ 1.   5.   2.3  3.3  1.   1.   1. ]
 [ 1.   4.9  2.4  3.3  1.   1.   1. ]
 [ 1.   6.   2.2  4.   1.   1.   1. ]
 [ 1.   6.3  2.9  5.6  1.8  0.   0. ]
 [ 1.   7.1  3.   5.9  2.1  0.   0. ]
 [ 1.   4.9  2.5  4.5  1.7  0.   0. ]
 [ 1.   6.1  3.   4.9  1.8  0.   0. ]
 [ 1.   5.4  3.   4.5  1.5  1.   1. ]
 [ 1.   6.3  2.3  4.4  1.3  1.   1. ]]
30 30
分类正确率是:100.00%
[0, 1]
[[15  0]
 [ 0 15]]
    sepal_length  sepal_width  petal_length  petal_width  species  predict
0            6.9          3.2           5.7          2.3      0.0      0.0
1            7.3          2.9           6.3          1.8      0.0      0.0
2            6.8          2.8           4.8          1.4      1.0      1.0
3            6.3          2.7           4.9          1.8      0.0      0.0
4            5.7          2.6           3.5          1.0      1.0      1.0
5            6.3          3.3           6.0          2.5      0.0      0.0
6            6.7          3.1           4.7          1.5      1.0      1.0
7            5.0          2.0           3.5          1.0      1.0      1.0
8            5.6          2.9           3.6          1.3      1.0      1.0
9            5.8          2.7           4.1          1.0      1.0      1.0
10           7.7          2.8           6.7          2.0      0.0      0.0
11           6.7          3.3           5.7          2.5      0.0      0.0
12           7.7          2.6           6.9          2.3      0.0      0.0
13           6.5          2.8           4.6          1.5      1.0      1.0
14           6.4          2.7           5.3          1.9      0.0      0.0
15           5.9          3.0           5.1          1.8      0.0      0.0
16           6.7          3.1           4.4          1.4      1.0      1.0
17           6.3          3.4           5.6          2.4      0.0      0.0
18           5.7          2.5           5.0          2.0      0.0      0.0
19           5.1          2.5           3.0          1.1      1.0      1.0
20           5.5          2.6           4.4          1.2      1.0      1.0
21           5.0          2.3           3.3          1.0      1.0      1.0
22           4.9          2.4           3.3          1.0      1.0      1.0
23           6.0          2.2           4.0          1.0      1.0      1.0
24           6.3          2.9           5.6          1.8      0.0      0.0
25           7.1          3.0           5.9          2.1      0.0      0.0
26           4.9          2.5           4.5          1.7      0.0      0.0
27           6.1          3.0           4.9          1.8      0.0      0.0
28           5.4          3.0           4.5          1.5      1.0      1.0
29           6.3          2.3           4.4          1.3      1.0      1.0

Process finished with exit code 0
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读