dataSet=pd.read_csv('data2.csv',engine='python') train=dataSet.iloc[ :16000,:]#前800行 #print(train) test= dataSet.iloc[16000:,]#800行后 #print(test) #训练输入数据 train_x=train.iloc[ :,:-1] print('train_x',type(train_x)) #print(train_data) train_y=train.iloc[:,-1] #print(train_label) #测试数据 test_x=test.iloc[ :,:-1] test_y=test.iloc[:,-1] #加载的训练集和测试集转化为tensor格式 x_train = tf.convert_to_tensor(train_x) y_train = tf.convert_to_tensor(train_y) x_test =tf.convert_to_tensor(test_x) y_test =tf.convert_to_tensor(test_y) #构建一个结构为[18,15,1]的BP神经网络 model = tf.keras.Sequential( [tf.keras.layers.Dense(15, activation='tanh',input_shape=(18,)), tf.keras.layers.Dense(1)]) model.compile(optimizer='adam',loss='mse',metrics=[tf.keras.metrics.sparse_categorical_accuracy])#tf.keras.optimizers.Adam(lr=0.00035) model.fit(x_train, y_train, epochs=10) model.summary() print('x_test',x_test) y_predict = model.predict(x_test) print('y_predict',y_predict) test_loss,test_accuracy=model.evaluate(x_test,y_test) print('test_accuracy是',test_loss,test_accuracy) plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 plt.figure(1
) plt.plot( y_test, color = 'red', linewidth=0.2,label = '实际值') plt.plot(y_predict,color = 'green',label = '预测值') plt.xlabel('空预器运行时间') plt.ylabel('压差(KPa)') plt.title('空气预热器压差预测结果图') plt.show()