mlp_classification

本文介绍了一种使用Keras构建的气体浓度预测模型,通过预处理数据并应用深度学习技术,实现了对不同气体类型的准确分类。模型采用多层神经网络结构,并通过MinMaxScaler进行特征缩放,训练过程使用Adam优化器,最终在测试集上获得了良好的分类精度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import keras
from keras import Sequential
from keras.layers import Dense, Dropout
from keras import backend
import numpy as np
from matplotlib import pyplot as plt
from keras.utils import to_categorical

np.seterr(divide='ignore', invalid='ignore')
gas = np.loadtxt('feature_result.txt')
# gas = all_data[np.where(all_data[:, 0] == 4)]
# np.savetxt('gas.txt', gas, fmt='%.5f'),目的是找出4号气体的浓度和所有数据
np.random.seed(133)
np.random.shuffle(gas)
gas_max_min = gas[:, 0:15]

# # z-score方法
# gas_mean = np.mean(gas_z_score, axis=0)
# gas_std = np.std(gas_z_score, axis=0)
# gas_new = (gas_z_score - gas_mean) / gas_std

from sklearn import preprocessing
min_max_scaler = preprocessing.MinMaxScaler()
gas_new = min_max_scaler.fit_transform(gas_max_min)

gas_train_attr = gas_new[:int(gas_new.shape[0] * 0.75)]
gas_train_label_origin = gas[:int(gas.shape[0] * 0.75), 15].reshape(-1, 1)
print(gas_train_label_origin)
gas_train_label = to_categorical(gas_train_label_origin)  #转换为只有0和1的二进制类型

gas_test_attr = gas_new[int(gas_new.shape[0] * 0.75):]
gas_test_label_origin = gas[int(gas.shape[0] * 0.75):, 15].reshape(-1, 1)
gas_test_label = to_categorical(gas_test_label_origin)
print(gas_test_label_origin)



model = Sequential()
model.add(Dense(100, activation='relu'))
model.add(Dense(50, activation='relu'))
# model.add(Dense(50, activation='relu'))
model.add(Dropout(0.2))

# model.add(Dense(128, activation='relu'))
# model.add(Dropout(0.5))
# model.add(Dense(128, activation='relu'))
model.add(Dense(4, activation='softmax'))
# sgd = keras.optimizers.SGD(lr=0.1, decay=1e-6, nesterov=True)
model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])

his = model.fit(gas_train_attr, gas_train_label, validation_data=(gas_test_attr, gas_test_label), epochs=2000)
# plt.plot(hist.history['acc'])
plt.plot(his.history['acc'])
plt.plot(his.history['val_acc'])
plt.title("")
plt.ylabel("Classification Accuracy")
plt.xlabel("epoch")
plt.legend(["Training set","Validation set"],loc="lower right")
plt.show()
# gas_val_predict = model.predict(gas_test_attr[:int(gas_test_attr.shape[0] * 0.5)])
gas_test_predict = model.predict(gas_test_attr) 
gas_test_predict = (np.argmax(gas_test_predict, axis = 1)).reshape(-1, 1)
print(gas_test_predict)
gas_train_predict = model.predict(gas_train_attr) 
gas_train_predict = (np.argmax(gas_train_predict, axis = 1)).reshape(-1, 1)
print(gas_train_predict)


# a=range(len(his.history['acc']))  #np.arange()返回的是一个一个array,而range返回的是一个list
# np.savetxt('min_max_sgd.txt', np.hstack((a, his.history['acc'])))

# for i in range(len(his.history['acc'])):
# 	a.append(his.history['acc'][i])
	
	
# model.evaluate(gas_test_attr, gas_test_label)

np.savetxt('gas_train_result.txt', np.hstack((gas_train_label_origin, gas_train_predict)),
           fmt='%.5f')

np.savetxt('gas_test_result.txt', np.hstack((gas_test_label_origin, gas_test_predict)),
           fmt='%.5f')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值