BP神经网络数据预测(excel)

整体的处理思路是
先加载训练数据,对数据进行训练,然后再进行预测,最后输出预测值。

main()

import numpy as py
import DataLoad as dataload
import ModelTrain as modeltrain
import DataPrediction as dataprediction


def main():
    data_train = dataload.load_data_train() # 加载训练数据 
    print(data_train) 
    modeltrain.Train_Model(data_train) # 对数据进行训练
    data_pre = dataload.load_data_pre() # 加载需要预测的数据
    data_real = dataload.load_data_real() # 加载数据的真实值
    print(data_pre)
    print(3)
    pre_result = dataprediction.Predict_Data(data_pre) 进行预测
    print("真实值为:%d"%(data_real)+" 预测结果为:%d" % (pre_result))


if __name__ == '__main__':
    main()

数据加载 DataLoad

import numpy as np
import pandas as pd
data = pd.read_csv("./2015(z).CSV") #读取csv格式的文件全部数据
data1 = np.mat(data) #数据矩阵化
pre_raw = 30 #需要预测值所在行数
# print (data1)
def load_data_train():
    data_train = data1[0:26, 3:47] # 取出数据的第1行到26行中的第4列到47列作为训练数据
    print(data_train)
    return data_train

def load_data_pre():
    data_pre = data1[pre_raw, 3:46].astype('float64') #第31行第4列到第46列为输入数据
    data_mean = data_pre.mean() #求平均值
    data_std = data_pre.std() #求标准差
    data_pre = (data_pre - data_mean) / data_std #标准化
    return data_pre

def load_data_real():
    data_real = data1[pre_raw, 46]  取出第31行的第47列数据
    return data_real

模型训练 ModelTrain

import numpy as np
from keras.models import Sequential
from keras.layers.core import Dense, Activation



def Train_Model(data_train):
    modelfile = './modelweight' #此位置保存训练模型过程中的权重
    y_mean_std = "./y_mean_std.txt" # 保存标准化过程中的数据,后边数据还原需要用到
    data_train = np.matrix(data_train).astype('float64')
    data_mean = np.mean(data_train, axis=0)#对列求平均值
    data_std = np.std(data_train, axis=0)#计算每一列的标准差
    # data_train = (data_train - data_mean) / data_std
    print(1)
    x_train = data_train[:, 0:(data_train.shape[1] - 1)] #所有数据(除最后一列)作为输入x
    y_train = data_train[:, data_train.shape[1] - 1] #所有数据的最后一列作为输出y
    print(x_train)
    print(y_train)
    #模型训练
    model = Sequential()
    model.add(Dense(x_train.shape[1], input_dim=x_train.shape[1], kernel_initializer="uniform"))
    model.add(Activation('relu'))
    model.add(Dense(1, input_dim=x_train.shape[1]))
    model.compile(loss='mean_squared_error', optimizer='adam')
    model.fit(x_train, y_train, epochs=40, batch_size=x_train.shape[0])
    model.save_weights(modelfile) #保存模型权重
    y_mean = data_mean[:, data_train.shape[1] - 1]
    y_std = data_std[:, data_train.shape[1] - 1]
    print("训练完毕")
    # 将标准差过程中的参数写入文件
    f = open(y_mean_std, "w") 
    mean_std = str(y_mean.astype(str)) + " " + str(y_std.astype(str))
    mean_std = mean_std.replace("[", "")
    mean_std = mean_std.replace("]", "")
    mean_std = mean_std.replace("'", "")
    f.write(mean_std)

数据预测 DataPrediction

import numpy as np
from keras.models import Sequential
from keras.layers.core import Dense, Activation

def load_y():
    f = open("./y_mean_std.txt", "r") 
    y_mean = f.read()
    y_mean = y_mean.split(" ")
    f.close()
    return y_mean

def Predict_Data(data_pre):
    model = Sequential()
    model.add(Dense(data_pre.shape[1], input_dim=data_pre.shape[1], kernel_initializer="uniform"))
    print(data_pre.shape[1])
    model.add(Activation('relu'))
    model.add(Dense(1, input_dim=data_pre.shape[1]))
    model.compile(loss='mean_squared_error', optimizer='adam')
    model.load_weights('./modelweight')
    mean_std = load_y()
    pre_result = model.predict(data_pre) * float(mean_std[1]) + float(mean_std[0])
    print(model.summary())#输出参数param计算过程
    return pre_result

你可能感兴趣的:(python,神经网络)