├── BP neural network ├── README.md ├── bp-matlab │ ├── predict1(单预测) │ │ ├── cal.m │ │ ├── code_Octane.m │ │ ├── data_Octane.mat │ │ ├── 神经网络1:辛烷值和光谱分析.xlsx │ │ └── 贝叶斯(epoch=391).mat │ └── predict2(双预测) │ │ ├── cal2.m │ │ ├── code_Octane2.m │ │ ├── data_Octane2.mat │ │ ├── 代码计算的数据.mat │ │ └── 神经网络2:辛烷值和光谱分析(进阶版).xlsx └── bp-python │ ├── bpnei.py │ └── predict.py └── README.md /BP neural network/README.md: -------------------------------------------------------------------------------- 1 | # BP-neural-network 2 | BP神经网络预测实例--matlab代码实现 (个人觉得比python简单) 3 | 4 | BP神经网络预测实例--python代码实现 5 | 6 | 7 | -------------------------------------------------------------------------------- /BP neural network/bp-matlab/predict1(单预测)/cal.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ggy-king/BP-neural-network/6448f370b362876b5685d8cad08103c61b30071f/BP neural network/bp-matlab/predict1(单预测)/cal.m -------------------------------------------------------------------------------- /BP neural network/bp-matlab/predict1(单预测)/code_Octane.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ggy-king/BP-neural-network/6448f370b362876b5685d8cad08103c61b30071f/BP neural network/bp-matlab/predict1(单预测)/code_Octane.m -------------------------------------------------------------------------------- /BP neural network/bp-matlab/predict1(单预测)/data_Octane.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ggy-king/BP-neural-network/6448f370b362876b5685d8cad08103c61b30071f/BP neural network/bp-matlab/predict1(单预测)/data_Octane.mat -------------------------------------------------------------------------------- /BP neural network/bp-matlab/predict1(单预测)/神经网络1:辛烷值和光谱分析.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ggy-king/BP-neural-network/6448f370b362876b5685d8cad08103c61b30071f/BP neural network/bp-matlab/predict1(单预测)/神经网络1:辛烷值和光谱分析.xlsx -------------------------------------------------------------------------------- /BP neural network/bp-matlab/predict1(单预测)/贝叶斯(epoch=391).mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ggy-king/BP-neural-network/6448f370b362876b5685d8cad08103c61b30071f/BP neural network/bp-matlab/predict1(单预测)/贝叶斯(epoch=391).mat -------------------------------------------------------------------------------- /BP neural network/bp-matlab/predict2(双预测)/cal2.m: -------------------------------------------------------------------------------- 1 | clc; 2 | predict_y = zeros(10,2); % 初始化predict_y 3 | pre_test=mapminmax('apply',new_X(:,:)',inputps);% 对预测数据进行归一化 4 | for i = 1: 10 5 | result = sim(net, pre_test(:,i)); 6 | predict_y(i,1) = result(1); 7 | predict_y(i,2) = result(2); 8 | end 9 | disp('预测值为:') 10 | predict_y=predict_y'; 11 | predict_y=mapminmax('reverse',predict_y,outputps); %把预测结果还原 12 | disp(predict_y) 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /BP neural network/bp-matlab/predict2(双预测)/code_Octane2.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ggy-king/BP-neural-network/6448f370b362876b5685d8cad08103c61b30071f/BP neural network/bp-matlab/predict2(双预测)/code_Octane2.m -------------------------------------------------------------------------------- /BP neural network/bp-matlab/predict2(双预测)/data_Octane2.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ggy-king/BP-neural-network/6448f370b362876b5685d8cad08103c61b30071f/BP neural network/bp-matlab/predict2(双预测)/data_Octane2.mat -------------------------------------------------------------------------------- /BP neural network/bp-matlab/predict2(双预测)/代码计算的数据.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ggy-king/BP-neural-network/6448f370b362876b5685d8cad08103c61b30071f/BP neural network/bp-matlab/predict2(双预测)/代码计算的数据.mat -------------------------------------------------------------------------------- /BP neural network/bp-matlab/predict2(双预测)/神经网络2:辛烷值和光谱分析(进阶版).xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ggy-king/BP-neural-network/6448f370b362876b5685d8cad08103c61b30071f/BP neural network/bp-matlab/predict2(双预测)/神经网络2:辛烷值和光谱分析(进阶版).xlsx -------------------------------------------------------------------------------- /BP neural network/bp-python/bpnei.py: -------------------------------------------------------------------------------- 1 | # encoding:utf-8 2 | 3 | ''' 4 | BP神经网络Python实现 5 | ''' 6 | 7 | import random 8 | import numpy as np 9 | 10 | 11 | def sigmoid(x): 12 | ''' 13 | 激活函数 14 | ''' 15 | return 1.0 / (1.0 + np.exp(-x)) 16 | 17 | 18 | def sigmoid_prime(x): 19 | return sigmoid(x) * (1 - sigmoid(x)) 20 | 21 | 22 | class BPNNRegression: 23 | ''' 24 | 神经网络回归与分类的差别在于: 25 | 1. 输出层不需要再经过激活函数 26 | 2. 输出层的 w 和 b 更新量计算相应更改 27 | ''' 28 | 29 | def __init__(self, sizes): 30 | 31 | # 神经网络结构 32 | self.num_layers = len(sizes) 33 | self.sizes = sizes 34 | 35 | # 初始化偏差,除输入层外, 其它每层每个节点都生成一个 biase 值(0-1) 36 | self.biases = [np.random.randn(n, 1) for n in sizes[1:]] 37 | # 随机生成每条神经元连接的 weight 值(0-1) 38 | self.weights = [np.random.randn(r, c) 39 | for c, r in zip(sizes[:-1], sizes[1:])] 40 | 41 | def feed_forward(self, a): 42 | ''' 43 | 前向传输计算输出神经元的值 44 | ''' 45 | for i, b, w in zip(range(len(self.biases)), self.biases, self.weights): 46 | # 输出神经元不需要经过激励函数 47 | if i == len(self.biases) - 1: 48 | a = np.dot(w, a) + b 49 | break 50 | a = sigmoid(np.dot(w, a) + b) 51 | return a 52 | 53 | def MSGD(self, training_data, epochs, mini_batch_size, eta, error=0.01): 54 | ''' 55 | 小批量随机梯度下降法 56 | ''' 57 | n = len(training_data) 58 | for j in range(epochs): 59 | # 随机打乱训练集顺序 60 | random.shuffle(training_data) 61 | # 根据小样本大小划分子训练集集合 62 | mini_batchs = [training_data[k:k+mini_batch_size] 63 | for k in range(0, n, mini_batch_size)] 64 | # 利用每一个小样本训练集更新 w 和 b 65 | for mini_batch in mini_batchs: 66 | self.updata_WB_by_mini_batch(mini_batch, eta) 67 | 68 | # 迭代一次后结果 69 | err_epoch = self.evaluate(training_data) 70 | print("Epoch {0} Error {1}".format(j, err_epoch)) 71 | if err_epoch < error: 72 | break 73 | # if test_data: 74 | # print("Epoch {0}: {1} / {2}".format(j, self.evaluate(test_data), n_test)) 75 | # else: 76 | # print("Epoch {0}".format(j)) 77 | return err_epoch 78 | 79 | def updata_WB_by_mini_batch(self, mini_batch, eta): 80 | ''' 81 | 利用小样本训练集更新 w 和 b 82 | mini_batch: 小样本训练集 83 | eta: 学习率 84 | ''' 85 | # 创建存储迭代小样本得到的 b 和 w 偏导数空矩阵,大小与 biases 和 weights 一致,初始值为 0 86 | batch_par_b = [np.zeros(b.shape) for b in self.biases] 87 | batch_par_w = [np.zeros(w.shape) for w in self.weights] 88 | 89 | for x, y in mini_batch: 90 | # 根据小样本中每个样本的输入 x, 输出 y, 计算 w 和 b 的偏导 91 | delta_b, delta_w = self.back_propagation(x, y) 92 | # 累加偏导 delta_b, delta_w 93 | batch_par_b = [bb + dbb for bb, dbb in zip(batch_par_b, delta_b)] 94 | batch_par_w = [bw + dbw for bw, dbw in zip(batch_par_w, delta_w)] 95 | # 根据累加的偏导值 delta_b, delta_w 更新 b, w 96 | # 由于用了小样本,因此 eta 需除以小样本长度 97 | self.weights = [w - (eta / len(mini_batch)) * dw 98 | for w, dw in zip(self.weights, batch_par_w)] 99 | self.biases = [b - (eta / len(mini_batch)) * db 100 | for b, db in zip(self.biases, batch_par_b)] 101 | 102 | def back_propagation(self, x, y): 103 | ''' 104 | 利用误差后向传播算法对每个样本求解其 w 和 b 的更新量 105 | x: 输入神经元,行向量 106 | y: 输出神经元,行向量 107 | 108 | ''' 109 | delta_b = [np.zeros(b.shape) for b in self.biases] 110 | delta_w = [np.zeros(w.shape) for w in self.weights] 111 | 112 | # 前向传播,求得输出神经元的值 113 | a = x # 神经元输出值 114 | # 存储每个神经元输出 115 | activations = [x] 116 | # 存储经过 sigmoid 函数计算的神经元的输入值,输入神经元除外 117 | zs = [] 118 | for b, w in zip(self.biases, self.weights): 119 | z = np.dot(w, a) + b 120 | zs.append(z) 121 | a = sigmoid(z) # 输出神经元 122 | activations.append(a) 123 | # ------------- 124 | activations[-1] = zs[-1] # 更改神经元输出结果 125 | # ------------- 126 | # 求解输出层δ 127 | # 与分类问题不同,Delta计算不需要乘以神经元输入的倒数 128 | #delta = self.cost_function(activations[-1], y) * sigmoid_prime(zs[-1]) 129 | delta = self.cost_function(activations[-1], y) # 更改后 130 | # ------------- 131 | delta_b[-1] = delta 132 | delta_w[-1] = np.dot(delta, activations[-2].T) 133 | for lev in range(2, self.num_layers): 134 | # 从倒数第1层开始更新,因此需要采用-lev 135 | # 利用 lev + 1 层的 δ 计算 l 层的 δ 136 | z = zs[-lev] 137 | zp = sigmoid_prime(z) 138 | delta = np.dot(self.weights[-lev+1].T, delta) * zp 139 | delta_b[-lev] = delta 140 | delta_w[-lev] = np.dot(delta, activations[-lev-1].T) 141 | return (delta_b, delta_w) 142 | 143 | def evaluate(self, train_data): 144 | test_result = [[self.feed_forward(x), y] 145 | for x, y in train_data] 146 | return np.sum([0.5 * (x - y) ** 2 for (x, y) in test_result]) 147 | 148 | def predict(self, test_input): 149 | test_result = [self.feed_forward(x) 150 | for x in test_input] 151 | return test_result 152 | 153 | def cost_function(self, output_a, y): 154 | ''' 155 | 损失函数 156 | ''' 157 | return (output_a - y) 158 | pass 159 | -------------------------------------------------------------------------------- /BP neural network/bp-python/predict.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from sklearn import preprocessing 4 | import numpy as np 5 | import pandas as pd 6 | import matplotlib.pyplot as plt 7 | import BPNN 8 | from sklearn import metrics 9 | from sklearn.metrics import mean_absolute_error 10 | from sklearn.metrics import mean_squared_error 11 | # 导入必要的库 12 | df1 = pd.read_excel('2000.xls', 0) 13 | df1 = df1.iloc[:, :] 14 | # 进行数据归一化 15 | min_max_scaler = preprocessing.MinMaxScaler() 16 | df0 = min_max_scaler.fit_transform(df1) 17 | df = pd.DataFrame(df0, columns=df1.columns) 18 | x = df.iloc[:, :-1] 19 | y = df.iloc[:, -1] 20 | # 划分训练集测试集 21 | cut = 300 # 取最后cut=30天为测试集 22 | # 列表的切片操作,X.iloc[0:2400,0:7]即为1-2400行,1-7列 23 | x_train, x_test = x.iloc[:-cut], x.iloc[-cut:] 24 | y_train, y_test = y.iloc[:-cut], y.iloc[-cut:] 25 | x_train, x_test = x_train.values, x_test.values 26 | y_train, y_test = y_train.values, y_test.values 27 | # 神经网络搭建 28 | bp1 = BPNN.BPNNRegression([3, 16, 1]) 29 | train_data = [[sx.reshape(3, 1), sy.reshape(1, 1)] 30 | for sx, sy in zip(x_train, y_train)] 31 | test_data = [np.reshape(sx, (3, 1)) for sx in x_test] 32 | # 神经网络训练 33 | bp1.MSGD(train_data, 60000, len(train_data), 0.2) 34 | # 神经网络预测 35 | y_predict = bp1.predict(test_data) 36 | y_pre = np.array(y_predict) # 列表转数组 37 | y_pre = y_pre.reshape(300, 1) 38 | y_pre = y_pre[:, 0] 39 | # 画图 #展示在测试集上的表现 40 | draw = pd.concat([pd.DataFrame(y_test), pd.DataFrame(y_pre)], axis=1) 41 | draw.iloc[:, 0].plot(figsize=(12, 6)) 42 | draw.iloc[:, 1].plot(figsize=(12, 6)) 43 | plt.legend(('real', 'predict'), loc='upper right', fontsize='15') 44 | plt.title("Test Data", fontsize='30') # 添加标题 45 | # 输出精度指标 46 | print('测试集上的MAE/MSE') 47 | print(mean_absolute_error(y_pre, y_test)) 48 | print(mean_squared_error(y_pre, y_test)) 49 | mape = np.mean(np.abs((y_pre-y_test)/(y_test)))*100 50 | print('=============mape==============') 51 | print(mape, '%') 52 | # 画出真实数据和预测数据的对比曲线图 53 | print("R2 = ", metrics.r2_score(y_test, y_pre)) # R2 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BP-neural-network 2 | ## bp神经网络的预测 3 | ### matlab 4 | 5 | 自己测试了辛烷值和光谱分析 6 | 7 | ### python 8 | 9 | 可以用mape r2测试一下 10 | --------------------------------------------------------------------------------